using LitJson;
using System;
using UnityEditor;
using UnityEngine;

namespace WS
{ 
    public class JumpToOperateEditor : IOperateEditor
    {
        public string Name => "跳转位置（同场景）";

        public Type Component => typeof(JumpToComponent);

        /// <summary> 起点 </summary>
        private GameObject startPoint;
        /// <summary> 终点点 </summary>
        private GameObject endPoint;

        private JumpToData Data;
        public void Dispose()
        {

        }

        public void Init(string json)
        {
            Data = string.IsNullOrEmpty(json) ? new JumpToData() : JsonMapper.ToObject<JumpToData>(json);
            if (!string.IsNullOrEmpty(Data.startName))
            {
                startPoint = GameObject.Find(Data.startName);
                if (startPoint == null)
                {
                    Debug.Log($"在场景中未找到 {Data.startName} 物体,请检查!");
                    return;
                }
            }
            if (!string.IsNullOrEmpty(Data.endName))
            {
                endPoint = GameObject.Find(Data.endName);
                if (endPoint == null)
                {
                    Debug.Log($"在场景中未找到 {Data.endName} 物体,请检查!");
                    return;
                }
            }
        }

        public void OnGUI()
        {

            EditorGUILayout.BeginHorizontal();
            GUILayout.Label("起点:", GUILayout.Width(55));
            startPoint = (GameObject)EditorGUILayout.ObjectField(startPoint, typeof(GameObject), true);
            if (startPoint != null)
            {
                Data.startPos = startPoint.transform.position;
                Data.startRot = startPoint.transform.localRotation.eulerAngles;
            }
            GUILayout.Label("终点:", GUILayout.Width(55));
            endPoint = (GameObject)EditorGUILayout.ObjectField(endPoint, typeof(GameObject), true);
            if (endPoint != null)
            {
                Data.endPos = endPoint.transform.position;
                Data.endRot = endPoint.transform.localRotation.eulerAngles;
            }
            EditorGUILayout.EndHorizontal();
            Data.audioName = EditorGUILayout.TextField("音频:", Data.audioName);

        }

        public string ToData()
        {
            if (Data.audioName != null)
            {
                Data.audioName = Data.audioName.Replace(" ", "");
            }

            Data.startName = startPoint != null ? EditorHelper.GetTransformParentPath(startPoint.transform) : "";
            Data.endName = endPoint != null ? EditorHelper.GetTransformParentPath(endPoint.transform) : "";
            return JsonMapper.ToJson(Data);
        }
    }
}
