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

public class UI_PlayerHUD : MonoBehaviour
{
	[SerializeField]
	private Image _energyProgress;
	[SerializeField]
	private Image _reinforcementProgress;
	[SerializeField]
	private Image _reinforcementGlow;
	[SerializeField]
	private Image _reinforcementIcon;

	[SerializeField]
	private Image _fleeIndicator;

	[SerializeField]
	private Image _shieldIndicator;
	[SerializeField]
	private Image _shieldIcon;
	[SerializeField]
	private Image _shieldButton;

	[SerializeField]
	private Image _marchIndicator;
	[SerializeField]
	private Image _marchIcon;
	[SerializeField]
	private Image _marchButton;

	[SerializeField]
	private Image _chargeIndicator;
	[SerializeField]
	private Image _chargeIcon;
	[SerializeField]
	private Image _chargeButton;

	public void UpdateProgressIndicators(float inEnergyProgress, float inReinforcementProgress, bool inCooldownReached, BoidController.BehaviourType inType)
	{
		_energyProgress.fillAmount = inEnergyProgress;
		_reinforcementProgress.fillAmount = inReinforcementProgress;
		ToggleCooldownReached(inCooldownReached, inType);
	}
	public void OnBehaviourChanged(BoidController.BehaviourType inType)
	{
		ToggleBehaviourIndicator(_shieldIndicator, inType == BoidController.BehaviourType.Defend);
		ToggleBehaviourIndicator(_marchIndicator, inType == BoidController.BehaviourType.March);
		ToggleBehaviourIndicator(_chargeIndicator, inType == BoidController.BehaviourType.Charge);
		ToggleBehaviourIndicator(_fleeIndicator, inType == BoidController.BehaviourType.Flee);
	}
	private void ToggleBehaviourIndicator(Image inIndicator, bool inOn)
	{
		if(inOn && !inIndicator.gameObject.activeInHierarchy)
		{
			inIndicator.gameObject.SetActive(true);
			inIndicator.DOFade(1, .5f);

			inIndicator.transform.localScale = Vector3.zero;
			inIndicator.transform.DOScale(1, .5f);
		}
		else if(!inOn && inIndicator.gameObject.activeInHierarchy)
		{
			inIndicator.transform.DOScale(0, .5f);
			inIndicator.DOFade(0, .5f).OnComplete(() => inIndicator.gameObject.SetActive(false));
		}
	}

	public void OnCallReinforcements()
	{
		_reinforcementGlow.gameObject.SetActive(true);
		_reinforcementGlow.transform.DOScale(1.1f, .5f).SetLoops(2, LoopType.Yoyo).SetEase(Ease.InQuad);
		_reinforcementGlow.DOFade(1, .25f);

		_reinforcementIcon.transform.DOScale(1.1f, .5f).SetLoops(2, LoopType.Yoyo).SetEase(Ease.InQuad);
		_reinforcementProgress.fillAmount = 0;
		StartCoroutine(DisableReinforcementGlow());
	}
	private IEnumerator DisableReinforcementGlow()
	{
		yield return new WaitForSeconds(1);
		_reinforcementGlow.DOFade(0, .25f).OnComplete(() => _reinforcementGlow.gameObject.SetActive(false));
	}
	private void ToggleCooldownReached(bool inCooldownReached, BoidController.BehaviourType inType)
	{
		_shieldIcon.DOFade(inCooldownReached || inType == BoidController.BehaviourType.Defend? 1 : .5f, 0);
		_marchIcon.DOFade(inCooldownReached || inType == BoidController.BehaviourType.March ? 1 : .5f, 0);
		_chargeIcon.DOFade(inCooldownReached || inType == BoidController.BehaviourType.Charge ? 1 : .5f, 0);

		_shieldButton.DOFade(inCooldownReached || inType == BoidController.BehaviourType.Defend ? 1 : .5f, 0);
		_marchButton.DOFade(inCooldownReached || inType == BoidController.BehaviourType.March ? 1 : .5f, 0);
		_chargeButton.DOFade(inCooldownReached || inType == BoidController.BehaviourType.Charge ? 1 : .5f, 0);
	}
}
