using System;
using System.Collections.Generic;

namespace ChillConnect
{
    public sealed class ModuleManager : BaseInterfaceManager<ModuleManager>
    {
        private Dictionary<string, BaseModel> modelDict = new();
        private Dictionary<string, Type> uiTypeDict = new();
        private Dictionary<string, BaseCtrl> ctrlDict = new();
        private Dictionary<string, BaseUICtrl> uiCtrlDict = new();

        public override void Init()
        {
            base.Init();
            InitAllModule();
        }

        private void InitAllModule()
        {
            List<string> ctrlDisableList = AppConst.CtrlDisableList;

            foreach (BaseModel model in modelDict.Values)
            {
                model.New();
            }

            foreach (BaseCtrl ctrl in ctrlDict.Values)
            {
                if (!ctrl.isEnable)
                {
                    continue;
                }

                if (ctrlDisableList.Contains(ctrl.ctrlName))
                {
                    ctrl.isEnable = false;
                    continue;
                }

                ctrl.isEnable = true;
                ctrl.New();
            }

            foreach (BaseUICtrl uiCtrl in uiCtrlDict.Values)
            {
                if (!uiCtrl.isEnable)
                {
                    continue;
                }

                if (ctrlDisableList.Contains(uiCtrl.ctrlName))
                {
                    uiCtrl.isEnable = false;
                    continue;
                }

                uiCtrl.isEnable = true;
                uiCtrl.New();
            }


            foreach (BaseModel model in modelDict.Values)
            {
                model.Init();
            }

            foreach (BaseCtrl ctrl in ctrlDict.Values)
            {
                ctrl.Init();
            }

            foreach (BaseUICtrl uiCtrl in uiCtrlDict.Values)
            {
                uiCtrl.Init();
            }
        }

        public void StartUpAllModule()
        {
            foreach (BaseModel model in modelDict.Values)
            {
                model.StartUp();
            }

            foreach (BaseCtrl ctrl in ctrlDict.Values)
            {
                ctrl.StartUp();
            }

            foreach (BaseUICtrl uiCtrl in uiCtrlDict.Values)
            {
                uiCtrl.StartUp();
            }
        }

        public void AllModuleGameStart()
        {
            foreach (BaseModel model in modelDict.Values)
            {
                model.GameStart();
            }

            foreach (BaseCtrl ctrl in ctrlDict.Values)
            {
                ctrl.GameStart();
            }

            foreach (BaseUICtrl uiCtrl in uiCtrlDict.Values)
            {
                uiCtrl.GameStart();
            }
        }

        public BaseModel GetModel(string modelName)
        {
            BaseModel model = null;
            if (!modelDict.TryGetValue(modelName, out model))
            {
            }

            return model;
        }

        public BaseUICtrl GetUICtrl(string uiCtrlName)
        {
            BaseUICtrl uiCtrl = null;
            if (!uiCtrlDict.TryGetValue(uiCtrlName, out uiCtrl))
            {
            }

            return uiCtrl;
        }

        public void AddModel(string modelName, BaseModel model)
        {
            model.modelName = modelName;
            modelDict[modelName] = model;
        }

        public void AddUIType(string uiName, Type uiType)
        {
            uiTypeDict[uiName] = uiType;
        }

        public void AddCtrl(string ctrlName, BaseCtrl ctrl)
        {
            ctrl.ctrlName = ctrlName;
            ctrlDict[ctrlName] = ctrl;
        }

        public void AddUICtrl(string ctrlName, BaseUICtrl uiCtrl)
        {
            uiCtrl.ctrlName = ctrlName;
            uiCtrlDict[ctrlName] = uiCtrl;
        }

        public void DisposeAllModel()
        {
            foreach (BaseModel model in modelDict.Values)
            {
                model.Dispose();
            }

            modelDict.Clear();
        }

        public void DisposeAllCtrl()
        {
            foreach (BaseCtrl ctrl in ctrlDict.Values)
            {
                ctrl.Dispose();
            }

            foreach (BaseUICtrl uiCtrl in uiCtrlDict.Values)
            {
                uiCtrl.Dispose();
            }

            ctrlDict.Clear();
            uiCtrlDict.Clear();
        }

        public void DisposeAllModule()
        {
            DisposeAllModel();
            DisposeAllCtrl();
        }

        public override void Dispose()
        {
            base.Dispose();

            modelDict = null;
            uiTypeDict = null;
            ctrlDict = null;
            uiCtrlDict = null;
        }
    }
}