using ChillConnect;
using DG.Tweening;
using IgnoreOPS;
using UnityEngine;

public class RewardSystem : BaseSystem
{
    public RewardSystem(bool isAutoInit = true)
    {
        if (isAutoInit)
        {
            Init();
        }
    }

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

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


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


        if (IsRewardDialog(rewardData))
        {
            UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.RewardUI_Open, rewardData);
        }
        else
        {
            if (IsRewardNumber(rewardData))
            {
                rewardData.OnCompleted(true);
                GameDispatcher.Instance.Dispatch(GameMsg.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(GameMsg.ProcessReward, rewardData);
    }

    private static Tween _tween;


    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;
            }


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

                    GameHelper.OnRiseUI(id);
                    // }
                }
                else if (id == 111 || id == 112)
                {
                    if (_tween is { active: true })
                    {
                        _tween?.Kill();
                    }
                }


                SetRewardValueChangeState(id, true);
            }


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


            var isNeedFly = IsRewardFly(rewardData);

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

            rewardDisplayModel.SetUpdate(changeValue => { OnChangeValue(id, changeValue); });
            rewardDisplayModel.SetUpdateComplete(() =>
            {
                completed++;
                if (isValueChange)
                {
                    SetRewardValueChangeState(id, false);
                    if (id == 101)
                    {
                        // if (!UIManager.Instance.IsExistUI(UIConst.H5UI))
                        // {
                        _tween = DOVirtual.DelayedCall(0.5f,
                            () => { GameHelper.OnRiseUIRecover(id, UILayerType.Normal); });
                        // }
                    }
                }

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

            rewardSingleData.InitFlyPosition();
            UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.RewardAniUI_Open, rewardDisplayModel);
        }
    }

    private static void SetRewardValueChangeState(int id, bool isShow)
    {
        switch (id)
        {
            case 101:
                DataMgr.IsShowRewardFly101.Value = isShow;
                break;
            case 102:
                DataMgr.IsShowRewardFly102.Value = isShow;
                break;
            case 111:
                DataMgr.IsShowRewardFly111.Value = isShow;
                break;
        }
    }

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