using System;
using System.Collections;
using DG.Tweening;
using SimpleTools.SceneManagement;
using TMPro;
using UnityEngine;

public class Interface : MonoBehaviour {
	private static readonly int FadeOut = Animator.StringToHash("Fade");
	[SerializeField] private TMP_Text title;
	private bool _titleHidden;

	[SerializeField] private CanvasGroup gameOver;
	private bool _dead;

	[SerializeField] private Animator anim;

	private void Start() {
		gameOver.alpha = 0;
		gameOver.interactable = false;
	}

	public void PlacePiece() {
		if (_titleHidden)
			return;

		_titleHidden = true;
		title.DOFade(0f, 2f);
	}

	public void Die() {
		if (_dead)
			return;

		_dead = true;
		gameOver.DOFade(1f, 1f).OnComplete(() => gameOver.interactable = true);
	}

	public void QuitGame() {
		StartCoroutine(QuitRoutine());
	}

	private IEnumerator QuitRoutine() {
		anim.SetTrigger(FadeOut);
		yield return new WaitForSeconds(1f);
		Application.Quit();
	}

	public void Retry() {
		StartCoroutine(RetryRoutine());
	}

	private IEnumerator RetryRoutine() {
		anim.SetTrigger(FadeOut);
		yield return new WaitForSeconds(1f);
		Loader.Load("Level 1");
	}
}