// using FGUI.A000_common;

using System;
using System.Collections.Generic;
using DG.Tweening;
using FairyGUI;
using FGUI.ZM_Sign_06;
using Spine.Unity;
using UnityEngine;

namespace RedHotRoast
{
    public class SignInViewUI : BaseUI
    {
        private SignInViewUICtrl ctrl;
        private SignInViewModel model;
        private FGUI.ZM_Sign_06.com_sign_in ui;

        private int _signNum = 15;

        public SignInViewUI(SignInViewUICtrl ctrl) : base(ctrl)
        {
            uiName = UIConst.SignInViewUI;
            this.ctrl = ctrl;
        }

        protected override void SetUIInfo(UIInfo uiInfo)
        {
            uiInfo.packageName = "ZM_Sign_06";
            uiInfo.assetName = "com_sign_in";
            uiInfo.layerType = UILayerType.Popup;
            uiInfo.isNeedOpenAnim = true;
            uiInfo.isNeedCloseAnim = true;
            uiInfo.isNeedUIMask = true;

        }

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

        protected override void OnClose()
        {
            GameHelper.showGameUI = true;

        }

        protected override void OnBind()
        {
            ui = baseUI as FGUI.ZM_Sign_06.com_sign_in;
        }

        private List<btn_sign> btnSignList = new List<btn_sign>();


        protected override void OnOpenBefore(object args)
        {
            if (Screen.safeArea.y != 0)
            {
                ui.com_gold.y += Screen.safeArea.y;
            }

            for (var i = 0; i < _signNum; i++)
            {
                btnSignList.Add(ui.GetChild($"btn_{i+1}") as btn_sign);
            }

            InitView();
        }

        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 void SetListHasAnimation(List<SkeletonAnimation> animList, int index, GGraph parent, Fx_Type fxType)
        {
            int realIndex = index;

            // 扩展列表到目标索引
            while (realIndex >= animList.Count)
            {
                animList.Add(null);
            }

            // 如果目标位置没有动画，则设置
            if (animList[realIndex] == null)
            {
                animList[realIndex] = FXManager.Instance.SetFx<SkeletonAnimation>(parent, fxType, ref closeCallback);
            }
        }

        //初始化页面逻辑
        private readonly List<SignDailyReward> signList = ConfigSystem.GetConfig<SignDailyReward>();
         private List<SkeletonAnimation> sign_anims = new List<SkeletonAnimation>();

        private void InitView()
        {

            Debug.Log($"signList.Count:{signList.Count}");

            for (var i = 0; i < _signNum; i++)
            {
                RefreshView(i);
            }

            ui.com_gold.GetChild("text_gold").text = $"{DataMgr.Coin.Value:N0}";
            ui.close.SetClick(CtrlCloseUI);
        }

        private void RefreshView(int index)
        {
            Debug.Log($" index ========== :{index}");
            btn_sign btnSign = btnSignList[index];;
            btnSign.state.selectedIndex = 0;
            btnSign.title = signList[index].id + "";
            
            btnSign.touchable = false;
            btnSign.reward_num.text = GameHelper.Get101Str(signList[index].quantity[0]);

            var animName = "appear2";
            if (index % 5 == 4)
            {
                btnSign.state.selectedIndex = 2;
            }

            var signDays = DataMgr.SignState.Value.Count;
            var isToday = true;
            if (signDays > 0)
                isToday = GameHelper.InToday(DataMgr.SignState.Value[signDays - 1], 0, true);
            
            // ui.btn_collect.state.selectedIndex = index == signDays && !(signDays > 0 && isToday)? 0 : 1;
            
            
            if (index < signDays)
            {
                btnSign.state.selectedIndex = 3;
            }
            else if (index == signDays)
            {
                ui.btn_collect.state.selectedIndex = 1;
                if (signDays > 0 && isToday)
                {

                    // btnSign.status.selectedIndex = btn_sign.Status_reward;
                }
                else
                {
                    btnSign.state.selectedIndex = 1;
                    ui.btn_collect.state.selectedIndex = 0;
                    

                    btnSign.isCloseClickAnim = true;
                    // btnSign.status.selectedIndex = btn_sign.State_today;

                    ui.btn_collect.SetClick(() =>
                    {
                        SetBtnClick(index);   
                    });
                    // btnSign.SetClick(() =>
                    // {
                    //
                    //     
                    // });
                }
            }
            else
            {
                // btnSign.status.selectedIndex = btn_sign.Status_reward;
            }
        }

        private void SetBtnClick(int index)
        {
            ui.btn_collect.onClick.Clear();
            ui.btn_collect.touchable = false;
            var startPos = GameHelper.GetUICenterPosition( ui.btn_collect);
            DOVirtual.DelayedCall(0.3f, () =>
            {
                ui.btn_collect.state.selectedIndex = 1;
                GetSignInReward(signList[index], startPos, ui.btn_collect,index);
            });
        }

        private Action closeCallback;

        private void GetSignInReward(SignDailyReward vo, Vector2 startPos, GButton btnDay,int index)
        {

            GameHelper.addInterAdnumber();

            var rewardModel = new RewardData();
            for (var i = 0; i < vo.item1.Length; i++)
            {
                var rewardData = new RewardSingleData(101, vo.quantity[i], RewardOrigin.SignIn,index)
                {
                    startPosition = startPos,
                    endPosition = new Vector2(ui.com_gold.x - 150, ui.com_gold.y - 136)

                };
                rewardModel.AddReward(rewardData);
            }

            if (vo.is_com_reward)
                rewardModel.displayType = RewardDisplayType.RewardFly | RewardDisplayType.Dialog |
                                          RewardDisplayType.ValueChange;
            else
                rewardModel.displayType = RewardDisplayType.RewardFly | RewardDisplayType.ValueChange;

            if (vo.is_double) rewardModel.condition = RewardCondition.None;

            rewardModel.AddCompleted(isSuccess =>
            {
                if (isSuccess)
                {
                    DataMgr.SignState.Value.Add(GameHelper.GetNowTime());
                    DataMgr.SignState.Save();

                    InitView();
                    
                    AudioManager.Instance.PlayDynamicEffect(AudioConst.DailyBonusCollect);

                    // 用来刷新todo界面的一些信息，如红点等
                    // GameDispatcher.Instance.Dispatch(GameMsg.UpdateTodoView);
                    CommonHelper.ShowNumAnim(ui.com_gold.GetChild("text_gold") as GTextField, vo.quantity[0], 101);
                    GameDispatcher.Instance.Dispatch(GameMsg.RefreshRedDot);

                    // CtrlCloseUI();
                }
            });

            GameDispatcher.Instance.Dispatch(GameMsg.GetReward, rewardModel);
        }
    }
}
