// using FGUI.A000_common;

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

namespace ChillConnect
{
    public class SignInViewUI : BaseUI
    {
        private SignInViewUICtrl ctrl;
        private SignInViewModel model;
        private SignDailyRewardModel signModel;

        private com_sign_in ui;

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

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

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

        }

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

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

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

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

            }
            var anim_paly = FXManager.Instance.SetFx<SkeletonAnimation>(ui.com_gold.GetChild("gold_anim") as GGraph, Fx_Type.fx_coin, ref closeCallback);
            anim_paly.state.SetAnimation(0, "animation", true);

            signBtnList.Add(ui.day1);
            signBtnList.Add(ui.day2);
            signBtnList.Add(ui.day3);
            signBtnList.Add(ui.day4);
            signBtnList.Add(ui.day5);
            signBtnList.Add(ui.day6);
            signBtnList.Add(ui.day7);

            signModel = ConfigSystem.GetConfig<SignDailyRewardModel>();

            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 InitView()
        {

            var signList = signModel.DataList;

            // Debug.Log($"signList.Count:{signList.Count}");
            for (int i = 0; i < signList.Count; i++)
            {
                if (i < 7)
                {
                    RefreshView(signBtnList[i], signList[i], i);
                }
            }

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

        private bool _haveReward = false;
        private void RefreshView(btn_sign btnSign, SignDailyReward reward, int index)
        {
            btnSign.title = "0" + reward.id;
            btnSign.reward_num.text = GameHelper.Get101Str(reward.quantity[0]);

            btnSign.day.selectedIndex = index != 6 ? 0 : 1;

            if (index == 6)
            {
                btnSign.reward_num1.text = GameHelper.Get101Str(reward.quantity[1]);
                setItemUrl(btnSign.loader_icon0, reward.rewards_type[1]);
            }
            btnSign.anim_sign.visible = false;

            var signDays = DataMgr.SignState.Value.Count;
            var isToday = true;
            if (signDays > 0)
                isToday = GameHelper.InToday(DataMgr.SignState.Value[signDays - 1], 0, true);

            if (index < signDays)
            {
                btnSign.status.selectedIndex = 1;
            }
            else if (index == signDays)
            {
                if (signDays > 0 && isToday)
                {

                    btnSign.status.selectedIndex = 0;
                }
                else
                {
                    _haveReward = true;
                    string names = index != 6 ? "small" : "big";
                    btnSign.anim_sign.visible = true;
                    var animPlay = FXManager.Instance.SetFx<SkeletonAnimation>(btnSign.GetChild("anim_sign") as GGraph, Fx_Type.fx_sign1, ref closeCallback);
                    animPlay.state.SetAnimation(0, names, true);

                    btnSign.status.selectedIndex = 2;
                    btnSign.isCloseClickAnim = true;

                    ui.btn_collect.SetClick(() =>
                    {
                        _haveReward = false;

                        ui.btn_collect.enabled = _haveReward;
                        var startPos = GameHelper.GetUICenterPosition(btnSign);
                        btnSign.status.selectedIndex = 1;
                        ui.btn_collect.onClick.Clear();
                        DOVirtual.DelayedCall(1f, () =>
                        {
                            GetSignInReward(reward, startPos, btnSign, index);
                        });

                    });
                }
            }
            else
            {
                btnSign.status.selectedIndex = 0;
            }

            ui.btn_collect.enabled = _haveReward;
        }
        void setItemUrl(GLoader loader, int urltype)
        {
            if (urltype == 0) loader.url = "ui://ZM_Common_01/cards_gold";  //jinbi
            else if (urltype == 1) loader.url = "ui://Common_01/spin_remove";   //yichu
            else if (urltype == 2) loader.url = "ui://Common_01/spin_undo";   //chehui
            else if (urltype == 3) loader.url = "ui://Common_01/spin_refresh";  //shuaxin
        }
        private Action closeCallback;

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

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

                };
                rewardModel.AddReward(rewardData);
            }
            rewardModel.sign_in_days = sign_day;
            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();

                    btnDay.onClick.Clear();
                    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);


                    // CtrlCloseUI();
                }
            });

            GameDispatcher.Instance.Dispatch(GameMsg.GetReward, rewardModel);
            // ui.visible = false;
        }

    }
}
