using DG.Tweening;
using BingoBrain;
using BingoBrain.Core;
using BingoBrain.HotFix;
using UnityEngine;

public class UsaSystem : BaseSystem
{
    private static Tween _tween;

    public UsaSystem(bool isAutoInit = true)
    {
        if (isAutoInit)
        {
            Init();
        }
    }

    public override void Init()
    {
        base.Init();
        AddListener();
    }

    private void AddListener()
    {
        GameDispatcher.Instance.AddListener(BingoInfo.GetReward, OnGetReward);
    }


    private void OnGetReward(object obj)
    {
        if (obj is not RewardData rewardData)
        {
            return;
        }


        if (IsRewardDialog(rewardData))
        {
            if (!GameHelper.IsGiftSwitch()) UICtrlDispatcher.Instance.Dispatch(SkinInfo.GodPleUI_Open, rewardData);
            else
            {
                // var temp = new SuccessData();
                // temp.IsWin = true;
                // temp.cash_number = (float)rewardData.rewardDataList[0].value;
                // temp.rate =  (int)GameHelper.GetRewardValue(2)[1];
                // temp.IsLevelSuccess = false;
                // temp.IsH5Reward = true;
             //   UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.LevelSuccessUI_Open, temp);
            }
        }
        else
        {
            if (IsRewardNumber(rewardData))
            {
                rewardData.OnCompleted(true);
                GameDispatcher.Instance.Dispatch(BingoInfo.ProcessReward, rewardData);
            }
            else
            {
                GetReward(rewardData);
            }
        }
    }


    public static bool IsRewardFly(RewardData rewardData)
    {
        return (RewardDisplayType.RewardFly & rewardData.displayType) == RewardDisplayType.RewardFly;
    }


    public static bool IsRewardValueChange(RewardData rewardData)
    {
        return (RewardDisplayType.ValueChange & rewardData.displayType) == RewardDisplayType.ValueChange;
    }


    public static bool IsRewardNumber(RewardData rewardData)
    {
        return (RewardDisplayType.Number & rewardData.displayType) == RewardDisplayType.Number;
    }


    public static bool IsRewardDialog(RewardData rewardData)
    {
        return (RewardDisplayType.Dialog & rewardData.displayType) == RewardDisplayType.Dialog;
    }


    public static void GetReward(RewardData rewardData)
    {
        PlayReward(rewardData);
        GameDispatcher.Instance.Dispatch(BingoInfo.ProcessReward, rewardData);
    }


    private static void PlayReward(RewardData rewardData)
    {
        if (rewardData == null) return;
        var completed = 0;

        foreach (var rewardSingleData in rewardData.GetRewardDataList())
        {
            var id = rewardSingleData.id;
            var sum = rewardSingleData.value;

            if (sum <= 0)
            {
                continue;
            }

            switch (rewardSingleData.origin)
            {
                case RewardOrigin.Play:
                case RewardOrigin.Slot:
                case RewardOrigin.PickBox:
                case RewardOrigin.PlayCard:
                case RewardOrigin.MoreCard:
                case RewardOrigin.PlayBallon:
                    BingoCell.AddReadSum(id, sum);
                    break;
            }


            var isValueChange = IsRewardValueChange(rewardData);
            if (isValueChange)
            {
                if (rewardSingleData.endPosition == Vector2.zero && id is 101 or 102)
                {
                    if (!UI.Instance.IsExistUI(UIConst.SmailUI))
                    {
                        if (_tween is { active: true })
                        {
                            _tween?.Kill();
                        }
                    }
                }
                else if (id == 111)
                {
                    if (_tween is { active: true })
                    {
                        _tween?.Kill();
                    }
                }


                SetRewardValueChangeState(id, true);
            }


            if (!rewardSingleData.IsCanFly())
            {
                continue;
            }


            var isNeedFly = IsRewardFly(rewardData);

            rewardSingleData.InitFlyPosition();

            var rewardDisplayModel = new GodDa(rewardSingleData)
            {
                isSingle = rewardData.isSingle,
                isPlayAudio = true,
                isNeedFly = isNeedFly,
                isNeedValueChange = isValueChange,
                audioName = DoConst.CoinFly
            };

            rewardDisplayModel.SetUpdate(changeValue => { OnChangeValue(id, changeValue); });
            rewardDisplayModel.SetUpdateComplete(() =>
            {
                completed++;
                if (isValueChange)
                {
                    SetRewardValueChangeState(id, false);
                    if (id is 101 or 102)
                    {
                        if (!UI.Instance.IsExistUI(UIConst.SmailUI))
                        {
                            _tween = DOVirtual.DelayedCall(0.5f, () => { });
                        }
                    }
                }

                if (completed >= rewardData.GetRewardFlyCount())
                {
                    rewardData.OnCompleted(true);
                }
            });

            UICtrlDispatcher.Instance.Dispatch(SkinInfo.GodPleAcUI_Open, rewardDisplayModel);
        }
    }


    private static void SetRewardValueChangeState(int id, bool isShow)
    {
        switch (id)
        {
            case 101:
                PreferencesMgr.Instance.IsShowRewardFly101 = isShow;
                break;
            case 102:
                PreferencesMgr.Instance.IsShowRewardFly102 = isShow;
                break;
            case 105:
                PreferencesMgr.Instance.IsShowRewardFly105 = isShow;
                break;
            case 106:
                PreferencesMgr.Instance.IsShowRewardFly106 = isShow;
                break;
        }
    }


    private static void OnChangeValue(int id, decimal changeValue)
    {
        switch (id)
        {
            case 101:
                GameDispatcher.Instance.Dispatch(BingoInfo.Update101, changeValue);
                break;
            case 102:
                GameDispatcher.Instance.Dispatch(BingoInfo.Update102, changeValue);
                break;
        }
    }
}