using System;
using DG.Tweening;
using FairyGUI;
using FGUI.Lobby_03;
using IgnoreOPS;
using UnityEngine;

namespace ChillConnect
{

    public class LevelRewardUI : BaseUI
    {
        private LevelRewardUICtrl ctrl;
        private LevelRewardModel model;
        private com_level_reward ui;

        protected GProgressBar pb_loading;
        private Tweener tweener;


        protected int currValue;

        public LevelRewardUI(LevelRewardUICtrl ctrl) : base(ctrl)
        {
            uiName = UIConst.LevelRewardUI;
            this.ctrl = ctrl;
        }

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

        #region 生命周期

        protected override void OnInit()
        {
        }

        protected override void OnClose()
        {
        }

        protected override void OnBind()
        {
            ui = baseUI as com_level_reward;
        }
        private Action closeCallback = null;

        protected override void OnOpenBefore(object args)
        {
            int gold = 0;

            if (args == null)
            {
                return;
            }

            gold = (int)args;
            ui.text_reward.text = gold.ToString();
            ui.btn_get.SetClick(() =>
            {
                getReward(gold);
            });

            ui.btn_close.SetClick(() =>
            {
                getReward(gold);

            });
        }

        private void getReward(int gold)
        {
            var start = GameHelper.GetUICenterPosition(ui.btn_get);
            var end = GameHelper.GetUICenterPosition(ui.btn_gold.GetChild("text_gold"));

            var rewardData = new RewardData();
            var rewardSingleData = new RewardSingleData(101, gold, RewardOrigin.AdTask)
            {
                startPosition = start,
                endPosition = new Vector2(end.x - 110, end.y - 110)
            };
            rewardData.AddReward(rewardSingleData);
            rewardData.displayType = RewardDisplayType.RewardFly | RewardDisplayType.ValueChange;
            rewardData.AddCompleted(isScu =>
            {
                DOVirtual.DelayedCall(0.2f, () =>
                                {
                                    var startNum = DataMgr.Coin.Value - gold;
                                    DOVirtual.Float(startNum, GameHelper.GetGoldNumber(), 1f,
                            value => { ui.btn_gold.GetChild("text_gold").text = GameHelper.Get101Str((decimal)value); });
                                });


            });

            GameDispatcher.Instance.Dispatch(GameMsg.GetReward, rewardData);

            CtrlCloseUI();
        }

        protected override void OnOpen(object args)
        {
        }

        #endregion


    }
}