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

public class InteractOperateEditor : IOperateEditor
{
    public string Name => "交互";

    public Type Component => typeof(InteractComponent);

    private InteractData 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 GameObject AnswerObj;

    ///<summary>相应物体所有动画片段名称</summary>
    private string[] AnswerAnimNames;
    ///<summary>响应物体动画的索引</summary>
    private int AnswerAnimIndex;
    public void Init(string json)
    {


        Data = string.IsNullOrEmpty(json) ? new InteractData() : JsonMapper.ToObject<InteractData>(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;
            }
        }


        if (!string.IsNullOrEmpty(Data.AnswerName))
        {
            AnswerObj = GameObject.Find(Data.AnswerName);
            if (AnswerObj == null)
            {
                Debug.Log($"在场景中未找到 {Data.AnswerName} 物体,请检查!");
                return;
            }
            if (!string.IsNullOrEmpty(Data.AnswerAnimName))
            {
                Animator animator = AnswerObj.transform.GetComponent<Animator>();
                if (animator != null)
                {
                    List<string> list = animator.runtimeAnimatorController.animationClips.Select(a => a.name).ToList();
                    AnswerAnimIndex = list.FindIndex(s => s == Data.AnswerAnimName);
                    if (AnswerAnimIndex < 0)
                        AnswerAnimIndex = 0;
                    AnswerAnimNames = list.ToArray();
                }
                IsDependence = true;
            }
        }
    }

    public void OnGUI()
    {
        EditorGUILayout.BeginHorizontal();
        GUILayout.Label("关联物体:", GUILayout.Width(55));
        IsDependence = EditorGUILayout.Toggle(IsDependence);
        if (IsDependence)
        {
            GUILayout.Label("同时播放动画:", GUILayout.Width(85));
            Data.IsSimulAni = EditorGUILayout.Toggle(Data.IsSimulAni);
        }
        GUILayout.Label("交互前显示:", GUILayout.Width(95));
        Data.BeforeEnable = EditorGUILayout.Toggle(Data.BeforeEnable);
        //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(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();

        EditorGUILayout.BeginHorizontal();
        if (IsDependence)
        {
            GUILayout.Label("关联物体:", GUILayout.Width(55));
            //for (int i =0;i<AnswerObjList.Count;i++)
            //{
            //    AnswerObjList[i] = (GameObject)EditorGUILayout.ObjectField(AnswerObjList[i], typeof(GameObject), true);
            //    if (AnswerObjList[i] != null)
            //    {
            //        if (AnswerAnimNames == null)
            //        {
            //            Animator animator = AnswerObjList[i].transform.GetComponent<Animator>();
            //            if (animator != null)
            //                AnswerAnimNames = animator.runtimeAnimatorController.animationClips.Select(a => a.name).ToArray();
            //        }
            //        if (AnswerAnimNames != null)
            //        {
            //            GUILayout.Label("动画:", GUILayout.Width(35));
            //            AnswerAnimIndex = EditorGUILayout.Popup(AnswerAnimIndex, AnswerAnimNames);
            //        }
            //    }
            //}
            AnswerObj = (GameObject)EditorGUILayout.ObjectField(AnswerObj, typeof(GameObject), true);
            if (AnswerObj != null)
            {
                if (AnswerAnimNames == null)
                {
                    Animator animator = AnswerObj.transform.GetComponent<Animator>();
                    if (animator != null)
                        AnswerAnimNames = animator.runtimeAnimatorController.animationClips.Select(a => a.name).ToArray();
                }
                if (AnswerAnimNames != null)
                {
                    GUILayout.Label("动画:", GUILayout.Width(35));
                    AnswerAnimIndex = EditorGUILayout.Popup(AnswerAnimIndex, AnswerAnimNames);
                }
            }
        }
        else
        {
            AnswerObj = null;
        }
        EditorGUILayout.EndHorizontal();


    }

    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.AnswerName = AnswerObj != null ? EditorHelper.GetTransformParentPath(AnswerObj.transform) : "";
        if (AnswerAnimNames != null)
            Data.AnswerAnimName = IsDependence ? AnswerAnimNames[AnswerAnimIndex] : "";
        return JsonMapper.ToJson(Data);
    }

    public void Dispose()
    {
    }
}
