using UnityEngine.Events;

namespace WS
{
    ///<summary>Controller 基类</summary>
    public abstract class BaseController : WSComponent
    {
        ///<summary>打开UI</summary>
        public abstract void OpenView();

        ///<summary>打开UI</summary>
        ///<param name="opencallback">回调</param>
        public abstract void OpenView(UnityAction opencallback);

        /// <summary>打开UI</summary>
        /// <param name="clone">克隆体名称</param>
        public abstract void OpenView(string clone);

        /// <summary>打开UI</summary>
        /// <param name="clone">克隆体名称</param>
        ///<param name="opencallback">回调</param>
        public abstract void OpenView(string clone, UnityAction opencallback);

        /// <summary>打开UI</summary>
        /// <param name="showmode">显示Canvas类型</param>
        public abstract void OpenView(ViewShowModeType showmode);

        /// <summary>打开UI</summary>
        /// <param name="showmode">显示Canvas类型</param>
        /// <param name="clone">克隆体名称</param>
        public abstract void OpenView(ViewShowModeType showmode, string clone);

        /// <summary>打开UI</summary>
        /// <param name="showmode">显示Canvas类型</param>
        /// <param name="clone">克隆体名称</param>
        ///<param name="opencallback">回调</param>
        public abstract void OpenView(ViewShowModeType showmode, string clone, UnityAction opencallback);

        /// <summary>关闭UI</summary>
        public abstract void CloseView(UnityAction closecallback = null, bool isDispose = true);
    }

    ///<summary>控制器-View</summary>
    public class Controller<V> : BaseController where V : View
    {
        ///<summary>视图</summary>
        protected V _View;

        ///<summary>打开UI</summary>
        public override void OpenView()
        {
            _View = FacadeComponent.Instance.OpenView<V>();
        }

        ///<summary>打开UI</summary>
        ///<param name="opencallback">回调</param>
        public override void OpenView(UnityAction opencallback)
        {
            _View = FacadeComponent.Instance.OpenView<V>(opencallback);
        }

        /// <summary>打开UI</summary>
        /// <param name="clone">克隆体名称</param>
        public override void OpenView(string clone)
        {
            _View = FacadeComponent.Instance.OpenView<V>(clone);
        }

        /// <summary>打开UI</summary>
        /// <param name="clone">克隆体名称</param>
        ///<param name="opencallback">回调</param>
        public override void OpenView(string clone, UnityAction opencallback)
        {
            _View = FacadeComponent.Instance.OpenView<V>(clone, opencallback);
        }

        /// <summary>打开UI</summary>
        /// <param name="showmode">显示Canvas类型</param>
        public override void OpenView(ViewShowModeType showmode)
        {
            _View = FacadeComponent.Instance.OpenView<V>(showmode);
        }

        /// <summary>打开UI</summary>
        /// <param name="showmode">显示Canvas类型</param>
        /// <param name="clone">克隆体名称</param>
        public override void OpenView(ViewShowModeType showmode, string clone)
        {
            _View = FacadeComponent.Instance.OpenView<V>(showmode, clone);
        }

        /// <summary>打开UI</summary>
        /// <param name="showmode">显示Canvas类型</param>
        /// <param name="clone">克隆体名称</param>
        ///<param name="opencallback">回调</param>
        public override void OpenView(ViewShowModeType showmode, string clone, UnityAction opencallback)
        {
            _View = FacadeComponent.Instance.OpenView<V>(showmode, clone, opencallback);
        }

        /// <summary>关闭UI</summary>
        public override void CloseView(UnityAction closecallback = null, bool isDispose = true)
        {
            FacadeComponent.Instance.CloseView(_View, closecallback);
            _View = null;
            if (isDispose)
            {
                FacadeComponent.Instance.RemoveController(this);
            }
        }

        public override void Dispose()
        {
            base.Dispose();
            if (_View != null)
            {
                if (_View.ShowType == ViewShowType.Open)
                {
                    CloseView();
                }
                _View = null;
            }
        }
    }

    ///<summary>控制器-View & Model</summary>
    public abstract class Controller<V, M> : Controller<V> where V : View where M : Model
    {
        ///<summary>数据</summary>
        protected M _Model;

        ///<summary>绑定View&Model</summary>
        protected abstract void BindingViewModel();

        public override void OpenView()
        {
            base.OpenView();
            CreateModel();
        }

        public override void OpenView(string clone)
        {
            base.OpenView(clone);
            CreateModel();
        }

        public override void OpenView(UnityAction opencallback = null)
        {
            base.OpenView(opencallback);
            CreateModel();
        }

        public override void OpenView(string clone, UnityAction opencallback = null)
        {
            base.OpenView(clone, opencallback);
            CreateModel();
        }

        public override void OpenView(ViewShowModeType showmode)
        {
            base.OpenView(showmode);
            CreateModel();
        }

        public override void OpenView(ViewShowModeType showmode, string clone)
        {
            base.OpenView(showmode, clone);
            CreateModel();
        }

        public override void OpenView(ViewShowModeType showmode, string clone, UnityAction opencallback)
        {
            base.OpenView(showmode, clone, opencallback);
            CreateModel();
        }

        ///<summary>创造Model</summary>
        private void CreateModel()
        {
            _Model = FacadeComponent.Instance.CreateModel<M>();
            BindingViewModel();
        }

        public override void Dispose()
        {
            base.Dispose();
            FacadeComponent.Instance.RemoveModel(_Model);
            _Model = null;
        }
    }
}