using UnityEngine;
using UnityEngine.SceneManagement;
using TMPro;
using DG.Tweening;

public class Celebration : MonoBehaviour
{
	public TextMeshPro CelebrationText;

	public Color Gold;
	public Color Purple;
	public Color Plain;

	private bool celebrating = false;

    void Update()
    {
		if (celebrating)
			return;

        if(Scoreboard.instance.timesUp && AllBalloonsResolved())
		{
			ShowCelebration();
		}
    }

	private bool AllBalloonsResolved()
	{
		return GameObject.FindWithTag("Balloon") == null;
	}

	private void ShowCelebration()
	{
		celebrating = true;

		int total =
			Scoreboard.instance.goldenBalloons * 5 +  // 30 possible (6 x 5)
			Scoreboard.instance.purpleBalloons * 2 +  // 24 possible (12 x 2)
			Scoreboard.instance.blueBalloons * 1;     // 18 possible (18 x 1)
													  // 72 is a perfect score

		if (total >= 72)
		{
			Time.timeScale = 0f;

			transform.localScale = Vector3.zero;
			transform.DOScale(Vector3.one, 1f).SetEase(Ease.InCubic).SetUpdate(true);
			transform.DOLocalRotate(new Vector3(0, 1440, 0), 1f, RotateMode.FastBeyond360).SetRelative(true).SetUpdate(true);

			CelebrationText.color = Gold;
			CelebrationText.text = "AMAZING!!\nPERFECT!!";

			DOVirtual.DelayedCall(3f, ReloadLevel, true);
		}
		else if (total > 68)
		{
			Time.timeScale = 0f;

			transform.localScale = Vector3.zero;
			transform.DOScale(Vector3.one, 0.3f).SetEase(Ease.OutElastic).SetUpdate(true);

			CelebrationText.color = Purple;
			CelebrationText.text = "SO CLOSE!!";

			DOVirtual.DelayedCall(2f, ReloadLevel, true);
		}
		else if (total > 60)
		{
			Time.timeScale = 0f;

			transform.localScale = Vector3.zero;
			transform.DOScale(Vector3.one, 0.05f).SetUpdate(true); ;

			CelebrationText.color = Purple;
			CelebrationText.text = "Great!";

			DOVirtual.DelayedCall(1.7f, ReloadLevel, true);
		}
		else if (total > 55)
		{
			CelebrationText.color = Purple;
			CelebrationText.text = "Good Job!";

			DOVirtual.DelayedCall(1.6f, ReloadLevel, true);
		}
		else if (total > 50)
		{
			CelebrationText.color = Plain;
			CelebrationText.text = "Solid";

			DOVirtual.DelayedCall(3f, ReloadLevel, true);
		}
		else if (total > 40)
		{
			CelebrationText.color = Plain;
			CelebrationText.text = "Not Bad";

			DOVirtual.DelayedCall(1.5f, ReloadLevel, true);
		}
		else if(total > 30)
		{
			CelebrationText.color = Plain;
			CelebrationText.text = "Just Okay";

			DOVirtual.DelayedCall(1.3f, ReloadLevel, true);
		}
		else
		{
			CelebrationText.color = Plain;
			CelebrationText.text = "Needs Work";

			DOVirtual.DelayedCall(1.1f, ReloadLevel, true);
		}

	}

	private void ReloadLevel()
	{
		Time.timeScale = 1f;
		DOTween.KillAll();
		SceneManager.LoadScene(SceneManager.GetActiveScene().name);
	}

	void Start()
	{
		CelebrationText.text = "";
	}
}
