using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class EndGameManager : MonoBehaviour
{
	public enum EndgameScreen {
		winScenario,
		Lose,
		WinToUruguay
	}

	private static EndGameManager instance;
	public static EndGameManager Instance {
		get {
			return instance;
		}
	}

	private void Awake() {
		instance = this;
	}

	public void Start() {
		mainImage.gameObject.SetActive(false);
		mainText.gameObject.SetActive(false);
	}

	public Image[] endGameScreen;
	public Text[] textsToShow;
	public float timeShowingEndgame;

	public string[] textsAsWon;
	public AudioClip creepyMusic;

	public Image mainImage;
	public Text mainText;

	public Sprite[] endGame;

	public void ShowEndGameScreen(EndgameScreen endGameSprite) {
		mainText.text = textsAsWon[(int) endGameSprite];
		mainImage.sprite = endGame[(int)endGameSprite];

		mainImage.gameObject.SetActive(true);
		mainText.gameObject.SetActive(true);
		StartCoroutine(ShowEndGameScreenCoroutine());
		MusicManager.Instance.StopMusic();
		AudioManager.Instance.Play(creepyMusic);
	}

	IEnumerator ShowEndGameScreenCoroutine() {
		bool finished = false;
		float timer = 0;
		Color tempColor = new Color(1f, 1f, 1f, 0f);
		while (!finished) {
			timer += Time.deltaTime;
			tempColor.a = Mathf.Lerp(0, 1, timer / timeShowingEndgame);
			for (int x = 0; x < endGameScreen.Length; ++x) {
				endGameScreen[x].color = tempColor;
			}
			for (int x = 0; x < textsToShow.Length; ++x) {
				textsToShow[x].color = tempColor;
			}
			if (timer > timeShowingEndgame) {
				finished = true;
			}
			yield return null;
		}
	}

}
