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

///<summary>项目默认配置以及说明</summary>
[InitializeOnLoad]
public class VFrameworkSettingEditor : EditorWindow
{
    [MenuItem("Tools/Framework/项目注意事项")]
    public static void ShowWindow()
    {
        VFrameworkSettingEditor window = GetWindow<VFrameworkSettingEditor>("项目注意事项");
        window.autoRepaintOnSceneChange = true;
        window.minSize = window.maxSize = new Vector2(600, 400);
    }

    static VFrameworkSettingEditor()
    {
        EditorApplication.update += Update;
    }

    private static void Update()
    {
        AssetDatabase.Refresh();
        Setting = AssetDatabase.LoadAssetAtPath<WS_ProjectSetting>("Assets/Editor/Framework/Other/WS_ProjectSetting.asset");
        if (Setting == null)
        {
            Setting = ScriptableObject.CreateInstance<WS_ProjectSetting>();
            AssetDatabase.CreateAsset(Setting, "Assets/Editor/Framework/Other/WS_ProjectSetting.asset");
            AssetDatabase.Refresh();
        }
        if (!Setting.IsInit)
        {
            #region -- 场景设置 --
            EditorBuildSettingsScene[] scenes = new EditorBuildSettingsScene[2];
            scenes[0] = new EditorBuildSettingsScene("Assets/Scenes/Init.unity", true);
            scenes[1] = new EditorBuildSettingsScene("Assets/Scenes/Loading.unity", true);
            EditorBuildSettings.scenes = scenes;
            #endregion

            #region -- 文件夹设置 --
            for (int i = 0; i < Files.Length; i++)
            {
                if (!Directory.Exists(Files[i]))
                    Directory.CreateDirectory(Files[i]);
            }
            #endregion

            #region -- 渲染设置 --
            if (PlayerSettings.colorSpace != ColorSpace.Linear)
                PlayerSettings.colorSpace = ColorSpace.Linear;
            #endregion

            #region -- Package包设置 --
            Client.Add("com.unity.2d.sprite");
            #endregion

            #region -- 宏设置 -- 
#if UNITY_ANDROID
        Macro = PlayerSettings.GetScriptingDefineSymbolsForGroup(BuildTargetGroup.Android);
#else
            Macro = PlayerSettings.GetScriptingDefineSymbolsForGroup(BuildTargetGroup.Standalone);
#endif
            List<string> macros = Macro.Split(';').ToList();
            if (!macros.Contains("DEBUG_MODEL"))
            {
                macros.Insert(0, "DEBUG_MODEL");
                for (int i = 0; i < macros.Count; i++)
                {
                    Macro += $"{macros[i]};";
                }
#if UNITY_ANDROID
            PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Android, Macro);
#else
                PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Standalone, Macro);
#endif
            }
            #endregion

            #region -- API设置 --
            ApiCompatibilityLevel api;
#if UNITY_ANDROID
        api = PlayerSettings.GetApiCompatibilityLevel(BuildTargetGroup.Android);
#else
            api = PlayerSettings.GetApiCompatibilityLevel(BuildTargetGroup.Standalone);
#endif
            if (api != ApiCompatibilityLevel.NET_4_6)
            {
#if UNITY_ANDROID
            PlayerSettings.SetApiCompatibilityLevel(BuildTargetGroup.Android, ApiCompatibilityLevel.NET_4_6);
#else
                PlayerSettings.SetApiCompatibilityLevel(BuildTargetGroup.Standalone, ApiCompatibilityLevel.NET_4_6);
#endif
            }
            #endregion

            Setting.IsInit = true;
            EditorUtility.SetDirty(Setting);
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
        }
        if (!PlayerPrefs.HasKey("IsLookAtThat"))
            ShowWindow();
        EditorApplication.update -= Update;
    }

    ///<summary>文件夹</summary>
    private static string[] Files = new[]
    {
        "Assets/GameResources/Model/Characters/",
        "Assets/GameResources/Model/Environment/",
        "Assets/GameResources/Prefabs/Characters/",
        "Assets/GameResources/Prefabs/Environment/",
        "Assets/GameResources/Prefabs/Item/",
        "Assets/GameResources/Shaders/",
        "Assets/Scripts/Data/Project/",
        "Assets/Resources/Config/Process/",
        "Assets/Log"
    };
    ///<summary>标题</summary>
    private GUIStyle titleText;
    ///<summary>内容</summary>
    private GUIStyle contentText;
    ///<summary>宏</summary>
    private static string Macro;
    ///<summary>设置</summary>
    private static WS_ProjectSetting Setting;

    private void OnEnable()
    {
        titleText = new GUIStyle();
        titleText.normal.textColor = Color.yellow;
        titleText.alignment = TextAnchor.UpperCenter;
        titleText.fontSize = 32;

        contentText = new GUIStyle();
        contentText.normal.textColor = Color.white;
        contentText.fontSize = 16;
    }

    private void OnGUI()
    {
        EditorGUILayout.BeginHorizontal();
        GUILayout.Label("项目注意事项", titleText);
        EditorGUILayout.EndHorizontal();

        GUILayout.Label("");

        GUILayout.Label("1.项目会在打包时代码混淆，<color=#FF0000>请谨用反射!</color>,添加<color=#00DFFF>SkipRename属性</color>的类会忽略混淆;", contentText);
        GUILayout.Label("2.所有需要Json序列化的结构(类)，放到<color=#00DFFF>Assets/Scripts/Data</color>文件夹下避免被混淆;", contentText);
        GUILayout.Label("3.音频文件需要放在<color=#00DFFF>Assets/GameResources/Audio</color>文件夹下，然后在编辑器\n   <color=#00DFFF>Tools/Framework/自动生成语音预制件</color>生成预制件；", contentText);
        GUILayout.Label("4.UI预制件需要放在<color=#00DFFF>Assets/Resources/UI</color>文件夹下在根据UI显示类型分类放入子目\n   录，然后在编辑器<color=#00DFFF>Tools/Framework/自动对应UI路径</color>生成路径预设；", contentText);
        GUILayout.Label("5.项目启动需要在<color=#00DFFF>Init场景</color>中的<color=#00DFFF>Init物体</color>挂载一个<color=#00DFFF>继承Init类</color>的派生类，在派生类的\n   <color=#00DFFF>Initialize方法</color>为启动入口，派生类<color=#00DFFF>Update</color>方法不能使用替换为<color=#00DFFF>OnUpdate</color>方法；", contentText);

        GUILayout.Label("");

        EditorGUILayout.BeginHorizontal();
        GUILayout.Label("");
        if (GUILayout.Button("确定", GUILayout.Width(100)))
        {
            PlayerPrefs.SetInt("IsLookAtThat", 1);
            Close();
        }
        EditorGUILayout.EndHorizontal();
    }
}