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

namespace ScrewsMaster
{
    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>();

        private GTextField _goldNumText;

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

        protected override void SetUIInfo(UIInfo uiInfo)
        {
            uiInfo.packageName = "G014_SignIn";
            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.G014_SignIn.com_sign_in;
        }

        protected override void OnOpenBefore(object args)
        {
            
            if (Screen.safeArea.y != 0)
            {
                ui.gold.y += Screen.safeArea.y;    
            }
            
            _goldNumText = ui.gold.GetChild("number_text") as GTextField;
            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;

            for (int i = 0; i < signList.Count; i++)
            {
                if (i < 7)
                {
                    RefreshView(signBtnList[i], signList[i], i);
                }
            }


            ui.close.SetClick(CtrlCloseUI);
            _goldNumText.text = GameHelper.GetGoldNumber().ToString();
        }

        private void RefreshView(btn_sign btnSign, SignDailyReward reward, int index)
        {
            btnSign.TitleStyle_1.text = $"Day {index + 1}";
            btnSign.TitleStyle_2.text = $"Day {index + 1}";
            btnSign.reward_num.text = reward.quantity[0] + "";
            
            // sk.SetActive(false);

            var signDays = PreferencesMgr.Instance.SignState.Count;
            var isToday = true;
            if (signDays > 0)
                isToday = GameHelper.InToday(PreferencesMgr.Instance.SignState[signDays - 1], 0, true);
            
            if (index < signDays)
            {
                btnSign.State.selectedIndex = btn_sign.State_Received;
            }
            else if (index == signDays)
            {
                if (signDays > 0 && isToday)
                {
                    btnSign.State.selectedIndex = btn_sign.State_Unreceived;
                }
                else
                {
                    btnSign.State.selectedIndex = btn_sign.State_Current;
                    SkeletonAnimation sk = FXManager.Instance.SetFx<SkeletonAnimation>(btnSign.GetChild("fx_parent") as GGraph, Fx_Type.fx_signin, ref closeCallback);
                    
                    sk.state.SetAnimation(0, index >= 6 ? "big" : "small", true);
                    btnSign.isCloseClickAnim = true;

                    btnSign.SetClick(() =>
                    {
                        var startPos = GameHelper.GetUICenterPosition(btnSign);
                        btnSign.State.selectedIndex = btn_sign.State_Received;
                        btnSign.onClick.Clear();
                        btnSign.touchable = false;
                        sk.SetActive(false);

                        GetSignInReward(reward, startPos, btnSign);
                    });
                }
            }
            else
            {
                btnSign.State.selectedIndex = btn_sign.State_Unreceived;
            }
            
            btnSign.Style.selectedIndex = index >= 6 ? btn_sign.Style_2 : btn_sign.Style_1;
        }

        private Action closeCallback;

        private void GetSignInReward(SignDailyReward vo, Vector2 startPos, GButton btnDay)
        {
            var curGoldNum = GameHelper.GetGoldNumber();
            var rewardModel = new RewardData();
            for (var i = 0; i < vo.item1.Length; i++)
            {
                var rewardData = new RewardSingleData(101, vo.quantity[i], RewardOrigin.SignIn)
                {
                    startPosition = startPos,
                    endPosition = GameHelper.GetUICenterPosition(_goldNumText),

                };
                rewardModel.AddReward(rewardData);
            }

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

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

            rewardModel.AddCompleted(isSuccess =>
            {
                if (isSuccess)
                {
                    PreferencesMgr.Instance.SignState.Add(GameHelper.GetNowTime());
                    PreferencesMgr.Instance.SaveSignState();
                    
                    btnDay.onClick.Clear();
                    AudioManager.Instance.PlayDynamicEffect(AudioConst.DailyBonusCollect);

                    DOVirtual.Float(curGoldNum, GameHelper.GetGoldNumber(), 1f,
                        value => { _goldNumText.text = ((int)value).ToString(); });//金币变化动画

                    // 用来刷新todo界面的一些信息，如红点等 
                    // GameDispatcher.Instance.Dispatch(GameMsg.UpdateTodoView);


                    // CtrlCloseUI();
                }
                else
                {
                    GameHelper.ShowTips("Not time to check in yet");
                    
                }
            });

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