using System;
using DG.Tweening;
using FGUI.Lobby_03;
using Spine.Unity;
using UnityEngine;
// using FGUI.G002_main;

namespace ChillConnect
{
    public class FirstRewardUI : BaseUI
    {
        private FirstRewardUICtrl ctrl;
        private FirstRewardModel model;
        private com_first_award ui;
        private Action closeCallback;
        private Action open_sign;
        private bool is_get;
        public FirstRewardUI(FirstRewardUICtrl ctrl) : base(ctrl)
        {
            uiName = UIConst.FirstRewardUI;
            this.ctrl = ctrl;
        }

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

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

        protected override void OnClose()
        {
            closeCallback();
            //open_sign();
        }

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

        protected override void OnOpenBefore(object args)
        {
            if (Screen.safeArea.y != 0)
            {//刘海屏
                ui.end_point.y += 68;
            }
            InitView();
        }

        protected override void OnOpen(object args)
        {
            //open_sign = (System.Action)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()
        {
            //ui.money.GetChild("text_gold").text = $"{DataMgr.Coin.Value:N0}";

            ui.text_num.text = GameHelper.Get102Str(ConfigSystem.GetConfig<CommonModel>().InitialNum);
            var sk = FXManager.Instance.SetFx<SkeletonAnimation>(ui.spine_parent, Fx_Type.fx_first_reward, ref closeCallback);
            sk.state.SetAnimation(0, "newbie_reward_an01", false);
            sk.state.Complete += a =>
            {
                sk.state.SetAnimation(0, "newbie_reward_an02", true);
                ui.btn_getreward.SetClick(() =>
                {
                    ui.btn_getreward.visible = false;
                    sk.state.SetAnimation(0, "newbie_reward_an03", false);
                    sk.state.Complete += a =>
                    {
                        ui.state.selectedIndex = com_first_award.State_page;
                    };
                });

            };

            ui.btn_get.SetClick(GetReward);
            //  ui.btn_close.SetClick(GetReward);
        }
        private void GetReward()
        {
            if (is_get) return;
            is_get = true;


            var start = GameHelper.GetUICenterPosition(ui.img_gold);
            var end = GameHelper.GetUICenterPosition(ui.end_point);

            int awardNum = ConfigSystem.GetConfig<CommonModel>().InitialNum;

            var rewardData = new RewardData();

            var rewardSingleData = new RewardSingleData(102, awardNum, RewardOrigin.AdTask)
            {
                startPosition = start,
                endPosition = new Vector2(end.x - 110, end.y - 110)
            };

            rewardData.AddReward(rewardSingleData);
            rewardData.displayType = RewardDisplayType.RewardFly | RewardDisplayType.ValueChange;
            rewardData.AddCompleted(success =>
            {
                if (success)
                {
                    GameHelper.AddGoldNumber(50);

                    //var startNum = DataMgr.Coin.Value - awardNum;
                    // DOVirtual.Float((float)startNum, (float)DataMgr.Coin.Value, 1,
                    //     value => { ui.money.GetChild("text_gold").text = GameHelper.Get101Str((decimal)value); });
                    SaveData.GetSaveObject().IsGetFirstReward = true;
                    SaveData.saveDataFunc();

                    DOVirtual.DelayedCall(1f, () =>
                    {
                        UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.FirstRewardUI_Close);
                        GameDispatcher.Instance.Dispatch(GameMsg.Update101);
                    });
                }
            });
            GameDispatcher.Instance.Dispatch(GameMsg.GetReward, rewardData);

        }
    }
}
