using UnityEngine;
using UnityEditor;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.IO;


/// <summary>
/// Custom Editor for editing vertices and exporting the mesh.
/// </summary>
[CustomEditor(typeof(CameraTool))]
public class MapPosObjEditor : Editor
{
    //navmesh object reference
    private CameraTool script;

    SerializedProperty Far;
    SerializedProperty Height;
    SerializedProperty RotateX;
    SerializedProperty RotateY;
    SerializedProperty RotateZ;
    SerializedProperty ModelID;

    void OnEnable()
    {
        script = (CameraTool)target;
        Far = serializedObject.FindProperty("Far");
        Height = serializedObject.FindProperty("Height");
        RotateX = serializedObject.FindProperty("RotateX");
        RotateY = serializedObject.FindProperty("RotateY");
        RotateZ = serializedObject.FindProperty("RotateZ");
        ModelID = serializedObject.FindProperty("ModelID");
    }


    /// <summary>
    /// Custom inspector override for buttons.
    /// </summary>
    public override void OnInspectorGUI()
    {
        //DrawDefaultInspector();
        //EditorGUILayout.Space();
        script.ModelID = EditorGUILayout.IntField(new GUIContent("ModelID"), script.ModelID);
        script.Far = EditorGUILayout.Slider(new GUIContent("Far"), script.Far, 0.01f, 9);
        script.Height = EditorGUILayout.Slider(new GUIContent("Height"), script.Height, -5, 5);
        script.RotateX = EditorGUILayout.Slider(new GUIContent("RotateX"), script.RotateX, 0, 360);
        script.RotateY = EditorGUILayout.Slider(new GUIContent("RotateY"), script.RotateY, 0, 360);
        script.RotateZ = EditorGUILayout.Slider(new GUIContent("RotateZ"), script.RotateZ, 0, 360);
        EditorGUILayout.Space();
        script.UpdateModel();

        if (GUILayout.Button("ShowModel"))
        {

            if (CharModelUIShowTable._TabItems.Count == 0)
            {
                CharModelUIShowTable.ReadTable();
            }

            if (!CharModelUIShowTable._TabItems.ContainsKey(script.ModelID))
            {
                CharModelUIShowTable.ReadTable();
                if (!CharModelUIShowTable._TabItems.ContainsKey(script.ModelID))
                {
                    Debug.LogError("没有此ID的CharModel");
                    return;
                }
            }
            var modelTab = CharModelUIShowTable._TabItems[script.ModelID];
            string[] assetPaths = AssetDatabase.GetAssetPathsFromAssetBundleAndAssetName(LoadAssetBundle.BUNDLE_PATH_MODEL + modelTab.ModelPath.ToLower() + ".common", modelTab.ModelPath);
            if (assetPaths.Length == 0)
            {
                Debug.LogError("没有此模型:" + modelTab.ModelPath);
                return;
            }
            GameObject resObj = AssetDatabase.LoadAssetAtPath(assetPaths[0], typeof(GameObject)) as GameObject;
            GameObject modelObj = GameObject.Instantiate(resObj) as GameObject;

            var trans = script.transform.Find("GameObject");
            for (int i = 0; i < trans.childCount; ++i)
            {
                GameObject.DestroyImmediate(trans.GetChild(i).gameObject);
            }
            modelObj.transform.SetParent(trans);
            modelObj.transform.localPosition = Vector3.zero;
            modelObj.transform.localRotation = Quaternion.Euler(Vector3.zero);

            if (script.gameObject.name.Contains("UIObjCamera"))
            {
                script.Far = modelTab.BodyPosition.z;
                script.Height = modelTab.BodyPosition.y;
                script.RotateY = modelTab.BodyRotation.y;
                script.RotateX = modelTab.BodyRotation.x;
                script.RotateZ = modelTab.BodyRotation.z;
            }
            else if (script.gameObject.name.Contains("UIHalfCamera"))
            {
                script.Far = modelTab.HalfPosition.z;
                script.Height = modelTab.HalfPosition.y;
                script.RotateY = modelTab.HalfRotation.y;
                script.RotateX = modelTab.BodyRotation.x;
                script.RotateZ = modelTab.BodyRotation.z;
            }
        }

        if (GUILayout.Button("SaveModel"))
        {
            if (CharModelUIShowTable._TabItems.Count == 0)
            {
                CharModelUIShowTable.ReadTable();
            }
            var modelTab = CharModelUIShowTable._TabItems[script.ModelID];

            if (script.gameObject.name.Contains("UIObjCamera"))
            {
                modelTab.BodyPosition.z = script.Far;
                modelTab.BodyPosition.y = script.Height;
                modelTab.BodyRotation.y = script.RotateY;
                modelTab.BodyRotation.x = script.RotateX;
                modelTab.BodyRotation.z = script.RotateZ;
            }
            else if (script.gameObject.name.Contains("UIHalfCamera"))
            {
                modelTab.HalfPosition.z = script.Far;
                modelTab.HalfPosition.y = script.Height;
                modelTab.HalfRotation.y = script.RotateY;
                modelTab.BodyRotation.x = script.RotateX;
                modelTab.BodyRotation.z = script.RotateZ;
            }

            CharModelUIShowTable.WriteTable();
        }

        //if (GUILayout.Button("FitEnemies"))
        //{
        //}

    }

    public void LoadModelPathFinish(string modelName, GameObject resObj, Hashtable hash)
    {
        

    }

}