using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

public class BattleController : Singleton<BattleController>
{
	[SerializeField]
	private BoidController _controllerA;
	[SerializeField]
	private BoidController _controllerB;

	public PrepareBattleEvent OnPrepareBattle = new PrepareBattleEvent();
	public UnityEvent OnBattleStarted = new UnityEvent();
	public BattleEndedEvent OnBattleEnded = new BattleEndedEvent();

	private bool _battleStarted = false;
	public bool BattleStarted { get { return _battleStarted; } }

	// Use this for initialization
	void Start ()
	{
		StartCoroutine(PrepareAndStartBattle(1));
	}

	public void RestartBattle()
	{
		StartCoroutine(PrepareAndStartBattle(1));
	}
	private IEnumerator PrepareAndStartBattle(float inDelay)
	{
		yield return new WaitForSeconds(inDelay);

		_battleStarted = false;
		OnPrepareBattle.Invoke(_controllerA, _controllerB);

		yield return new WaitForSeconds(3);

		OnBattleStarted.Invoke();
		_battleStarted = true;
	}

	public void OnArmyDestroyed(BoidController inArmy)
	{
		OnBattleEnded.Invoke(inArmy);
		//_battleStarted = false;
		/*if (inArmy.PlayerIndex == 1)
			Debug.Log("Player 1 Wins!!!");
		else
			Debug.Log("Player 2 Wins!!!");

		StartCoroutine(PrepareAndStartBattle(2));*/
	}
}

public class PrepareBattleEvent : UnityEvent<BoidController, BoidController>{ }
public class BattleEndedEvent : UnityEvent<BoidController> { }

