﻿using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(FOHEART_Mocap.FoheartGloveH1Pose))]
public class FOHEART_ModelEditor : Editor 
{
    SerializedProperty skeletonObjectsField;
    SerializedProperty endPointObjectsField;

    public override void OnInspectorGUI()
    {
        FOHEART_Mocap.FoheartGloveH1Pose modelScript = (FOHEART_Mocap.FoheartGloveH1Pose)target;
        //default
        DrawDefaultInspector();

        if (GUILayout.Button("Auto bind skeletons"))
        {
            Debug.Log("Load transforms references into skeletons list.");
            modelScript.FindAllSkeletons();
            EditorUtility.SetDirty(modelScript);
            UnityEditor.SceneManagement.EditorSceneManager.MarkAllScenesDirty();
        }
        if (GUILayout.Button("Clear binded skeletons"))
        {
            Debug.Log("Clear all transforms references of skeletons list.");

            modelScript.ClearAllSkeletons();
            EditorUtility.SetDirty(modelScript);
            UnityEditor.SceneManagement.EditorSceneManager.MarkAllScenesDirty();
        }

        serializedObject.Update();
        skeletonObjectsField = serializedObject.FindProperty("skeletonObjects");

        EditorGUI.indentLevel += 1;
        for (int i = 0; i < skeletonObjectsField.arraySize; i++)
        {
            EditorGUILayout.PropertyField(
                skeletonObjectsField.GetArrayElementAtIndex(i), 
                new GUIContent(((FOHEART_Mocap.FoheartSkeletonName.kinemHumanHandsSkeleton32Index)i).ToString())
                );
        }
        EditorGUI.indentLevel -= 1;
        serializedObject.ApplyModifiedProperties();

#if false
        /*endpoint*/
        if (GUILayout.Button("Auto bind endPoint"))
        {
            Debug.Log("Load skeleton endpoint into list.");
            modelScript.FindAllEndPoints();
            EditorUtility.SetDirty(modelScript);
            UnityEditor.SceneManagement.EditorSceneManager.MarkAllScenesDirty();
        }
        if (GUILayout.Button("Clear binded endPoint"))
        {
            Debug.Log("Clear all endPoint of skeletons list.");

            modelScript.ClearAllEndPoint();
            EditorUtility.SetDirty(modelScript);
            UnityEditor.SceneManagement.EditorSceneManager.MarkAllScenesDirty();
        }

        serializedObject.Update();
        endPointObjectsField = serializedObject.FindProperty("endPointObjects");

        EditorGUI.indentLevel += 1;
        for (int i = 0; i < endPointObjectsField.arraySize; i++)
        {
            EditorGUILayout.PropertyField(
                endPointObjectsField.GetArrayElementAtIndex(i), 
                new GUIContent(((FOHEART_Mocap.FoheartSkeletonName.kinemHumanHandsEndPoint12Index)i).ToString())
                );
        }
        EditorGUI.indentLevel -= 1;
        serializedObject.ApplyModifiedProperties();
#endif


    }
}

