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

public class UI_GameOverWindow : MonoBehaviour
{
	[SerializeField]
	private TextFader _mainTextFader;
	[SerializeField]
	private Text _mainText;

	[SerializeField]
	private TextFader _subTextFader;

	private bool _canRestart = false;

	void Start ()
	{
		BattleController.Instance.OnBattleEnded.AddListener(DoGameOverWindow);
	}
	void Update()
	{
		if(_canRestart && (InputManager.GetButtonDown(0, ControllerButton.A) || InputManager.GetButtonDown(1, ControllerButton.A)))
		{
			RestartGame();
		}
	}
	private void DoGameOverWindow(BoidController inLosingArmy)
	{
		StartCoroutine(GameOverRoutine(inLosingArmy));
	}
	private IEnumerator GameOverRoutine(BoidController inLosingArmy)
	{
		_mainText.text = inLosingArmy.PlayerIndex == 0 ? "Player 2 Was Victorious!" : "Player 1 Was Victorious!";

		_mainText.transform.localScale = Vector3.zero;
		_mainText.gameObject.SetActive(true);
		_mainText.transform.DOScale(1, .25f);

		yield return new WaitForSeconds(1);

		_subTextFader.transform.localScale = Vector3.zero;
		_subTextFader.gameObject.SetActive(true);
		_subTextFader.transform.DOScale(1, .25f);

		_canRestart = true;

	}
	private void RestartGame()
	{
		_canRestart = false;
		_mainText.transform.DOScale(0, .25f).OnComplete(() => 
		{
			_mainText.gameObject.SetActive(false);
			_mainTextFader.enabled = true;
		});

		_subTextFader.transform.DOScale(0, .25f).OnComplete(() =>
		{
			_subTextFader.gameObject.SetActive(false);
			_subTextFader.enabled = true;
		});

		BattleController.Instance.RestartBattle();
	}
}
