using System.Collections.Generic;
using IgnoreOPS;
using UnityEngine;

namespace ChillConnect
{
    public class FAQUICtrl : BaseUICtrl
    {
        private FAQUI ui;
        private FAQModel model;

        private uint openUIMsg = UICtrlMsg.FAQUI_Open;
        private uint closeUIMsg = UICtrlMsg.FAQUI_Close;

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

        protected override void OnDispose()
        {
        }

        public override void OpenUI(object args = null)
        {
            if (ui == null)
            {
                ui = new FAQUI(this);
                ui.Open(args);
            }
        }

        public override void CloseUI(object args = null)
        {
            if (ui != null && !ui.isClose)
            {
                ui.Close();
            }
            ui = null;
        }
        #endregion

        #region 消息
        public override uint GetOpenUIMsg(string uiName)
        {
            return openUIMsg;
        }
        public override uint GetCloseUIMsg(string uiName)
        {
            return closeUIMsg;
        }

        protected override void AddListener()
        {
            uiCtrlDispatcher.AddListener(openUIMsg, OpenUI);
            uiCtrlDispatcher.AddListener(closeUIMsg, CloseUI);
            HallManager.Instance.UpdateSecondEvent += timeEvent;

        }
        protected override void RemoveListener()
        {
            uiCtrlDispatcher.RemoveListener(openUIMsg, OpenUI);
            uiCtrlDispatcher.RemoveListener(closeUIMsg, CloseUI);
        }

        protected override void AddServerListener()
        {

        }
        protected override void RemoveServerListener()
        {

        }
        private int time;
        private int refresh_time=5;
        void timeEvent()
        {
            time++;
            // Debug.Log((int)GameHelper.GetNowTime());
            if (FAQUI.user_arr == null&&time > refresh_time) //第一次显示
            {
                List<MessageBoard> message_list = ConfigSystem.GetConfig<MessageBoardModel>().DataList;
                FAQUI.user_arr = message_list[0].user_name.Split(",");
                int user_index = PlayerPrefs.GetInt("user_index", 0);
                if (user_index + 10 >= FAQUI.user_arr.Length) user_index = 0;


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


                for (int i = user_index; i < user_index + 10; i++)
                {
                    FAQUI.list_user_arr.Add(FAQUI.user_arr[i]);
                }
                for (int i = faq_index; i < faq_index + 10; i++)
                {
                    FAQUI.list_content_arr.Add(FAQUI.content_arr[i]);
                }
                if (PlayerPrefs.GetInt("user_FAQindex", 0) < 10)
                {
                    if (!string.IsNullOrEmpty(PlayerPrefs.GetString("user_FAQcontent", "")))
                    {
                        FAQUI.list_user_arr.Insert(10 - PlayerPrefs.GetInt("user_FAQindex", 0), DataMgr.PlayerName.Value);
                        FAQUI.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);
            }
            
            if (time > refresh_time)
            {

                time = 0;

                int user_index = PlayerPrefs.GetInt("user_index", 0);
                Debug.Log(user_index);
                Debug.Log(FAQUI.user_arr);
                Debug.Log(FAQUI.user_arr.Length);

                if (user_index + 1 >= FAQUI.user_arr.Length) user_index = 0;



                int faq_index = PlayerPrefs.GetInt("faq_index", 0);
                if (faq_index + 1 >= FAQUI.content_arr.Length) faq_index = 0;


                FAQUI.list_user_arr.Add(FAQUI.user_arr[user_index]);
                FAQUI.list_content_arr.Add(FAQUI.content_arr[faq_index]);
                if (FAQUI.list_user_arr.Count > 50)
                {
                    FAQUI.list_user_arr.RemoveAt(0);
                }
                if (FAQUI.list_content_arr.Count > 50)
                {
                    FAQUI.list_content_arr.RemoveAt(0);
                }
                user_index++;
                faq_index++;
                PlayerPrefs.SetInt("user_FAQindex", PlayerPrefs.GetInt("user_FAQindex", 0) + 1);
                PlayerPrefs.SetInt("user_index", user_index);
                PlayerPrefs.SetInt("faq_index", faq_index);
                GameDispatcher.Instance.Dispatch(GameMsg.faq_refresh);

            }

        }
        #endregion
    }
}