
using FGUI.Common_01;
using UnityEngine;
using FairyGUI;
using IgnoreOPS;
using System.Collections.Generic;
using DG.Tweening;

namespace ChillConnect
{
    public class GoldRewardUI : BaseUI
    {
        private GoldRewardUICtrl ctrl;
        private GoldRewardModel model;
        private FGUI.ZM_AddCell_12.com_goldreward ui;

        public GoldRewardUI(GoldRewardUICtrl ctrl) : base(ctrl)
        {
            uiName = UIConst.GoldRewardUI;
            this.ctrl = ctrl;
        }

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

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

        protected override void OnClose()
        {
        }

        protected override void OnBind()
        {
            ui = baseUI as FGUI.ZM_AddCell_12.com_goldreward;
        }

        protected override void OnOpenBefore(object args)
        {
            turn_list = ConfigSystem.GetConfig<TurnOffRewardsModel>().DataList;
            InitView();
            ui.btn_close.SetClick(() =>
            {
                GameHelper.addInterAdnumber();
                CtrlCloseUI();
            });
        }
        private List<TurnOffRewards> turn_list;
        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()
        {
            int weight_all = 0;
            float max_gold = 0;
            for (int i = 0; i < turn_list.Count; i++)
            {
                weight_all += turn_list[i].weight;
                if (turn_list[i].quantity > max_gold)
                {
                    max_gold = turn_list[i].quantity;
                }
            }
            int random_ = Random.Range(0, weight_all);
            int int_ = 0;


            TurnOffRewards reward = null;

            for (int i = 0; i < turn_list.Count; i++)
            {
                int_ += turn_list[i].weight;
                if (random_ < int_)
                {
                    reward = turn_list[i];
                    break;
                }
            }
            if (reward == null) reward = turn_list[0];
            ui.text_goldnum.text = max_gold.ToString();
            if (GameHelper.IsGiftSwitch() && ConfigSystem.GetConfig<CommonModel>().PiggyBankSwitch == 1)
            {
                (ui.btn_receive.GetChild("img_saveingpot") as GImage).visible = true;
            }
            ui.btn_receive.SetClick(() =>
            {
                GameHelper.ShowVideoAd("TurnOffRewards", isSuccess =>
                {
                    if (isSuccess)
                    {
                        ui.btn_receive.SetClick(() => { });
                        decimal gold = (decimal)reward.quantity;
                        var start = GameHelper.GetUICenterPosition(ui.text_goldnum);
                        var end = GameHelper.GetUICenterPosition(ui.end_point);

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

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


                    }
                });
            });

        }
    }
}