using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
using System;

public class GameDirector : MonoBehaviour {

	public byte targetPoints = 130;
	public float timerLimitMinutes = 3;
	public MinionJaugeScript scr;
	public MinionJaugeScript scrDe;
	DateTime dateLastDrop;

	GameObject[] Minions;

	byte ownedCPs = 0;
	byte minionPoints = 0;
	float time = 0.00F;
	DateTime dateBegin;
	DateTime lastIncMinionScoreForVictoryAgainstMonsterNo;
	int nbMinions;
	
	void Start () {
		Minions = GameObject.FindGameObjectsWithTag("Minion");
		dateBegin = DateTime.Now;
		lastIncMinionScoreForVictoryAgainstMonsterNo = DateTime.Now;

		dateLastDrop = DateTime.Now;
	}

	public void altarFallen()
	{
		++ownedCPs;
		Debug.Log("CP captured");
	}

	public void altarLost()
	{
		--ownedCPs;
		Debug.Log("CP lost");
	}
	
	void Update ()
	{
		if (DateTime.Now >= lastIncMinionScoreForVictoryAgainstMonsterNo.AddSeconds(1.0f)){
			lastIncMinionScoreForVictoryAgainstMonsterNo = DateTime.Now;
			minionPoints += ownedCPs;
			scr.heal(ownedCPs);
			scrDe.heal (1);



		}
		nbMinions = GameObject.FindGameObjectsWithTag("Minion").Length;
		if (DateTime.Now >= dateBegin.AddMinutes(timerLimitMinutes) || nbMinions == 0)
		{
			Winner(true);
		}
		else if (minionPoints >= targetPoints){
			Winner(false);
		}
	}
	
	public void Winner(bool isMonster){
		if (isMonster){
			GameObject.Find("Main Camera").GetComponent<SoundDispenser>().PlaySound(SoundDispenser.e_music.WIN_MONSTER);
			SceneManager.LoadScene("WinMonster", LoadSceneMode.Single);
		}
		else{
			GameObject.Find("Main Camera").GetComponent<SoundDispenser>().PlaySound(SoundDispenser.e_music.WIN_MINIONS);
			SceneManager.LoadScene("WinMinions", LoadSceneMode.Single);
		}
	}
}
