using System.Collections;
using UnityEngine;

namespace Wizard;

public class BattlePassGuage : MonoBehaviour
{
	[SerializeField]
	public ParticleSystem GaugeEfc;

	[SerializeField]
	private UIGauge _uIGauge;

	[SerializeField]
	private UILabel _nextLevelTitleLabel;

	[SerializeField]
	private UILabel _nextLevelPoint;

	[SerializeField]
	private UILabel _level;

	[SerializeField]
	private UISprite _battlePassIcon;

	private BattlePassGaugeInfo _battlePassGaugeInfo;

	private const float GAUGEUP_DURATION = 0.3f;

	private const float GAUGEUP_DELAY = 1f;

	private const float GAUGE_SIZE = 1.15f;

	private const int GAUGEUP_SE_CNT = 6;

	private int _nowLevel;

	private int _nowNextLevelPoint;

	public bool IsAnimationComplete { get; private set; }

	public void Initialize(BattlePassGaugeInfo battlePassGaugeInfo)
	{
		_uIGauge.Value = battlePassGaugeInfo.CurrentGaugeValue();
		_nextLevelTitleLabel.text = Data.SystemText.Get("BattlePass_0009");
		_nextLevelPoint.text = battlePassGaugeInfo.NextLevelRequiredPoint.ToString();
		_level.text = battlePassGaugeInfo.CurrentLevel.ToString();
	}

	public void LvUpInitialize(BattlePassGaugeInfo battlePassGaugeInfo)
	{
		_battlePassGaugeInfo = battlePassGaugeInfo;
		_battlePassIcon.spriteName = (battlePassGaugeInfo.IsPremium ? "icon_battlepass_premium" : "icon_battlepass_normal");
		_uIGauge.Value = battlePassGaugeInfo.BeforeGaugeValue;
		_nextLevelTitleLabel.text = Data.SystemText.Get("BattlePass_0009");
		_nowNextLevelPoint = battlePassGaugeInfo.BeforeLevelNextLevelRequiredPoint;
		_nextLevelPoint.text = _nowNextLevelPoint.ToString();
		_level.text = battlePassGaugeInfo.BeforeCurrentLevel.ToString();
	}

	public void AddBattlePassPoint()
	{
		_nowLevel = _battlePassGaugeInfo.BeforeCurrentLevel;
		StartCoroutine(AddBattlePassPointCor());
	}

	private IEnumerator AddBattlePassPointCor()
	{
		yield return new WaitForSeconds(0.5f);
		if (_battlePassGaugeInfo.IsLevelChange)
		{
			SetGaugeAniamtion(_uIGauge.Value, 1f, "LvUp");
		}
		else
		{
			SetGaugeAniamtion(_uIGauge.Value, _battlePassGaugeInfo.CurrentGaugeValue(), "LvupComplete");
		}
	}

	private void LvUp()
	{
		_nowLevel++;
		_level.text = _nowLevel.ToString();
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_RESULT_LEVELUP);
		GameMgr.GetIns().GetEffectMgr().Start(EffectMgr.EffectType.CMN_RESULT_LVUP_1, _battlePassIcon.transform.position)
			.gameObject.SetLayer(LayerMask.NameToLayer("SystemUI"), isSetChildren: true);
		Effect effect = GameMgr.GetIns().GetEffectMgr().Start(EffectMgr.EffectType.CMN_RESULT_GAUGE_2, _uIGauge.GetTransformGaugeStartEdge().position);
		if (effect != null)
		{
			effect.transform.localScale = Vector3.one * 1.15f;
			effect.gameObject.SetLayer(LayerMask.NameToLayer("SystemUI"), isSetChildren: true);
		}
		if (_nowLevel == BattlePassUtility.BattlePassMaxLevel)
		{
			LvupComplete();
		}
		else if (_nowLevel < _battlePassGaugeInfo.CurrentLevel)
		{
			SetGaugeAniamtion(0f, 1f, "LvUp");
		}
		else
		{
			SetGaugeAniamtion(0f, _battlePassGaugeInfo.CurrentGaugeValue(), "LvupComplete");
		}
	}

	private void SetGaugeAniamtion(float from, float to, string oncomp = "")
	{
		iTween.ValueTo(base.gameObject, iTween.Hash("from", from, "to", to, "time", 0.3f, "delay", 0.1f, "onstart", "StartBattlePassPoint", "onupdate", "UpdateBattlePassPoint", "oncomplete", oncomp, "easetype", iTween.EaseType.easeOutQuad));
	}

	private void StartBattlePassPoint()
	{
		PlayGaugeUpSE();
		GaugeEfc.gameObject.SetActive(value: true);
		GaugeEfc.Play();
	}

	private void PlayGaugeUpSE()
	{
		StartCoroutine(GaugeUpSECoroutine());
	}

	private IEnumerator GaugeUpSECoroutine()
	{
		for (int i = 0; i < 6; i++)
		{
			GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_RESULT_GAUGEUP);
			yield return new WaitForSeconds(0.05f);
		}
	}

	private void UpdateBattlePassPoint(float num)
	{
		_uIGauge.Value = num;
		int num2 = BattlePassUtility.NextLevelRequiredPoint(_nowLevel);
		_nowNextLevelPoint = num2 - (int)((float)num2 * num);
		_nextLevelPoint.text = _nowNextLevelPoint.ToString();
	}

	private void CompleteBattlePassPoint()
	{
		GaugeEfc.Stop();
	}

	private void LvupComplete()
	{
		_nextLevelPoint.text = _battlePassGaugeInfo.NextLevelRequiredPoint.ToString();
		StartCoroutine(SetLvupCompletAnimation());
	}

	private IEnumerator SetLvupCompletAnimation()
	{
		yield return new WaitForSeconds(0.1f);
		CompleteBattlePassPoint();
		IsAnimationComplete = true;
	}
}
