using LitJson;
using System;
using UnityEditor;
using UnityEngine;
using UnityEngine.SceneManagement;
using WS;

public class LoadSceneOperateEditor : IOperateEditor
{
    public string Name => "加载场景";
    /// <summary> 传送点 </summary>
    private GameObject transferPoint;
    public Type Component => typeof(LoadSceneComponent);
    private LoadSceneData Data;

    public void Dispose()
    {
        
    }

    public void Init(string json)
    {
        Data = string.IsNullOrEmpty(json) ? new LoadSceneData() : JsonMapper.ToObject<LoadSceneData>(json);
        if (!string.IsNullOrEmpty(Data.transferPoint))
        {
            transferPoint = GameObject.Find(Data.transferPoint);

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

                return;
            }

        }
    }

    public void OnGUI()
    {      
        EditorGUILayout.BeginHorizontal();
        GUILayout.Label("起始场景名称:", GUILayout.Width(85));
        Data.startSceneName = (SceneName)EditorGUILayout.EnumPopup(Data.startSceneName);
        GUILayout.Label("目标场景名称:", GUILayout.Width(85));
        Data.endSceneName = (SceneName)EditorGUILayout.EnumPopup(Data.endSceneName);
        EditorGUILayout.EndHorizontal();

        EditorGUILayout.BeginHorizontal();
        GUILayout.Label("音频:", GUILayout.Width(55));
        Data.AudioName = EditorGUILayout.TextField(Data.AudioName);
        GUILayout.Label("加载后的位置点:", GUILayout.Width(95));
        transferPoint = (GameObject)EditorGUILayout.ObjectField(transferPoint, typeof(GameObject), true);
        if(transferPoint != null)
        {
            Data.pos = transferPoint.transform.position;
            Data.rot = transferPoint.transform.localRotation.eulerAngles;
        }

        EditorGUILayout.EndHorizontal();
    }

    public string ToData()
    {
        if (Data.AudioName != null)
        {
            Data.AudioName = Data.AudioName.Replace(" ", "");
        }
        Data.transferPoint = transferPoint != null ? EditorHelper.GetTransformParentPath(transferPoint.transform) : "";
        return JsonMapper.ToJson(Data);
    }
}
