using UnityEngine;
using Shapes;

public class VictoryReward : MonoBehaviour
{
	public AntiFriction littleBooster;
	public AntiFriction bigBooster;

	enum VState
	{
		Scanning,
		Waiting,
		Congratulating,
		Informing,
		Motivating,
		Done
	}
	private VState state;
	private float lastSwitchTime;

	private DoubleCenter doubleCenter;
	private BigAir bigAir;
	private LittleKing littleKing;

	void Update()
	{
		switch (state)
		{
			case VState.Scanning:
				if (AchievementsComplete()) // TESTING: || Input.GetKeyDown(KeyCode.X))
				{
					SetState(VState.Waiting);
				}
				break;
			case VState.Waiting:
				if (TimePassed() > 4.2f)
				{
					SetState(VState.Congratulating);
				}
				break;
			case VState.Congratulating:
				if (TimePassed() > 2.7f)
				{
					SetState(VState.Informing);
				}
				break;
			case VState.Informing:
				if (TimePassed() > 2.7f)
				{
					ActivateRingBoosters();
					SetState(VState.Motivating);
				}
				break;
			case VState.Motivating:
				if (TimePassed() > 2.7f)
				{
					SetState(VState.Done);
				}
				break;
		}
	}

	public void ImmediateDraw()
	{
		Vector3 pos = new Vector3(0, 12f);
		string msg = "";

		switch (state)
		{
			case VState.Congratulating:
				msg = "All Challenges Complete";
				Draw.Text(pos, msg, fontSize: 12, Color.white);
				break;
			case VState.Informing:
				msg = "Momentum Boosters Activating...";
				Draw.Text(pos, msg, fontSize: 12, Color.white);
				break;
			case VState.Motivating:
				msg = "Thanks for Playing!";
				Draw.Text(pos, msg, fontSize: 12, Color.white);
				break;
		}
	}

	private void ActivateRingBoosters()
	{
		littleBooster.Activate();
		bigBooster.Activate();
	}

	private float TimePassed()
	{
		return Time.time - lastSwitchTime;
	}

	private bool AchievementsComplete()
	{
		return doubleCenter.isAchieved() && bigAir.isAchieved() && littleKing.isAchieved();
	}

	private void SetState(VState newState)
	{
		state = newState;
		lastSwitchTime = Time.time;
	}

	private void Start()
	{
		doubleCenter = GetComponent<DoubleCenter>();
		bigAir = GetComponent<BigAir>();
		littleKing = GetComponent<LittleKing>();
	}
}
