using System;
using System.Collections;
using System.Collections.Generic;
using BingoBrain.Core;
using BingoBrain.HotFix;
using DG.Tweening;
using FairyGUI;
using FGUI.ACommon;
using FGUI.bkg_jilu;
using NSubstitute.ReturnsExtensions;
using UnityEngine;

namespace BingoBrain
{
    public class RecordViewUI : BaseUI
    {
        public RecordViewUICtrl ctrl;
        public RecordViewModel model;
        private FGUI.bkg_jilu.com_jilu ui;

        public RecordViewUI(RecordViewUICtrl ctrl) : base(ctrl)
        {
            uiName = UIConst.RecordViewUI;
            this.ctrl = ctrl;
        }

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

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

        protected override void OnClose()
        {
            if (coroutine != null)
            {
                IsfvKit.StopCoroutine(coroutine);
            }
        }

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

        protected override void OnOpenBefore(object args)
        {
            InitView();

            ui.btn_close.onClick.Add(() =>
            {
                CtrlCloseUI();
            });
        }

        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 bool isroll;

        private List<GObject> list_panel = new List<GObject>();

        // 初始化页面逻辑
        private void InitView()
        {
            for (int i = 0; i < 9; i++)
            {
                GObject item = ui.panel.GetChild("item" + i);
                list_panel.Add(item);
            }

            UpdateRecord();
        }

        private Coroutine coroutine;
        public void UpdateRecord()
        {
            if (ctrl.model == null || ctrl.model.recordList.Count == 0) return;

            for (int i = 0; i < ctrl.model.recordList.Count; i++)
            {
                if (i < list_panel.Count)
                {
                    setRecord(i, list_panel[i]);
                    list_panel[i].visible = true;
                }
            }

            if (ctrl.model.recordList.Count >= 9 && !isroll) // 注意这里应该是 9，因为 list_panel 只有 9 个元素
            {
                isroll = true;
                coroutine = IsfvKit.StartCoroutine(ScrollRoutine());
            }
        }

        private void setRecord(int index, GObject obj)
        {
            com_record_item item = (com_record_item)obj;
            string[] values = ctrl.model.recordList[index].Split('-');

            item.name.text = values[0];

            item.num.text = GameHelper.getChString(decimal.Parse(values[1]));

            if (GameHelper.isRDExchangeMode())
            {
                string str = "<img src='ui://pmf3wbjidz5jpj6'/>";
                item.num.text = str + $" {decimal.Parse(values[1]):N}";
            }
            else
            {
                item.num.text = $"${decimal.Parse(values[1]):N}";

            }
            string[] parts = values[2].Split(" ");
            item.time.text = parts[0];
            item.lab_claim.text = GameHelper.getDesByKey("ww");
        }

        private IEnumerator ScrollRoutine()
        {
            while (true)
            {
                int panelCount = list_panel.Count; // 缓存 list_panel 的长度
                for (int i = 0; i < panelCount; i++)
                {
                    if (i < list_panel.Count) // 再次检查索引是否有效
                    {
                        float targetY = list_panel[i].y - 110;
                        list_panel[i].MoveTween(targetY);
                    }
                }

                yield return new WaitForSeconds(1f); // 每隔1秒滚动一次
            }
        }

        private void hideEvent(object sender = null)
        {

        }

        private void showEvent(object sender = null)
        {

        }
    }

    // 定义一个非泛型静态类来包含扩展方法
    public static class GObjectMoveFunc
    {
        public static void MoveTween(this GObject obj, float targetY, Action action = null)
        {
            obj.TweenMove(new Vector2(obj.x, targetY), 0.5f)
               .SetEase((EaseType)Ease.Linear)
               .OnComplete(() =>
               {
                   if (targetY <= -16)
                   {
                       // 第一个 item 放到最后一个位置
                       obj.y = 890;
                   }
               });
        }
    }
}
