using UnityEditor;
//Created by Neodrop
//neodrop@unity3d.ru
using UnityEngine;

[CustomEditor(typeof(CsBiped))]
public class CsBipedEditor : Editor 
{
    public Texture bodyTexture;
    CsBiped targetObject;
    SerializedObject serializedObject;
    static int w = 339, h = 341;

	void OnEnable () 
    {
        targetObject = target as CsBiped;
        serializedObject = new SerializedObject(target);
        serializedObject.Update();
        if (bodyTexture == null)
        {
            bodyTexture = AntaresGameObjectCustom.Routines.imageGoldMan;
            string bipName = "";
            foreach (Transform tr in targetObject.gameObject.GetComponentsInChildren<Transform>())
            {
                bipName = tr.gameObject.name;

                if (bipName.Contains("R Hand"))targetObject.handRight = tr;
                else if (bipName.Contains("L Hand"))targetObject.handLeft = tr;
                else if (bipName.Contains(" Head"))targetObject.head = tr;
                else if (bipName.Contains("R Toe0") && targetObject.footRight == null)targetObject.footRight = tr;
                else if (bipName.Contains("L Toe0") && targetObject.footLeft == null)targetObject.footLeft = tr;
            }
            targetObject.FindBones();
        }
        serializedObject.ApplyModifiedProperties();
	}
	
	public override void OnInspectorGUI () 
    {
        if (bodyTexture == null) return;
	    bool showBones = targetObject.showBones;
	    targetObject.showBones = GUILayout.Toggle(targetObject.showBones, new GUIContent("Show Bones", "Show bipeds bones"));
        if (showBones != targetObject.showBones)
        {
            if (targetObject.showBones) targetObject.FindBones();
            else
            {
                foreach (CsBipedBone bb in targetObject.bones) DestroyImmediate(bb);
                targetObject.bones = null;
            }
            EditorUtility.SetDirty(targetObject.gameObject);
        }

        Rect lRect = GUILayoutUtility.GetLastRect();
        AntaresGameObjectCustom.Routines.DrawHeader("ANTARES BIPED BODY");
        float middle = lRect.width / 2, half = 170f;
        Vector2 leftUp = new Vector2(middle - half, lRect.y + 64);
        GUI.Box(new Rect(leftUp.x, leftUp.y, w, h), bodyTexture);

        EditorGUILayout.BeginHorizontal();
        float yHand = leftUp.y + 107;
        if (targetObject.handRight != null && GUI.Button(new Rect(leftUp.x + 45, yHand, 15, 15), new GUIContent(":", "Right Hand"))) EditorGUIUtility.PingObject(targetObject.handRight.gameObject);
        if (targetObject.handLeft != null && GUI.Button(new Rect(leftUp.x + 279, yHand, 15, 15), new GUIContent(":", "Left Hand"))) EditorGUIUtility.PingObject(targetObject.handLeft.gameObject);
        float xFoot = (lRect.y + h - 10) + 36;
        if (targetObject.footRight != null && GUI.Button(new Rect(middle - 80, xFoot, 15, 15), new GUIContent(".", "Right Foot"))) EditorGUIUtility.PingObject(targetObject.footRight.gameObject);
        if (targetObject.footLeft != null && GUI.Button(new Rect(middle + 74, xFoot, 15, 15), new GUIContent(".", "Left Foot"))) EditorGUIUtility.PingObject(targetObject.footLeft.gameObject);
        EditorGUILayout.EndHorizontal();

        if (targetObject.head != null && GUI.Button(new Rect(middle - 7.5f, lRect.y + 121, 15, 15), new GUIContent("+", "Head"))) EditorGUIUtility.PingObject(targetObject.head.gameObject);
        GUILayout.Space(h + 20);
	    AntaresGameObjectCustom.Routines.DrawFooter();
    }

    [MenuItem(("Antares/Biped/Add Biped finder module"))]
    public static void AddBipedFinder()
    {
        if (Selection.activeGameObject.GetComponent<CsBiped>() != null) return;
        Selection.activeGameObject.AddComponent<CsBiped>();
        EditorUtility.SetDirty(Selection.activeGameObject);
    }

    [MenuItem(("Antares/Biped/Add Biped finder module"), true)]
    public static bool AddBipedFinderValidate()
    {
        return Selection.activeGameObject && Selection.activeGameObject.GetComponent<CsBiped>() == null;
    }

}
