using UnityEngine;
using UnityEngine.Events;

namespace WS
{
    [ObjectSystem]
    public class FacadeComponentAwake : AwakeSystem<FacadeComponent>
    {
        public override void Awake(FacadeComponent self)
        {
            self.Awake();
        }
    }

    ///<summary>MVC 中介者</summary>
    public class FacadeComponent : WSComponent
    {

        public static FacadeComponent Instance;
        //Model管理器
        private ModelManager _ModelManager;
        //View管理器
        private ViewManager _ViewManager;
        //Controller管理器
        private ControllerManager _ControllerManager;

        public void Awake()
        {
            Instance = this;
            _ModelManager = AddComponent<ModelManager>();
            _ViewManager = AddComponent<ViewManager>();
            _ControllerManager = AddComponent<ControllerManager>();
        }

        #region ---- View ----
        ///<summary>打开窗口</summary>
        public V OpenView<V>(UnityAction opencallback = null) where V : View
        {
            return _ViewManager.OpenView<V>(opencallback);
        }

        ///<summary>打开克隆窗口</summary>
        public V OpenView<V>(string clone, UnityAction opencallback = null) where V : View
        {
            return _ViewManager.OpenView<V>(clone, opencallback);
        }

        ///<summary>打开窗口</summary>
        public V OpenView<V>(ViewShowModeType showmode, UnityAction opencallback = null) where V : View
        {
            return _ViewManager.OpenView<V>(showmode, opencallback);
        }

        ///<summary>打开克隆窗口</summary>
        public V OpenView<V>(ViewShowModeType showmode, string clone, UnityAction opencallback = null) where V : View
        {
            return _ViewManager.OpenView<V>(showmode, clone, opencallback);
        }

        ///<summary>关闭窗口</summary>
        public void CloseView(View view, UnityAction closecallback)
        {
            _ViewManager.CloseView(view, closecallback);
        }

        ///<summary>创建ViewCanvas</summary>
        public void CreateViewCanvas(ViewShowModeType viewshow, Transform root)
        {
            _ViewManager.CreateViewCanvas(viewshow, root);
        }

        ///<summary>获取ViewCanvas</summary>
        public ViewCanvas GetViewCanvas(ViewShowModeType viewshow)
        {
            return _ViewManager.GetViewCanvas(viewshow);
        }

        ///<summary>移除ViewCanvas</summary>
        public void RemoveViewCanvas(ViewShowModeType viewshow)
        {
            _ViewManager.RemoveViewCanvas(viewshow);
        }

        #endregion

        #region ---- Model ----

        ///<summary>注册/创建Model</summary>
        public T CreateModel<T>() where T : Model
        {
            return _ModelManager.CreateModel<T>();
        }

        ///<summary>删除Model</summary>
        public void RemoveModel(Model model)
        {
            _ModelManager.Remove(model);
        }

        ///<summary>删除所有Model</summary>
        public void RemoveAllModel()
        {
            _ModelManager.RemoveAll();
        }
        #endregion

        #region ---- Controller ----

        /// <summary>注册/创建控制器</summary>
        /// <param name="isenterlibrary">是否存入集合(存入后不能重复创建)</param>
        public T CreateController<T>(bool isenterlibrary = true) where T : BaseController
        {
            return _ControllerManager.CreateController<T>(isenterlibrary);
        }

        ///<summary>删除控制器</summary>
        public void RemoveController<T>() where T : BaseController
        {
            _ControllerManager.RemoveController<T>();
        }

        ///<summary>删除控制器</summary>
        public void RemoveController(BaseController controller)
        {
            _ControllerManager.RemoveController(controller);
        }

        ///<summary>删除所有控制器</summary>
        public void RemoveAllController()
        {
            _ControllerManager.RemoveAllController();
        }

        ///<summary>获取控制器</summary>
        public T GetController<T>() where T : BaseController
        {
            return _ControllerManager.GetController<T>();
        }
        #endregion

        public override void Dispose()
        {
            base.Dispose();
            _ControllerManager.Dispose();
            _ViewManager.Dispose();
            _ModelManager.Dispose();
        }
    }
}