using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

namespace WS
{
    ///<summary>消息框类型</summary>
    public enum MessagerType
    {
        Error,

        Default,
    }

    ///<summary>消息框控制器</summary>
    public class MessagerContorller : Controller<MessagerView, MessagerModel>
    {
        /// <summary>打开消息框</summary>
        /// <param name="type">面板类型</param>
        /// <param name="messager">消息</param>
        /// <param name="defineName">第一个按钮名称</param>
        /// <param name="onDefine">第一个按钮回调</param>
        /// <param name="cancelName">第二个按钮名称 (如果第二个按钮名称为空，不会显示)</param>
        /// <param name="onCancel">第二个按钮回调</param>Async
        public static void OpenMessageView(MessagerType type, string messager, string defineName = "确定", UnityAction onDefine = null, string cancelName = "", UnityAction onCancel = null)
        {
            MessagerContorller contorller = FacadeComponent.Instance.CreateController<MessagerContorller>(false);
            contorller.OpenView();
            contorller.SetViewAndModel(type, messager, defineName, onDefine, cancelName, onCancel);
        }

        /// <summary>打开消息框</summary>
        /// <param name="clone">克隆体</param>
        /// <param name="type">面板类型</param>
        /// <param name="messager">消息</param>
        /// <param name="defineName">第一个按钮名称</param>
        /// <param name="onDefine">第一个按钮回调</param>
        /// <param name="cancelName">第二个按钮名称 (如果第二个按钮名称为空，不会显示)</param>
        /// <param name="onCancel">第二个按钮回调</param>
        public static void OpenMessageView(string clone, MessagerType type, string messager, string defineName = "确定", UnityAction onDefine = null, string cancelName = "", UnityAction onCancel = null)
        {
            MessagerContorller contorller = FacadeComponent.Instance.CreateController<MessagerContorller>(false);
            contorller.OpenView(clone);
            contorller.SetViewAndModel(type, messager, defineName, onDefine, cancelName, onCancel);
        }

        /// <summary>打开消息框</summary>
        /// <param name="showMode">显示在什么类型的Canvas上</param>
        /// <param name="type">面板类型</param>
        /// <param name="messager">消息</param>
        /// <param name="defineName">第一个按钮名称</param>
        /// <param name="onDefine">第一个按钮回调</param>
        /// <param name="cancelName">第二个按钮名称 (如果第二个按钮名称为空，不会显示)</param>
        /// <param name="onCancel">第二个按钮回调</param>
        public static void OpenMessageView(ViewShowModeType showMode, MessagerType type, string messager, string defineName = "确定", UnityAction onDefine = null, string cancelName = "", UnityAction onCancel = null)
        {
            MessagerContorller contorller = FacadeComponent.Instance.CreateController<MessagerContorller>(false);
            contorller.OpenView(showMode);
            contorller.SetViewAndModel(type, messager, defineName, onDefine, cancelName, onCancel);
        }

        /// <summary>打开消息框</summary>
        /// <param name="showMode">显示在什么类型的Canvas上</param>
        /// <param name="clone">克隆体</param>
        /// <param name="type">面板类型</param>
        /// <param name="messager">消息</param>
        /// <param name="defineName">第一个按钮名称</param>
        /// <param name="onDefine">第一个按钮回调</param>
        /// <param name="cancelName">第二个按钮名称 (如果第二个按钮名称为空，不会显示)</param>
        /// <param name="onCancel">第二个按钮回调</param>
        public static void OpenMessageView(ViewShowModeType showMode, string clone, MessagerType type, string messager, string defineName = "确定", UnityAction onDefine = null, string cancelName = "", UnityAction onCancel = null)
        {
            MessagerContorller contorller = FacadeComponent.Instance.CreateController<MessagerContorller>(false);
            contorller.OpenView(showMode, clone);
            contorller.SetViewAndModel(type, messager, defineName, onDefine, cancelName, onCancel);
        }

        ///<summary>第一个按钮回调</summary>
        private UnityAction OnDefineCallback;
        ///<summary>第二个按钮回调</summary>
        private UnityAction OnCancelCallback;

        ///<summary>绑定View&Model</summary>
        protected override void BindingViewModel()
        {
            _Model.Bind(_View.M_Error_Text, _Model.Message);
            _Model.Bind(_View.M_Default_Text, _Model.Message);
            _Model.Bind(_View.Define_But, OnDefine);
            _Model.Bind(_View.Cancel_But, OnCancel);
            _Model.Bind(_View.B_Define_Text, _Model.ButDefineText);
            _Model.Bind(_View.B_Cancel_Text, _Model.ButCancelText);
        }

        ///<summary>第一个按钮回调绑定</summary>
        private void OnDefine()
        {
            OnDefineCallback?.Invoke();
            CloseView();
        }

        ///<summary>第二个按钮回调绑定</summary>
        private void OnCancel()
        {
            OnCancelCallback?.Invoke();
            CloseView();
        }

        /// <summary>设置消息框</summary>
        /// <param name="type">面板类型</param>
        /// <param name="messager">消息</param>
        /// <param name="defineName">第一个按钮名称</param>
        /// <param name="onDefine">第一个按钮回调</param>
        /// <param name="cancelName">第二个按钮名称 (如果第二个按钮名称为空，不会显示)</param>
        /// <param name="onCancel">第二个按钮回调</param>
        public void SetViewAndModel(MessagerType type, string messager, string defineName, UnityAction onDefine, string cancelName, UnityAction onCancel)
        {
            //是否是默认面板 
            bool panelactive = type == MessagerType.Default ? true : false;
            //是否是单个Button
            bool butactive = string.IsNullOrEmpty(cancelName);
            _Model.Message.Value = messager;
            _Model.ButDefineText.Value = defineName;
            OnDefineCallback = onDefine;
            if (!butactive)
            {
                OnCancelCallback = onCancel;
                _Model.ButCancelText.Value = cancelName;
            }
            _View.MessageDefault.SetActive(panelactive);
            _View.MessageError.SetActive(!panelactive);
            _View.Cancel_But.gameObject.SetActive(!butactive);
            if (butactive)
                _View.Define_But.transform.localPosition = new Vector3(0, -180, 0);
            else
                _View.Define_But.transform.localPosition = new Vector3(-220, -180, 0);
        }

        public override void Dispose()
        {
            base.Dispose();
            OnDefineCallback = null;
            OnCancelCallback = null;
        }
    }

    ///<summary>消息框View</summary>
    public class MessagerView : View
    {
        ///<summary>错误面板</summary>
        public GameObject MessageError;
        ///<summary>错误面板文本</summary>
        public Text M_Error_Text;
        ///<summary>普通面板</summary>
        public GameObject MessageDefault;
        ///<summary>普通面板文本</summary>
        public Text M_Default_Text;
        ///<summary>确认按钮</summary>
        public Button Define_But;
        ///<summary>确认按钮文本</summary>
        public Text B_Define_Text;
        ///<summary>取消按钮</summary>
        public Button Cancel_But;
        ///<summary>取消按钮文本</summary>
        public Text B_Cancel_Text;

        public override void LoadInit()
        {
            Group = ViewGroup.HUD;
            ReferenceCollector rc = UIGameObject.GetComponent<ReferenceCollector>();
            MessageError = rc.Get<GameObject>("MessageError");
            M_Error_Text = rc.GetComponent<Text>("M_Error_Text");
            MessageDefault = rc.Get<GameObject>("MessageDefault");
            M_Default_Text = rc.GetComponent<Text>("M_Default_Text");
            Define_But = rc.GetComponent<Button>("Define_But");
            B_Define_Text = rc.GetComponent<Text>("B_Define_Text");
            Cancel_But = rc.GetComponent<Button>("Cancel_But");
            B_Cancel_Text = rc.GetComponent<Text>("B_Cancel_Text");
        }
    }

    ///<summary>消息框Model</summary>
    public class MessagerModel : Model
    {
        ///<summary>消息</summary>
        public BindableProperty<string> Message;
        ///<summary>Define按钮名称</summary>
        public BindableProperty<string> ButDefineText;
        ///<summary>Cancel按钮名称</summary>
        public BindableProperty<string> ButCancelText;

        public override void InitProperty()
        {
            Message = CreateProperty<string>();
            ButDefineText = CreateProperty<string>();
            ButCancelText = CreateProperty<string>();
        }
    }
}