/****************************************************
    文件：Preference.cs
    作者：陶长春
    邮箱：376248129@qq.com
    日期：2025年2月11日 6:32:03
    UnityVersion: 2021.3.13f1
    功能：偏好设置
*****************************************************/
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
namespace TFramework
{
    public class Preference : EditorWindow
    {
        [MenuItem("TFramework/偏好设置")]
        public static void Open()
        {
            Preference win = GetWindow<Preference>();
            win.minSize = new Vector2(200,300);
            win.titleContent.image = EditorGUIUtility.IconContent("d__Popup@2x").image;
            win.titleContent.text = "偏好设置";
        }

        const string DATAPATH = "Assets/TFramework/Editor/Data/PreferenceData.asset";
        PreferenceData preferenceData;

        private void OnEnable()
        {
            preferenceData = AssetDatabase.LoadAssetAtPath<PreferenceData>(DATAPATH);
            if(preferenceData == null )
            {
                preferenceData = CreateInstance<PreferenceData>();
                AssetDatabase.CreateAsset(preferenceData, DATAPATH);
                AssetDatabase.SaveAssets();
            }
        }

        private void OnGUI()
        {
            if (preferenceData == null) return;
            using(new EditorGUILayout.HorizontalScope())
            {
                GUILayout.Label("自动保存");
                preferenceData.m_isAutoSave = EditorGUILayout.Toggle(preferenceData.m_isAutoSave);
            }

            using(new EditorGUILayout.HorizontalScope())
            {
                GUI.enabled = preferenceData.m_isAutoSave;
                GUILayout.Label("保存间隔(分钟)");
                preferenceData.m_autoSaveTime = EditorGUILayout.FloatField(preferenceData.m_autoSaveTime);
                GUI.enabled = true;
            }
            using (new EditorGUILayout.HorizontalScope())
            {
                GUILayout.Label("始终从指定场景开始：");
                preferenceData.m_isAlwaysFormAssignScene = EditorGUILayout.Toggle(preferenceData.m_isAlwaysFormAssignScene);
            }
            using (new EditorGUILayout.HorizontalScope())
            {
                GUI.enabled = preferenceData.m_isAlwaysFormAssignScene;
                GUILayout.Label("指定场景：");
                Object scene = AssetDatabase.LoadAssetAtPath<Object>(preferenceData.m_assignScene);
                using (new EditorGUILayout.VerticalScope())
                {
                    scene = EditorGUILayout.ObjectField(scene, typeof(Object), false);
                    if(preferenceData.m_isAlwaysFormAssignScene)
                    {
                        if (scene == null)
                            EditorGUILayout.HelpBox("请指定一个场景资源！", MessageType.Info);
                        else if (!AssetDatabase.GetAssetPath(scene).EndsWith(".unity"))
                            EditorGUILayout.HelpBox("选择的资源不是场景资源！", MessageType.Error);
                        else
                        {
                            preferenceData.m_assignScene = AssetDatabase.GetAssetPath(scene);
                        }
                    }
                }
                GUI.enabled = true;
            }
        }

        private void OnDisable()
        {
            SaveData();
        }

        private void SaveData()
        {
            if (preferenceData == null) return;
            EditorUtility.SetDirty(preferenceData);
            AssetDatabase.SaveAssets();
        }
    }
}