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

namespace NBF
{
    public class BuilderWindow : EditorWindow
    {
        public const int Width = 400;
        public const int Height = 645;

        [MenuItem("构建/打开构建面板", false, 9)]
        public static void ShowWindow()
        {
            BuilderWindow wnd = GetWindow<BuilderWindow>("APP打包器", true);
            wnd.minSize = new Vector2(Width, Height);
        }

        #region 样式

        private GUIStyle _styleTitleSmall;

        private GUIStyle StyleTitleSmall
        {
            get
            {
                if (_styleTitleSmall == null)
                {
                    _styleTitleSmall = new GUIStyle(EditorStyles.label)
                    {
                        fontStyle = FontStyle.Bold,
                        fontSize = 12
                    };
                }

                return _styleTitleSmall;
            }
        }

        #endregion

        private readonly HashSet<int> _selectTaskId = new HashSet<int>();
        private string Ver;

        protected void OnEnable()
        {
            Ver = PlayerSettings.bundleVersion;


            _selectTaskId.Clear();
        }

        protected void OnGUI()
        {
            EditorGUILayout.Space();

            DrawMenu();
            ShowTitle($"环境选择");
            Ver = EditorGUILayout.TextField("打包版本号", Ver);
            ShowTitle("任务列表");
            var tasks = Builder.BuildTaskInfo;
            foreach (var task in tasks)
            {
                var select = _selectTaskId.Contains(task.Id);
                GUI.color = select ? Color.white : Color.grey;
                if (task.Visable)
                {
                    //复选框
                    var s = EditorGUILayout.Toggle(task.Name, select, GUILayout.Height(20));
                    if (s)
                    {
                        _selectTaskId.Add(task.Id);
                    }
                    else
                    {
                        _selectTaskId.Remove(task.Id);
                    }
                }
            }


            EditorGUILayout.LabelField("", GUI.skin.horizontalSlider);

            GUI.color = Color.white;
            if (GUILayout.Button("执行打包", GUILayout.Height(45)))
            {
                Run();
            }
        }

        private void Run(bool distributed = false, bool cloud = false)
        {
            var ids = _selectTaskId.ToArray();
            if (!cloud)
            {
                BuildContext buildContext = new BuildContext
                {
                    Ver = Ver,
                };

                Builder.RunBuildTasks(buildContext, ids);
            }
            else
            {
                Debug.LogError("远端构建暂未完成==");
            }
        }


        private void DrawMenu()
        {
            ShowTitle("快速选中");

            GUILayout.BeginHorizontal();
            if (GUILayout.Button("提审包", GUILayout.Height(30)))
            {
                SelectAll();
            }
            

            if (GUILayout.Button("自测包", GUILayout.Height(30)))
            {
                SelectSelfTest();
            }

            if (GUILayout.Button("取消选择", GUILayout.Height(30)))
            {
                _selectTaskId.Clear();
            }

            EditorGUILayout.EndHorizontal();
        }

        #region 快速选择

        private void SelectAll()
        {
            _selectTaskId.Clear();
            foreach (var task in Builder.BuildTaskInfo)
            {
                _selectTaskId.Add(task.Id);
            }
        }
        

        private void SelectSelfTest()
        {
            _selectTaskId.Clear();
            _selectTaskId.Add(BuildTaskId.Excel);
            _selectTaskId.Add(BuildTaskId.AB);
            _selectTaskId.Add(BuildTaskId.App);
            _selectTaskId.Add(BuildTaskId.CopyShare);
        }

        #endregion

        private void ShowTitle(string str)
        {
            EditorGUILayout.Space();
            GUILayout.Label(str, StyleTitleSmall);
            EditorGUILayout.LabelField("", GUI.skin.horizontalSlider);
        }

        private bool HasSelectTask(int id)
        {
            return _selectTaskId.Contains(id);
        }
    }
}