using Spine.Unity;

namespace ScrewsMaster
{
    using System;
    using System.Linq;
    using DG.Tweening;
    using FairyGUI;

    public class RewardUI : BaseUI
    {
        private RewardUICtrl ctrl;
        private RewardModel model;
        private FGUI.G008_reward.com_reward ui;

        private bool isMultiple = false;
        private Action closeCallback;

        public RewardUI(RewardUICtrl ctrl) : base(ctrl)
        {
            uiName = UIConst.RewardUI;
            this.ctrl = ctrl;
        }

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

        protected override void OnInit()
        {
            model = moduleManager.GetModel(ModelConst.RewardModel) as RewardModel;
        }

        protected override void OnClose()
        {
            tween?.Kill();
            CommonHelper.FadeOut(ui);
        }

        protected override void OnBind()
        {
            ui = baseUI as FGUI.G008_reward.com_reward;
        }

        protected override void OnOpenBefore(object args)
        {
            if (args != null)
            {
                model.rewardInfo = (RewardData)args;
            }

            InitData();
            InitView();
        }

        protected override void OnOpen(object args)
        {
            CommonHelper.FadeIn(ui);
            if (GetRewardOrigin() == RewardOrigin.H5Icon)
            {
                uiMask.touchable = false;
            }
        }

        private void InitData()
        {
            foreach (var rewardSingleData in model.rewardInfo.GetRewardDataList()
                         .Where(rewardSingleData => rewardSingleData.multiRate == 1))
            {
                rewardSingleData.multiRate = rewardSingleData.origin == RewardOrigin.Play
                    ? GameHelper.GetQuizRate()
                    : GameHelper.GetVideoRate();
            }
        }

        private void InitView()
        {
            AudioManager.Instance.PlayDynamicEffect("reward_show");
            if (GetRewardOrigin() == RewardOrigin.H5Icon)
            {
                uiMask.touchable = false;
            }

            var value = GetRewardValue();
            ui.text_num.text = GameHelper.Get102Str(value);

            DOVirtual.DelayedCall(0.2f, () =>
            {
                var gamwin =
                    FXManager.Instance.SetFx<SkeletonAnimation>(ui.fx_place, Fx_Type.gamwin, ref closeCallback);
                gamwin.state.SetAnimation(0, "out", true);
            });

            ui.btn_nor.SetClick(OnClickCollect);
            ui.btn_collect.SetClick(OnClickCollect);
 if (GameHelper.IsGiftSwitch() && ConfigSystem.GetConfig<CommonModel>().PiggyBankSwitch == 1) (ui.btn_multi.GetChild("img_saveingpot") as GImage).visible = true;
            SetName();
            SetAvatar();
            Set101();
        }

        private Tween tween;

        public void Set101(decimal cash = -1)
        {
            if (cash < 0)
            {
                cash = GameHelper.Get102();
            }

            // ui.text_num1.text = $"{GameHelper.Get101Str(cash)}";
        }

        private void SetAvatar()
        {
            // GameHelper.SetSelfAvatar(((btn_avatar)ui.btn_av).loader_avatar);
        }

        private void SetName()
        {
            // GameHelper.SetName(ui.text_name);
        }

        private RewardSingleData GetRewardData(int index = 0)
        {
            return model.rewardInfo.GetRewardDataList()[index];
        }

        private decimal GetRewardValue(int index = 0)
        {
            return GetRewardData(index).value;
        }

        private RewardOrigin GetRewardOrigin(int index = 0)
        {
            return GetRewardData(index).origin;
        }

        private void OnClickCollect()
        {
            isMultiple = false;
            GetReward();
            CtrlCloseUI();
            GameHelper.ShowInterstitial("interstitial_skip");
        }

        public void GetReward()
        {
            if (isMultiple)
            {
                foreach (var rewardSingleData in model.rewardInfo.GetRewardDataList())
                {
                    rewardSingleData.rate = rewardSingleData.multiRate;
                }
            }

            RewardSystem.GetReward(model.rewardInfo);
        }
    }
}