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


using System;
using DG.Tweening;
using Spine.Unity;
using BingoBrain.Core;
using FGUI.ACommon;
using FairyGUI;
using BingoBrain.HotFix;
using System.Text.RegularExpressions;

namespace BingoBrain
{
    public class BroadcastUI : BaseUI
    {
        private BroadcastUICtrl ctrl;
        private BroadcastModel model;
        private FGUI.ACommon.com_broadcast1 ui;

        public BroadcastUI(BroadcastUICtrl ctrl) : base(ctrl)
        {
            uiName = UIConst.BroadcastUI;
            this.ctrl = ctrl;
        }

        protected override void SetUIInfo(UIInfo uiInfo)
        {
            uiInfo.packageName = "ACommon";
            uiInfo.assetName = "com_broadcast1";
            uiInfo.layerType = UILayerType.Normal;
            uiInfo.isNeedOpenAnim = false;
            uiInfo.isNeedCloseAnim = false;
            uiInfo.isNeedUIMask = false;
        }

        #region 生命周期
        protected override void OnInit()
        {

        }

        protected override void OnClose()
        {
        }

        protected override void OnBind()
        {
            ui = baseUI as FGUI.ACommon.com_broadcast1;
        }

        protected override void OnOpenBefore(object args)
        {
            broad_text = Regex.Replace(GameHelper.getDesByKey("makeup_4"), @"<img\s+[^>]*?/>", "<img src='ui://pmf3wbjidz5jpj6'/>");
            InitView();
            //ui.y = 241;

            // if (UI.Instance.IsExistUI(UIConst.RainPlayUI))
            // {
            //     Setbuttom();
            // }
            // else
            // {
            //     Settop();
            // }
            // ui.visible = false;
            // if (Screen.safeArea.y != 0)
            // {//刘海屏
            //     ui.y += Screen.safeArea.y;
            // }
            text_ui = ui.broad_cast_text;

            ui.btn_record.SetClick(OpenRecord);
            ui.broad_cast_text.btn_broad.SetClick(OpenRecord);

            if (ConfigSystem.GetConfig<exBrPoolModel>() == null) return;
            //  ConfigSystem.GetConfig<exBrPoolModel_2>().dataList[0]
            config_name_list = exBrPoolModel.config_name_list;
            config_money_list = exBrPoolModel.config_money_list;
            // GameDispatcher.Instance.AddListener(GameMsg.hideBroadCast, hideEvent);
            // GameDispatcher.Instance.AddListener(GameMsg.showBroadCast, showEvent);
            // UICtrlDispatcher.Instance.AddListener(UICtrlMsg.MainUI_Open, Settop);
            // UICtrlDispatcher.Instance.AddListener(UICtrlMsg.RainPlayUI_Open, Setbuttom);
        }

        protected override void OnOpen(object args)
        {
        }

        protected override void OnHide()
        {
        }

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

        #region 消息
        protected override void AddListener()
        {

        }
        protected override void RemoveListener()
        {

        }
        #endregion
        private int time_count = 11;
        private com_broadcast_text1 text_ui;
        private List<string> config_name_list;
        private List<string> config_money_list;
        private bool main_ui_show = true;
        //初始化页面逻辑

        private Action closeCallback = null;
        private void InitView()
        {
            Hall.Instance.UpdateSecondEvent += timeEvent;


            // var sk = FX.Instance.SetFx<SkeletonAnimation>(ui.broad_cast_text.bg_panel, Fx_Type.fx_broad,  closeCallback);
            // sk.state.SetAnimation(0, "animation", true);
            // FX.Instance.SetFx<SkeletonAnimation>(ui.broad_cast_text.bg_panel, Fx_Type.fx_broad, sk =>
            // {
            //     // ske_pot = sk;

            //     //   NAAVsa.AddCompleteEvent(cardSpine, OnChangeCardFinish);
            //     // OnChangeCardFinish(null);
            //     NAAVsa.PlayAnim(sk, "animation", true);
            // }, closeCallback);

            // ui.group_.y = 220;

            if (Screen.safeArea.y != 0)
            {//刘海屏
             // ui.group_.y += Screen.safeArea.y;
            }
        }

        private void OpenRecord()
        {
            UICtrlDispatcher.Instance.Dispatch(SkinInfo.RecordViewUI_Open);
        }
        private void hideEvent(object sender = null)
        {
            Debug.Log("hide");
            main_ui_show = false;
            ui.visible = false;
        }
        private void showEvent(object sender = null)
        {
            Debug.Log("show");
            main_ui_show = true;
            ui.visible = true;
        }
        private void Settop(object sender = null)
        {
            ui.group_.y = 241;
            if (Screen.safeArea.y != 0)
            {//刘海屏
                ui.group_.y += Screen.safeArea.y;
            }
        }
        private void Setbuttom(object sender = null)
        {
            ui.group_.y = GRoot.inst.height - 300;
        }
        string broad_text = "";
        void timeEvent()
        {
            time_count++;
            // if (time_count > 30)
            // {
            //     time_count = 0;
            //     var name_index = UnityEngine.Random.Range(0, config_name_list.Count);
            //     var money_index = UnityEngine.Random.Range(0, config_money_list.Count);
            //     if (main_ui_show)
            //     {
            //         ui.visible = true;
            //     }
            //     Debug.Log(text_ui.cast_text.text);
            //     text_ui.cast_text.text = String.Format(text_ui.hide_text.text, config_name_list[name_index],
            //         config_money_list[money_index], DateTimeBoardk.Instance.GetCurrDateTime());
            //     text_ui.cast_text.x = 686;
            //     Tweener tweener = DOTween.To(() => text_ui.cast_text.x,
            //            x => text_ui.cast_text.x = x, -1300, 10);

            // }

            // time_count = 0;
            //[color=#63f021][size=32]{0}[/size][/color] claimed <img src='ui://pmf3wbji112epi7'/>[color=#009900][size=36]{1} [/size][/color][color=#efc26b][size=22]{2}[/size][/color]

            //  Debug.Log(GameHelper.getDesByKey("makeup_4"));
            //Debug.Log($"barry  broad time count======== {time_count}");
            if (time_count > 20)
            {
                time_count = 0;
                var name_index = UnityEngine.Random.Range(0, config_name_list.Count);
                var money_index = UnityEngine.Random.Range(0, config_money_list.Count);
                Debug.Log(text_ui.cast_text.text);

                string name = config_name_list[name_index];
                if (name.Length > 4)
                {
                    name = name[..4] + "...";
                }
                // text_ui.hide_text.text = GameHelper.getDesByKey("makeup_4");



                string temp_text = String.Format(broad_text, name, GameHelper.Get102Str_new(decimal.Parse(config_money_list[money_index])), DateTimeBoardk.Instance.GetCurrDateTime());
                text_ui.cast_text.text = temp_text;
                ui.t0.Play();

                DOVirtual.DelayedCall(3.5f, () =>
                {
                    // Tweener tweener = DOTween.To(() => ui.broad.x,
                    //     x => ui.broad.x = x, 1080, 0.5f);
                    ui.t1.Play();
                });
                string str = name + "-" + config_money_list[money_index] + "-" + DateTimeBoardk.Instance.GetCurrDateTime();
                GameDispatcher.Instance.Dispatch(BingoInfo.updateRecordList, str);

            }

        }
    }
}