using Thousandto.Code.Center;
using Thousandto.Core.Base;
using Thousandto.Plugins.Common;
using System;
using System.Collections.Generic;
using System.Text;
using Thousandto.Code.Global;
using Thousandto.Cfg.Data;
using XLua;
using CoreEventDefine = UnityEngine.Gonbest.MagicCube.CoreEventDefine;

namespace Thousandto.Code.Logic
{
    /// <summary>
    /// 消息提示系统
    /// 1.对话框窗体
    /// 2.走马的灯的公告窗体
    /// 3.往上滚动的提示消息窗体
    /// </summary>
    public class MsgPromptSystem
    {
        //初始化处理
        public void Initialize()
        {
            //清除跑马灯信息
            GameCenter.PushFixEvent(LogicEventDefine.EID_EVENT_MSG_MARQUEE_CLEAR);
            GameCenter.RegFixEventHandle(CoreEventDefine.EID_CORE_GLOBAL_SHOW_MESSAGE_BOX, OnGlobalShowMessageHandler);
        }

        //卸载处理
        public void Uninitialize()
        {
            GameCenter.UnRegFixEventHandle(CoreEventDefine.EID_CORE_GLOBAL_SHOW_MESSAGE_BOX, OnGlobalShowMessageHandler);
        }

        /// <summary>
        /// 处理全局展示消息的句柄
        /// </summary>
        /// <param name="param"></param>
        /// <param name="sender"></param>
        private void OnGlobalShowMessageHandler(object param, object sender)
        {
            var ps = param as object[];
            if (ps.Length > 1)
            {
                var method = ps[0] as string;
                if (!string.IsNullOrEmpty(method))
                {
                    method = method.Trim();
                    switch (method)
                    {
                        case "ShowMsgBox":
                            //全局的显示Box,需要做一下翻译
                            var text = ps[1] as string;
                            var btn1 = ps[2] as string;
                            var bnt2 = ps[3] as string;

                            if (!string.IsNullOrEmpty(text))
                            {
                                text = GameCenter.LanguageConvertSystem.ConvertLan(text);
                            }

                            if (!string.IsNullOrEmpty(btn1))
                            {
                                btn1 = GameCenter.LanguageConvertSystem.ConvertLan(btn1);
                            }

                            if (!string.IsNullOrEmpty(bnt2))
                            {
                                bnt2 = GameCenter.LanguageConvertSystem.ConvertLan(bnt2);
                            }

                            //这里的消息提示参数为:new object[] { "ShowMsgBox"/*方法 字符串*/, msg/*提示信息 字符串*/, btn1Str/*第一个按钮 字符串*/, btn2Str/*第二个按钮 字符串*/, callBackAction/*回调函数 类型为Action<int>*/ }
                            ShowMsgBox(text, btn1, bnt2, x =>
                            {
                                var act = ps[4] as Action<int>;
                                if (act != null)
                                {
                                    act((int)x);
                                }
                            });
                            break;

                    }
                  
                }
            }
        }
        /// <summary>
        /// 提示系统信息
        /// </summary>
        /// <param name="str">显示的文本消息</param>        
        /// <param name="pro">优先级</param>
        public void ShowPrompt(string str, MsgInfoPriority pro = MsgInfoPriority.Highest)
        {
            if (string.IsNullOrEmpty(str)) return;
            MsgPromptInfo temp = new MsgPromptInfo();
            temp.Msg = str;
            temp.Priority = pro;
            temp.ShowLifeTime = 2f;
            GameCenter.PushFixEvent((int)UIEventDefine.UISYSTEMINFO_SHOWINFO, temp);
        }
        /// <summary>
        /// 提示系统信息
        /// </summary>
        /// <param name="msgStrId">messagestringid</param>        
        /// <param name="parames">参数</param>
        public void ShowPromptByMsgStrID(long msgStrId, params object[] parames)
        {
            string str = null;
            if(parames.Length > 0)
            {
                str = DeclareMessageString.Format((int)msgStrId, parames);
            }
            else
            {
                str = DeclareMessageString.Get((int)msgStrId);
            }
            if (string.IsNullOrEmpty(str)) return;
            MsgPromptInfo temp = new MsgPromptInfo();
            temp.Msg = str;
            temp.Priority = MsgInfoPriority.Highest;
            temp.ShowLifeTime = 2f;
            GameCenter.PushFixEvent((int)UIEventDefine.UISYSTEMINFO_SHOWINFO, temp);
        }
        /// <summary>
        /// 提示系统信息(带Item的)
        /// </summary>
        /// <param name="str">显示的文本消息</param>        
        /// <param name="pro">优先级</param>
        public void ShowPrompt(ItemBase itemBase, MsgInfoPriority pro = MsgInfoPriority.Highest)
        {
            if (itemBase == null) return;
            MsgPromptInfo temp = new MsgPromptInfo();
            //temp.Msg = str;
            temp.ItemBase = itemBase;
            temp.Priority = pro;
            temp.ShowLifeTime = 2f;
            GameCenter.PushFixEvent((int)UIEventDefine.UISYSTEMINFO_SHOWINFO, temp);
        }

        /// <summary>
        /// 走马灯
        /// </summary>
        /// <param name="str">消息文本</param>
        /// <param name="speed">速度</param>
        /// <param name="times">显示次数</param>
        /// <param name="pro">优先级</param>
        public void ShowMarquee(string str, float speed = 50, int times = 1, MsgInfoPriority pro = MsgInfoPriority.Highest)
        {
            MsgMarqueeInfo temp = new MsgMarqueeInfo();
            temp.Msg = str;
            temp.Priority = pro;
            temp.Speed = speed;
            temp.Times = times;            
            GameCenter.PushFixEvent((int)UIEventDefine.UIMARQUEE_SHOWINFO, temp);
        }
        public void ShowYunYingMarquee(string str, float speed = 50, int times = 1, MsgInfoPriority pro = MsgInfoPriority.Highest)
        {
            MsgMarqueeInfo temp = new MsgMarqueeInfo();
            temp.Msg = str;
            temp.Priority = pro;
            temp.Speed = speed;
            temp.Times = times;
            GameCenter.PushFixEvent((int)UIEventDefine.UIYunYingMarqueeForm_OPEN, temp);
        }

        //显示高阶公告
        public void ShowHighOrderNotice(string str)
        {
            MsgHighLevelNotifyInfo temp = new MsgHighLevelNotifyInfo();
            temp.Msg = str;
            GameCenter.PushFixEvent((int)LogicEventDefine.EID_EVENT_HIGHNOTIFY, temp);
        }

        /// <summary>
        /// 显示消息对话框
        /// </summary>
        /// <param name="str">消息文本</param>
        /// <param name="btnText1">第一个按钮的文本</param>
        /// <param name="btnText2">第二个按钮的文本</param>
        /// <param name="callBack">回调函数</param>
        /// <param name="isClickBGClose">点击背景就会关闭</param>
        /// <param name="autoHide">不操作情况下自动关闭</param>
        /// <param name="liftTime">生存期</param>
        /// <param name="pro">优先级</param>
        /// <param name="btnSprite1">第一个按钮的背景Sprite</param>
        /// <param name="btnSprite2">第二个按钮的背景Sprite</param>
        /// <param name="isDoCallBackHideAfter">隐藏后是否执行回调</param>
        public void ShowMsgBox(bool isDoCallBackHideAfter , string str, MyAction<MsgBoxResultCode> callBack, bool isClickBGClose = false, bool autoHide = false, float liftTime = 5, MsgInfoPriority pro = MsgInfoPriority.Highest, float alpha = 1, string btnSprite1 = null, string btnSprite2 = null, MsgBoxInfoTypeCode msgBoxInfoType = MsgBoxInfoTypeCode.None)
        {
            ShowMsgBox(str, DeclareMessageString.Get(DeclareMessageString.C_MSGBOX_CANCEL), DeclareMessageString.Get(DeclareMessageString.C_MSGBOX_OK), callBack, isClickBGClose, autoHide, liftTime, pro, alpha, btnSprite1, btnSprite2, msgBoxInfoType, isDoCallBackHideAfter);
        }

        /// <summary>
        /// 显示消息对话框
        /// </summary>
        /// <param name="str">消息文本</param>
        /// <param name="btnText1">第一个按钮的文本</param>
        /// <param name="btnText2">第二个按钮的文本</param>
        /// <param name="callBack">回调函数</param>
        /// <param name="isClickBGClose">点击背景就会关闭</param>
        /// <param name="autoHide">不操作情况下自动关闭</param>
        /// <param name="liftTime">生存期</param>
        /// <param name="pro">优先级</param>
        /// <param name="btnSprite1">第一个按钮的背景Sprite</param>
        /// <param name="btnSprite2">第二个按钮的背景Sprite</param>
        public void ShowMsgBox(string str, MyAction<MsgBoxResultCode> callBack, bool isClickBGClose = false, bool autoHide = false, float liftTime = 5, MsgInfoPriority pro = MsgInfoPriority.Highest, float alpha = 1, string btnSprite1 = null, string btnSprite2 = null,MsgBoxInfoTypeCode msgBoxInfoType = MsgBoxInfoTypeCode.None)
        {
            ShowMsgBox(str, DeclareMessageString.Get(DeclareMessageString.C_MSGBOX_CANCEL), DeclareMessageString.Get(DeclareMessageString.C_MSGBOX_OK), callBack, isClickBGClose, autoHide, liftTime, pro, alpha, btnSprite1, btnSprite2, msgBoxInfoType);
        }
        public void ShowMsgBox(string str, string btnText1 = null, string btnText2 = null, MyAction<MsgBoxResultCode> callBack = null, bool isClickBGClose = false, bool autoHide = false, float liftTime = 5, MsgInfoPriority pro = MsgInfoPriority.Highest, float alpha = 1, string btnSprite1 = null, string btnSprite2 = null,MsgBoxInfoTypeCode msgBoxInfoType = MsgBoxInfoTypeCode.None, bool isDoCallBackHideAfter = false)
        {
            MsgBoxSelectInfo temp = new MsgBoxSelectInfo();
            temp.IsClickBGClose = isClickBGClose;
            temp.IsAutoHide = autoHide;
            temp.ShowLifeTime = liftTime;
            temp.Button1 = btnText1 == null ? null : new MsgBoxButton(btnText1, btnSprite1);
            temp.Button2 = btnText2 == null ? null : new MsgBoxButton(btnText2, btnSprite2);
            temp.Priority = pro;
            temp.Msg = str;
            temp.AlphaValue = alpha;
            temp.OnCallBack = callBack;
            temp.MsgBoxInfoType = msgBoxInfoType;
            temp.OnSelectCallBack = null;
            temp.SelectText = null;
            temp.IsMsgBoxSelect = false;
            temp.IsDoCallBackHideAfter = isDoCallBackHideAfter;
            GameCenter.PushFixEvent((int)UIEventDefine.UIMESSAGEBOX_SHOWINFO, temp);
        }
        public void ShowSelectMsgBox(string str, string btnText1 = null, string btnText2 = null, MyAction<MsgBoxResultCode> callBack = null,MyAction<MsgBoxIsSelect> selectCallBack = null,string selectText = null, bool isClickBGClose = false, bool autoHide = false, float liftTime = 5, MsgInfoPriority pro = MsgInfoPriority.Highest, float alpha = 1, string btnSprite1 = null, string btnSprite2 = null, MsgBoxInfoTypeCode msgBoxInfoType = MsgBoxInfoTypeCode.None, bool isDoCallBackHideAfter = false)
        {
            MsgBoxSelectInfo temp = new MsgBoxSelectInfo();
            temp.IsClickBGClose = isClickBGClose;
            temp.IsAutoHide = autoHide;
            temp.ShowLifeTime = liftTime;
            temp.Button1 = btnText1 == null ? null : new MsgBoxButton(btnText1, btnSprite1);
            temp.Button2 = btnText2 == null ? null : new MsgBoxButton(btnText2, btnSprite2);
            temp.Priority = pro;
            temp.Msg = str;
            temp.AlphaValue = alpha;
            temp.OnCallBack = callBack;
            temp.MsgBoxInfoType = msgBoxInfoType;
            temp.OnSelectCallBack = selectCallBack;
            temp.SelectText = selectText;
            temp.IsMsgBoxSelect = true;
            temp.IsDoCallBackHideAfter = isDoCallBackHideAfter;
            GameCenter.PushFixEvent((int)UIEventDefine.UIMESSAGEBOX_SHOWINFO, temp);
        }
        //显示msgbox,lua专用,用于降低GC
        public void ShowMsgBoxByMsgStrID(MyAction<MsgBoxResultCode> callBack, long btn1StrId, long btn2StrId, long msgStrId, params object[] parames)
        {
            string str = null;
            if (parames.Length > 0)
            {
                str = DeclareMessageString.Format((int)msgStrId, parames);
            }
            else
            {
                str = DeclareMessageString.Get((int)msgStrId);
            }
            string btn1Text = null; 
            if(btn1StrId >= 0)
            {
                btn1Text = DeclareMessageString.Get((int)btn1StrId);
            }
            string btn2Text = null;
            if(btn2StrId >= 0)
            {
                btn2Text = DeclareMessageString.Get((int)btn2StrId);
            }
            ShowMsgBox(str, btn1Text, btn2Text, callBack);
        }
        //关闭消息框
        public void CloseMsgBox()
        {
            GameCenter.PushFixEvent((int)UIEventDefine.UIMESSAGEBOX_CLOSE);             
        }


        #region//静态函数  -- 提供工具函数

        //把一个数据压倒一个堆栈中,通过优先级排序
        public static void EnQueue<T>(List<T> msgQueue, T info)
            where T : MsgInfoBase
        {
            if (msgQueue.Count > 0)
            {
                for (int i = msgQueue.Count - 1; i >= 0; i--)
                {
                    if (msgQueue[i].Priority >= info.Priority)
                    {
                        msgQueue.Insert(i + 1, info);
                        break;
                    }
                }
            }
            else
            {
                msgQueue.Add(info);
            }
        }

        public static void EnQueueMsgPromptInfo(List<MsgPromptInfo> msgQueue, MsgPromptInfo info)
        {
            if (msgQueue.Count > 0)
            {
                for (int i = msgQueue.Count - 1; i >= 0; i--)
                {
                    if (msgQueue[i].Priority >= info.Priority)
                    {
                        msgQueue.Insert(i + 1, info);
                        break;
                    }
                }
            }
            else
            {
                msgQueue.Add(info);
            }
        }

        //从消息队列中获取一个数据
        public static T DeQueue<T>(List<T> msgQueue)
             where T : MsgInfoBase
        {
            if (msgQueue.Count > 0)
            {
                var item = msgQueue[0];
                msgQueue.RemoveAt(0);
                return item;
            }
            return default(T);
        }

        public static MsgBoxInfo DeQueueMsgBoxInfo(List<MsgBoxInfo> msgQueue)
        {
            if (msgQueue.Count > 0)
            {
                var item = msgQueue[0];
                msgQueue.RemoveAt(0);
                return item;
            }
            return null;
        }

        public static MsgPromptInfo DeQueueMsgPromptInfo(List<MsgPromptInfo> msgQueue)
        {
            if (msgQueue.Count > 0)
            {
                var item = msgQueue[0];
                msgQueue.RemoveAt(0);
                return item;
            }
            return null;
        }
        #endregion


    }
}