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

public class AnswerObjs
{
    ///<summary>响应物体</summary>
    public GameObject AnswerObj;
    ///<summary>响应物体所有动画片段名称</summary>
    public string[] AnswerAnimNames;
    ///<summary>响应物体动画的索引</summary>
    public int AnswerAnimIndex;
}
public class InteractAnimsOperateEditor : IOperateEditor
{
    public string Name => "交互(多个关联物体)";

    public Type Component => typeof(InteractAnimsComponent);

    private InteractAnimsData Data;

    ///<summary>交互模式</summary>
    private string[] Models = new string[] { "交互", "交互+动画" };
    ///<summary>交互模式索引</summary>
    private int ModelIndex;
    ///<summary>交互物体</summary>
    private GameObject InteractObj;
    ///<summary>所有动画片段名称</summary>
    private string[] AnimationNames;
    ///<summary>选择播放动画的索引</summary>
    private int AnimationIndex;

    ///<summary>是否有关联物体</summary>
    private bool IsDependence;
    ///<summary>响应物体列表</summary>
    private List<AnswerObjs> AnswerObjList = new List<AnswerObjs>();
    
    ///<summary> 折叠开关 </summary>
    private bool foldout;
    public void Init(string json)
    {


        Data = string.IsNullOrEmpty(json) ? new InteractAnimsData() : JsonMapper.ToObject<InteractAnimsData>(json);
        if (!string.IsNullOrEmpty(Data.InteractName))
        {
            InteractObj = GameObject.Find(Data.InteractName);

            if (InteractObj == null)
            {
                Debug.Log($"在场景中未找到 {Data.InteractName} 物体,请检查!");

                return;
            }
            if (!string.IsNullOrEmpty(Data.AnimationName))
            {
                Animator animator = InteractObj.transform.GetComponent<Animator>();
                if (animator != null)
                {
                    List<string> list = animator.runtimeAnimatorController.animationClips.Select(a => a.name).ToList();
                    AnimationIndex = list.FindIndex(s => s == Data.AnimationName);
                    if (AnimationIndex < 0)
                        AnimationIndex = 0;
                    AnimationNames = list.ToArray();
                }
                ModelIndex = 1;
            }
        }
        for (int i = 0;i<Data.AnswerObjDataList.Count;i++)
        {
            if (!string.IsNullOrEmpty(Data.AnswerObjDataList[i].AnswerName))
            {
                AnswerObjs answerObjs = new AnswerObjs();
                answerObjs.AnswerObj = GameObject.Find(Data.AnswerObjDataList[i].AnswerName);
                if (answerObjs.AnswerObj == null)
                {
                    Debug.Log($"在场景中未找到 {Data.AnswerObjDataList[i].AnswerName} 物体,请检查!");
                    return;
                }
                if (!string.IsNullOrEmpty(Data.AnswerObjDataList[i].AnswerAnimName))
                {
                    Animator animator = answerObjs.AnswerObj.transform.GetComponent<Animator>();
                    if (animator != null)
                    {
                        List<string> list = animator.runtimeAnimatorController.animationClips.Select(a => a.name).ToList();
                        answerObjs.AnswerAnimIndex = list.FindIndex(s => s == Data.AnswerObjDataList[i].AnswerAnimName);
                        if (answerObjs.AnswerAnimIndex < 0)
                            answerObjs.AnswerAnimIndex = 0;
                        answerObjs.AnswerAnimNames = list.ToArray();
                    }
                    IsDependence = true;
                }
                AnswerObjList.Add(answerObjs);
            }
        }
    }

    public void OnGUI()
    {
        EditorGUILayout.BeginHorizontal();
        GUILayout.Label("关联物体:", GUILayout.Width(55));
        IsDependence = EditorGUILayout.Toggle(IsDependence);

        GUILayout.Label("交互前显示:", GUILayout.Width(95));
        Data.BeforeEnable = EditorGUILayout.Toggle(Data.BeforeEnable);

        GUILayout.Label("是否有内部交互物体:", GUILayout.Width(115));
        Data.InteractInside = EditorGUILayout.Toggle(Data.InteractInside);
        GUILayout.Label("同时播放动画:", GUILayout.Width(85));
        Data.IsSimulAni = EditorGUILayout.Toggle(Data.IsSimulAni);
        GUILayout.Label("模式:", GUILayout.Width(35));
        ModelIndex = EditorGUILayout.Popup(ModelIndex, Models, GUILayout.Width(300));

        EditorGUILayout.EndHorizontal();

        EditorGUILayout.BeginHorizontal();
        GUILayout.Label("音频:", GUILayout.Width(35));
        Data.AudioName = EditorGUILayout.TextField(Data.AudioName);

        GUILayout.Label("音效:", GUILayout.Width(35));
        Data.AudioEffect = EditorGUILayout.TextField(Data.AudioEffect);

        GUILayout.Label("交互物体:", GUILayout.Width(55));
        InteractObj = (GameObject)EditorGUILayout.ObjectField(InteractObj, typeof(GameObject), true);
        if (ModelIndex == 1)
        {
            if (InteractObj != null)
            {
                if (AnimationNames == null)
                {
                    Animator animator = InteractObj.transform.GetComponent<Animator>();
                    if (animator != null)
                        AnimationNames = animator.runtimeAnimatorController.animationClips.Select(a => a.name).ToArray();
                }
                if (AnimationNames != null)
                {
                    GUILayout.Label("动画:", GUILayout.Width(35));
                    AnimationIndex = EditorGUILayout.Popup(AnimationIndex, AnimationNames);
                }
            }
        }
        EditorGUILayout.EndHorizontal();

        if (IsDependence)
        {
            foldout = EditorGUILayout.BeginFoldoutHeaderGroup(foldout, "关联物体列表:");
            if (foldout)
            {
                for (int i = 0; i < AnswerObjList.Count; i++)
                {
                    EditorGUILayout.BeginHorizontal();
                    AnswerObjList[i].AnswerObj = (GameObject)EditorGUILayout.ObjectField(AnswerObjList[i].AnswerObj, typeof(GameObject), true);

                    if (AnswerObjList[i].AnswerObj != null)
                    {
                        if (AnswerObjList[i].AnswerAnimNames == null)
                        {
                            Animator animator = AnswerObjList[i].AnswerObj.transform.GetComponent<Animator>();
                            if (animator != null)
                                AnswerObjList[i].AnswerAnimNames = animator.runtimeAnimatorController.animationClips.Select(a => a.name).ToArray();
                        }
                        if (AnswerObjList[i].AnswerAnimNames != null)
                        {
                            GUILayout.Label("动画:", GUILayout.Width(35));
                            AnswerObjList[i].AnswerAnimIndex = EditorGUILayout.Popup(AnswerObjList[i].AnswerAnimIndex, AnswerObjList[i].AnswerAnimNames);
                        }
                    }
                    EditorGUILayout.EndHorizontal();
                }

                EditorGUILayout.BeginHorizontal();
                if (GUILayout.Button("添加"))
                {
                    AnswerObjList.Add(new AnswerObjs());
                }
                if (GUILayout.Button("移除"))
                {
                    if (AnswerObjList.Count > 0)
                    {
                        AnswerObjList.Remove(AnswerObjList[AnswerObjList.Count - 1]);
                    }
                }
                EditorGUILayout.EndHorizontal();
            }
            EditorGUILayout.EndFoldoutHeaderGroup();
        }

    }

    public string ToData()
    {
        if (Data.AudioName != null)
        {
            Data.AudioName = Data.AudioName.Replace(" ", "");
        }
        Data.InteractName = InteractObj != null ? EditorHelper.GetTransformParentPath(InteractObj.transform) : "";

        if (AnimationNames != null)
        {
            Data.AnimationName = ModelIndex == 1 ? AnimationNames[AnimationIndex] : "";
        }

        Data.AnswerObjDataList.Clear();
        for (int i = 0; i < AnswerObjList.Count; i++)
        {
            AnswerObjData answerObj = new AnswerObjData();
            answerObj.AnswerName = AnswerObjList[i].AnswerObj != null ? EditorHelper.GetTransformParentPath(AnswerObjList[i].AnswerObj.transform) : "";
            if (AnswerObjList[i].AnswerAnimNames != null && IsDependence)
            {
                answerObj.AnswerAnimName = AnswerObjList[i].AnswerAnimNames[AnswerObjList[i].AnswerAnimIndex];
            }
            Data.AnswerObjDataList.Add(answerObj);
        }
        return JsonMapper.ToJson(Data);
    }

    public void Dispose()
    {
    }
}
