using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using FutureCore;
using FairyGUI;
using FGUI.ZM_Common_01;
using System;
using DG.Tweening;
using FGUI.bkg_jilu;
using SGModule.Common.Extensions;
using Unity.VisualScripting;

namespace RedHotRoast
{
    public class RecordViewUI : BaseUI
    {
        private RecordViewUICtrl ctrl;
        private 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 = true;
            uiInfo.isNeedCloseAnim = true;
            uiInfo.isNeedUIMask = true;
        }

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

        }

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

        protected override void OnBind()
        {
            ui = baseUI as FGUI.bkg_jilu.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 = false;

        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 = null;
        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 = CrazyAsyKit.StartCoroutine(ScrollRoutine());
            }
        }
        
        private (string key, int value) GetVipLevelFromName(string name)
        {
            switch (name)
            {
                case "Gold":
                    return ("G.VIP", 1);
                case "Platinum":
                    return ("P.VIP", 2);
                case "Diamond":
                    return ("D.VIP", 3);
                default:
                    return (string.Empty, 0);
            }
        }


        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];
            var (key, value) = GetVipLevelFromName(values[1]);
            item.num.text = key; // 假设 money 是显示金额的字段
            string[] parts = values[2].Split(" ");
            item.time.text = parts[0];
            
            item.icon_loader.url = $"ui://LG_Common/vip_{value}";
        }

        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 - 129;
                        GObjectMoveFunc.MoveTween(list_panel[i], 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 = 1048;
                   }
               });
        }
    }
}