using System;
using DG.Tweening;
using FairyGUI;
using FGUI.bkg_reward_new;
using UnityEngine;

namespace ChillConnect
{
    public class RewardMulNewUI : BaseUI
    {
        private RewardMulNewUICtrl ctrl;
        private RewardMulNewModel model;
        private com_reward_mul ui;
        private Action closeCallback;
        private GiftData rewardInfo;
        bool hasCoin;
        decimal m_coinReward;
        public RewardMulNewUI(RewardMulNewUICtrl ctrl) : base(ctrl)
        {
            uiName = UIConst.RewardMulNewUI;
            this.ctrl = ctrl;
        }

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


        protected override void OnInit()
        {
           
        }

        protected override void OnClose()
        {
      
        }

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

        protected override void OnOpenBefore(object args)
        {
            rewardInfo = (GiftData)args;
            InitView();
        }

        protected override void OnOpen(object args)
        {
 
        }



        private void InitView()
        {
          //  AudioManager.Instance.PlayDynamicEffect(AudioConst.reward_show);
            ui.node_coin.visible = false;
            ui.node_undo.visible = false;
            ui.node_move_out.visible = false;
            ui.node_refresh.visible = false;
            ui.add_undo.visible = false;
            ui.add_move_out.visible = false;
            ui.add_refresh.visible = false;

            if (GameHelper.IsGiftSwitch() && ConfigSystem.GetConfig<CommonModel>().PiggyBankSwitch == 1) {
                (ui.btn_mul.GetChild("img_saveingpot") as GImage).visible = true;
            } else {
                (ui.btn_mul.GetChild("img_saveingpot") as GImage).visible = false;
            }

            
            for (int i = 0; i < rewardInfo.type.Count; i++) {
                if (rewardInfo.type[i] == (int)GiftType.coin) {
                    hasCoin = true;
                    ui.node_coin.visible = true;
                    ui.node_coin.GetChild("lab_num").text = "x" + rewardInfo.reward[i];
                } else if (rewardInfo.type[i] == (int)GiftType.undo) {
                    ui.node_undo.visible = true;
                    ui.node_undo.GetChild("lab_num").text = "x" + rewardInfo.reward[i];
                    ui.add_undo.visible = true;
                    ui.add_undo.GetChild("lab_num").text = "x" + rewardInfo.reward[i];
                } else if (rewardInfo.type[i] == (int)GiftType.move_out) {
                    ui.node_move_out.visible = true;
                    ui.node_move_out.GetChild("lab_num").text = "x" + rewardInfo.reward[i];
                    ui.add_move_out.visible = true;
                    ui.add_move_out.GetChild("lab_num").text = "x" + rewardInfo.reward[i];
                } else if (rewardInfo.type[i] == (int)GiftType.refresh) {
                    ui.node_refresh.visible = true;
                    ui.node_refresh.GetChild("lab_num").text = "x" + rewardInfo.reward[i];
                    ui.add_refresh.visible = true;
                    ui.add_refresh.GetChild("lab_num").text = "x" + rewardInfo.reward[i];
                }
            }

            ui.btn_mul.SetClick(() => {
                GameHelper.ShowVideoAd("reward_lobby", isSuccess =>
                {
                    if (isSuccess)
                    {
                        GameHelper.ShowTips("Successfully claimed");

                        getReward(2);
                        playCoinAni();

                        ui.ani_group.visible = true;
                        ui.t0.Play(() => {
                            ui.ani_group.visible = false;
                        });
                    }
                });
            });

            ui.btn_claim.SetClick(() =>
            {
                ui.btn_claim.SetClick(() => { });
                ui.btn_mul.SetClick(() => { });
                getReward();
                playCoinAni();
                DOVirtual.DelayedCall(2f, () =>
                {
                    GameHelper.addInterAdnumber();
                });

                ui.ani_group.visible = true;
                ui.t0.Play(() => {
                    ui.ani_group.visible = false;
                });
            });
        }

        private void getReward(int rate = 1) {
            for (int i = 0; i < rewardInfo.type.Count; i++) {
                if (rewardInfo.type[i] == (int)GiftType.coin || rewardInfo.type[i] == 101) {
                    m_coinReward = rewardInfo.reward[i] * rate;
                } else if (rewardInfo.type[i] == (int)GiftType.undo) {
                    int num = GameHelper.GetItemNumber(1) + rewardInfo.reward[i] * rate;
                    GameHelper.SetItemNumber(1, num);
                    ui.add_undo.GetChild("lab_num").text = "x" + rewardInfo.reward[i] * rate;
                } else if (rewardInfo.type[i] == (int)GiftType.move_out) {
                    int num = GameHelper.GetItemNumber(0) + rewardInfo.reward[i] * rate;
                    GameHelper.SetItemNumber(0, num);
                    ui.add_move_out.GetChild("lab_num").text = "x" + rewardInfo.reward[i] * rate;
                } else if (rewardInfo.type[i] == (int)GiftType.refresh) {
                    int num = GameHelper.GetItemNumber(2) + rewardInfo.reward[i] * rate;
                    GameHelper.SetItemNumber(2, num);
                    ui.add_refresh.GetChild("lab_num").text = "x" + rewardInfo.reward[i] * rate;
                }
            }
        }

        private bool isget;
        private void playCoinAni()
        {
            if (!hasCoin) {
                GameDispatcher.Instance.Dispatch(GameMsg.rewardMul_close);
                CtrlCloseUI();
                return;
            }

            if(isget) return;
            isget=true;
            var rewardData = new RewardData();

            int curr_type = 101;
            Vector2 end_ = GameHelper.GetUICenterPosition(ui.node_end, true);


            var rewardSingleData = new RewardSingleData(curr_type, m_coinReward, RewardOrigin.Play)
            {
                startPosition = GameHelper.GetUICenterPosition(ui.btn_mul, true),
                endPosition = new Vector2(end_.x + 30, end_.y + 30),
            };

            rewardData.AddReward(rewardSingleData);
            rewardData.displayType = RewardDisplayType.RewardFly | RewardDisplayType.ValueChange;
            rewardData.AddCompleted(success =>
            {
                if (success)
                {
                    DOVirtual.DelayedCall(0.7f, () =>
                    {
                        GameDispatcher.Instance.Dispatch(GameMsg.Gold_refresh);
                        GameDispatcher.Instance.Dispatch(GameMsg.Sheep_item_refresh);
                        GameDispatcher.Instance.Dispatch(GameMsg.rewardMul_close);
                        CtrlCloseUI();
                    });
                }
            });

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