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

///<summary>项目数据配置</summary>
public class ProjectDataEditor : EditorWindow
{
    [MenuItem("Tools/Framework/项目数据配置")]
    public static void ShowVoice()
    {
        ProjectDataEditor window = EditorWindow.GetWindow<ProjectDataEditor>("项目数据配置");
        window.Show();
    }

    ///<summary>宏元素</summary>
    private class MacroItem
    {
        ///<summary>宏名称</summary>
        public string Name;

        ///<summary>窗口上显示的名称</summary>
        public string DisplayName;

        ///<summary>是否发布</summary>
        public bool isRelease;

        ///<summary>默认宏</summary>
        public bool isDefault;
    }

    ///<summary>所有宏</summary>
    private List<MacroItem> macroitems = new List<MacroItem>();

    ///<summary>Macro</summary>
    private string Macro = null;

    private void OnEnable()
    {
#if UNITY_ANDROID
        Macro = PlayerSettings.GetScriptingDefineSymbolsForGroup(BuildTargetGroup.Android);
#else
        Macro = PlayerSettings.GetScriptingDefineSymbolsForGroup(BuildTargetGroup.Standalone);
#endif
        List<string> macros = Macro.Split(';').ToList();
        macroitems.Clear();
        macroitems.Add(
            new MacroItem()
            {
                Name = "DEBUG_MODEL",
                DisplayName = "调试模式",
                isRelease = false
            }
        );
        macroitems.Add(
            new MacroItem()
            {
                Name = "DEBUG_LOG",
                DisplayName = "记录日志",
                isRelease = false
            }
        );
        macroitems.Add(
            new MacroItem()
            {
                Name = "MODE_TEST",
                DisplayName = "测试模式",
                isRelease = false
            }
        );
        macroitems.Add(
            new MacroItem()
            {
                Name = "COLLECT_DATA",
                DisplayName = "收集数据",
                isRelease = false
            }
        );
        //macroitems.Add(
        //    new MacroItem()
        //    {
        //        Name = "PLATFORM_STEAMVR",
        //        DisplayName = "SteamVR平台",
        //        isRelease = false
        //    }
        //);
        macroitems.Add(
            new MacroItem()
            {
                Name = "PLATFORM_PC",
                DisplayName = "PC平台",
                isRelease = false
            }
        );
        macroitems.Add(
            new MacroItem()
            {
                Name = "PLATFORM_PHONE",
                DisplayName = "手机平台",
                isRelease = false
            }
        );
        macroitems.Add(
            new MacroItem()
            {
                Name = "PLATFORM_PICO",
                DisplayName = "Pico平台",
                isRelease = false
            }
        );

        for (int i = 0; i < macros.Count; i++)
        {
            if (!macroitems.Exists(s => s.Name == macros[i]))
            {
                if (!string.IsNullOrEmpty(macros[i]))
                    macroitems.Add(
                        new MacroItem()
                        {
                            Name = macros[i],
                            DisplayName = "其它",
                            isRelease = false,
                            isDefault = true
                        }
                    );
            }
        }

        for (int i = 0; i < macroitems.Count; i++)
        {
            if (macros.Exists(s => s == macroitems[i].Name))
                macroitems[i].isRelease = true;
        }
    }

    private void OnGUI()
    {
        EditorGUILayout.BeginHorizontal("box");
        GUILayout.Label("");
        GUILayout.Label("环境配置", GUILayout.Width(55));
        GUILayout.Label("");
        EditorGUILayout.EndHorizontal();
        for (int i = 0; i < macroitems.Count; i++)
        {
            EditorGUILayout.BeginHorizontal();
            GUILayout.Label("名称:", GUILayout.Width(30));
            GUILayout.Label(macroitems[i].DisplayName, GUILayout.Width(100));
            GUILayout.Label("宏  :", GUILayout.Width(30));
            EditorGUILayout.TextField(macroitems[i].Name);
            GUILayout.Label("是否开启:", GUILayout.Width(60));
            if (macroitems[i].isDefault)
                EditorGUILayout.Toggle(macroitems[i].isRelease, GUILayout.Width(15));
            else
                macroitems[i].isRelease = EditorGUILayout.Toggle(
                    macroitems[i].isRelease,
                    GUILayout.Width(15)
                );
            EditorGUILayout.EndHorizontal();
        }

        EditorGUILayout.BeginHorizontal();
        GUILayout.Label("");
        if (GUILayout.Button("保存", GUILayout.Width(100)))
        {
            Macro = string.Empty;
            for (int i = 0; i < macroitems.Count; i++)
            {
                if (macroitems[i].isRelease)
                {
                    Macro += $"{macroitems[i].Name};";
                }
            }
#if UNITY_ANDROID
            PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Android, Macro);
#else
            PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Standalone, Macro);
#endif
            SetObfuscator();
            AssetDatabase.Refresh();
        }
        EditorGUILayout.EndHorizontal();
    }

    ///<summary>设置混淆</summary>
    private void SetObfuscator()
    {
        //bool exis = macroitems.Exists(m => m.Name == "DEBUG_MODEL" && m.isRelease);
        //Options options = OptionsManager.LoadOptions();
        //if (options.enabled == exis)
        //{
        //    options.enabled = !exis;
        //    EditorUtility.SetDirty(options);
        //    AssetDatabase.SaveAssets();
        //}
    }
}
