using System.Collections.Generic;
using FairyGUI;
using FGUI.Lobby_03;
using IgnoreOPS;
using Newtonsoft.Json;
using UnityEngine;

namespace ChillConnect
{
    public class FAQUI : BaseUI
    {
        private FAQUICtrl ctrl;
        private FAQModel model;
        private com_faq ui;

        public FAQUI(FAQUICtrl ctrl) : base(ctrl)
        {
            uiName = UIConst.FAQUI;
            this.ctrl = ctrl;
        }

        protected override void SetUIInfo(UIInfo uiInfo)
        {
            uiInfo.packageName = "Lobby_03";
            uiInfo.assetName = "com_faq";
            uiInfo.layerType = UILayerType.Popup;
            uiInfo.isNeedOpenAnim = false;
            uiInfo.isNeedCloseAnim = false;
            uiInfo.isNeedUIMask = true;
        }

        #region 生命周期
        protected override void OnInit()
        {
            //model = ModuleManager.Instance.GetModel(ModelConst.FAQModel) as FAQModel;
        }

        protected override void OnClose()
        {
        }

        protected override void OnBind()
        {
            ui = baseUI as com_faq;
        }

        protected override void OnOpenBefore(object args)
        {

            if (GameHelper.isRDExchangeMode())
            {
                rule_list = ConfigSystem.GetConfig<FAQRuleModel1>().DataList;
                message_list = ConfigSystem.GetConfig<MessageBoardModel>().DataList;
            }
            else
            {
                rule_list = ConfigSystem.GetConfig<FAQRuleModel>().DataList;
                message_list = ConfigSystem.GetConfig<MessageBoardModel>().DataList;
            }

            InitView();
        }
        private List<FAQRule> rule_list;
        private List<MessageBoard> message_list;
        protected override void OnOpen(object args)
        {
        }

        protected override void OnHide()
        {
        }

        protected override void OnDisplay(object args)
        {
        }
        #endregion

        #region 消息
        protected override void AddListener()
        {
            GameDispatcher.Instance.AddListener(GameMsg.faq_refresh, refreshList);
        }
        protected override void RemoveListener()
        {
            GameDispatcher.Instance.RemoveListener(GameMsg.faq_refresh, refreshList);
        }
        #endregion
        void refreshList(object a)
        {
            com_msg com_msg = (com_msg)UIPackage.CreateObject("Lobby_03", "com_msg");
            com_msg.text_msg.text = list_user_arr[list_user_arr.Count - 1] + ":" + list_content_arr[list_content_arr.Count - 1];

            ui.list_msg.AddChild(com_msg);
            ui.list_msg.scrollPane.ScrollDown(500, false);
        }
        //初始化页面逻辑
        private void InitView()
        {
            ui.btn_close.SetClick(CtrlCloseUI);
            for (int i = 0; i < rule_list.Count; i++)
            {
                com_question question = (com_question)UIPackage.CreateObject("Lobby_03", "com_question");
                question.text_question.text = (i + 1) + "." + rule_list[i].Rule;
                if (question.text_question.width > 730)
                {
                    question.text_question.autoSize = AutoSizeType.Shrink;
                    question.text_question.width = 730;
                }
                ui.list_faq.AddChild(question);
                for (int j = 0; j < 4; j++)
                {
                    if (j == 0)
                    {
                        if (!string.IsNullOrEmpty(rule_list[i].Information1))
                        {
                            com_answer anster = (com_answer)UIPackage.CreateObject("Lobby_03", "com_answer");
                            anster.text_answer.text = rule_list[i].Information1;
                            ui.list_faq.AddChild(anster);
                        }
                    }
                    else if (j == 1)
                    {
                        if (!string.IsNullOrEmpty(rule_list[i].Information2))
                        {
                            com_answer anster = (com_answer)UIPackage.CreateObject("Lobby_03", "com_answer");
                            anster.text_answer.text = rule_list[i].Information2;
                            ui.list_faq.AddChild(anster);
                        }
                    }
                    else if (j == 2)
                    {
                        if (!string.IsNullOrEmpty(rule_list[i].Information3))
                        {
                            com_answer anster = (com_answer)UIPackage.CreateObject("Lobby_03", "com_answer");
                            anster.text_answer.text = rule_list[i].Information3;
                            ui.list_faq.AddChild(anster);
                        }
                    }
                    else if (j == 3)
                    {
                        if (!string.IsNullOrEmpty(rule_list[i].Information4))
                        {
                            com_answer anster = (com_answer)UIPackage.CreateObject("Lobby_03", "com_answer");
                            anster.text_answer.text = rule_list[i].Information4;
                            ui.list_faq.AddChild(anster);
                        }
                    }
                }
            }

            if (user_arr == null) //第一次显示
            {
                user_arr = message_list[0].user_name.Split(",");
                int user_index = PlayerPrefs.GetInt("user_index", 0);
                if (user_index + 10 >= user_arr.Length) user_index = 0;


                content_arr = message_list[0].message.Split(",");
                int faq_index = PlayerPrefs.GetInt("faq_index", 0);
                if (faq_index + 10 >= content_arr.Length) faq_index = 0;


                for (int i = user_index; i < user_index + 10; i++)
                {
                    list_user_arr.Add(user_arr[i]);
                }
                for (int i = faq_index; i < faq_index + 10; i++)
                {
                    list_content_arr.Add(content_arr[i]);
                }
                if (PlayerPrefs.GetInt("user_FAQindex", 0) < 10)
                {
                    if (!string.IsNullOrEmpty(PlayerPrefs.GetString("user_FAQcontent", "")))
                    {
                        list_user_arr.Insert(10 - PlayerPrefs.GetInt("user_FAQindex", 0), DataMgr.PlayerName.Value);
                        list_content_arr.Insert(10 - PlayerPrefs.GetInt("user_FAQindex", 0), PlayerPrefs.GetString("user_FAQcontent", ""));
                    }
                }
                user_index += 10;
                faq_index += 10;
                PlayerPrefs.SetInt("user_index", user_index);
                PlayerPrefs.SetInt("faq_index", faq_index);
            }



            for (int i = 0; i < list_user_arr.Count; i++)
            {
                com_msg com_msg = (com_msg)UIPackage.CreateObject("Lobby_03", "com_msg");
                com_msg.text_msg.text = list_user_arr[i] + ":" + list_content_arr[i];
                ui.list_msg.AddChild(com_msg);
            }

            ui.btn_send.SetClick(() =>
            {
                if (string.IsNullOrEmpty(ui.input_msg.text))
                {
                    GameHelper.ShowTips("Please enter your message!");
                    return;
                }
                Debug.Log(ui.input_msg.text);
                com_msg com_msg = (com_msg)UIPackage.CreateObject("Lobby_03", "com_msg");
                com_msg.text_msg.text = DataMgr.PlayerName + ":" + ui.input_msg.text;
                list_user_arr.Add(DataMgr.PlayerName.Value);
                list_content_arr.Add(ui.input_msg.text);
                PlayerPrefs.SetInt("faq_time", (int)GameHelper.GetNowTime());
                PlayerPrefs.SetInt("user_FAQindex", 0);
                PlayerPrefs.SetString("user_FAQcontent", ui.input_msg.text);

                ui.input_msg.text = "";
                ui.input_msg.promptText = "[color=#999999][size=40]Can only send once every hour[/size][/color]";
                ui.input_msg.touchable = false;
                ui.btn_send.SetClick(() => { });
                ui.list_msg.AddChild(com_msg);
                ui.list_msg.scrollPane.ScrollDown(500, false);

                ui.btn_send.grayed = true;
                ui.input_bg.grayed = true;

            });
            ui.list_msg.scrollPane.ScrollDown(1000, false);
            if (PlayerPrefs.GetInt("faq_time", 0) + 3600 > GameHelper.GetNowTime())
            {
                ui.input_msg.promptText = "[color=#999999][size=40]Can only send once every hour[/size][/color]";
                ui.input_msg.touchable = false;
                ui.btn_send.SetClick(() => { });
                ui.btn_send.grayed = true;
                ui.input_bg.grayed = true;
            }

            ui.btn_contactus.SetClick(() => { GameHelper.OpenEmail(); });
        }

        public static string[] user_arr;
        public static string[] content_arr;

        public static List<string> list_user_arr = new List<string>();
        public static List<string> list_content_arr = new List<string>();

    }
}