using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class EnemyGenerator : MonoBehaviour
{
	public GameObject masterObject;
	public Camera minimapCamera;
	public GameObject gangMemberPrefab;
	public GameObject enforcerPrefab;
	public GameObject thiefPrefab;
	public GameObject technoWitchPrefab;
	public List<GameObject> enemyList = new List<GameObject>();
	void Start ()
	{
		masterObject = GameObject.Find("_MasterObject");
	}
	public void spawnEnemies (int level, int spawnCount)
	{
		GameObject enemiesGroup = new GameObject("_Enemies");
		int actualSpawnCount = 0;
		killAllEnemies();
		for (int j = 0; j < DungeonGenerator.maxDimension; j++)
		{
			for (int i = 0; i < DungeonGenerator.maxDimension; i++)
			{
				if (DungeonGenerator.mazeRoom[i, j] == (int)(DungeonGenerator.TileType.Floor)	&&
					i != ((PlayerControl)GameObject.Find("_Player").GetComponent<PlayerControl>()).xIndex	&&
					j != ((PlayerControl)GameObject.Find("_Player").GetComponent<PlayerControl>()).yIndex)
				{
					if (Random.Range(0, 101) <= 10)
					{
						GameObject unitPrefab = getRandomUnitPrefab(level);
						if (actualSpawnCount == 0	&&
							level >= masterObject.GetComponent<DungeonGenerator>().lastLevel)
						{
							unitPrefab = technoWitchPrefab;		// Only spawn one Techno Witch on the last level 7!
						}
						GameObject unit = (GameObject)Instantiate(unitPrefab, new Vector3(i * 0.32f, 0.0f, j * 0.32f), Quaternion.identity);
						EnemyAI eai = unit.GetComponent<EnemyAI>();
						eai.xIndex = i;
						eai.yIndex = j;
						enemyList.Add(unit);
						actualSpawnCount++;
						unit.transform.parent = enemiesGroup.transform;
					}
				}
				if (actualSpawnCount >= spawnCount)
				{
					minimapCamera.GetComponent<MinimapCamera>().setEnemyMarkers();
					return;
				}
			}
		}
		minimapCamera.GetComponent<MinimapCamera>().setEnemyMarkers();
	}
	/*
		NOTE: 	Probably to change enemies every two levels.
				Levels 1-2:	Basic 1 damage, 1 AP enemies that die in one shot (Gang Member).
				Levels 3-4:	New enemy that takes 2 shots to kill (Enforcer).
				Levels 5-6:	New enemy unit that has 2 AP and steals resources (Thief).
				Levels 7-8: New enemy that deals 2 damage, has 2 AP, and takes multiple shots to kill (Techno Witch).
	*/
	public GameObject getRandomUnitPrefab (int level)
	{
		if (level <= 2)
		{
			return gangMemberPrefab;
		}
		else if (level <= 4)
		{
			switch (Random.Range(0, 2))
			{
				case 0:	return gangMemberPrefab;
				case 1:	return enforcerPrefab;
			}
		}
		else if (level <= 6)
		{
			switch (Random.Range(0, 2))
			{
				case 0:	return enforcerPrefab;
				case 1:	return thiefPrefab;
			}
		}
		else//else if (level <= 8)
		{
			switch (Random.Range(0, 3))
			{
				case 0:	return gangMemberPrefab;
				case 1:	return enforcerPrefab;
				case 2:	return thiefPrefab;
			}
		}
		return null;
	}
	public bool isTileClearOfUnits (int xIndex, int yIndex)
	{
		EnemyAI eai;
		for (int i = 0; i < enemyList.Count; i++)
		{
			eai = enemyList[i].GetComponent<EnemyAI>();
			if (eai.xIndex == xIndex	&&
				eai.yIndex == yIndex)
			{
				return false;
			}
		}
		return true;
	}
	public void enemyTurn (int playerActionPoints)
	{
		EnemyAI eai;
		for (int i = 0; i < enemyList.Count; i++)
		{
			if (enemyList[i] != null)
			{
				eai = enemyList[i].GetComponent<EnemyAI>();
				if (eai.currentActionPoints >= playerActionPoints)
				{
					eai.takeTurn();
				}
			}
		}
		minimapCamera.GetComponent<MinimapCamera>().setEnemyMarkers();
	}
	public int bullhornEnemies (int voiceStat)
	{
		EnemyAI eai;
		int bullhornCount = 0;
		for (int i = 0; i < enemyList.Count; i++)
		{
			eai = enemyList[i].GetComponent<EnemyAI>();
			bool isBullhorned = eai.isBullhornSubmit(voiceStat);
			if (isBullhorned)
			{
				bullhornCount++;
			}
		}
		return bullhornCount;
	}
	public void boostEnemyWill ()
	{
		EnemyAI eai;
		for (int i = 0; i < enemyList.Count; i++)
		{
			eai = enemyList[i].GetComponent<EnemyAI>();
			eai.willStat += 10;
		}
	}
	public bool canTouchEnemy (int xIndex, int yIndex)	// This really shouldn't be in here...
	{
		EnemyAI eai;
		for (int i = 0; i < enemyList.Count; i++)
		{
			eai = enemyList[i].GetComponent<EnemyAI>();
			if (eai.xIndex == xIndex	&&
				eai.yIndex == yIndex)
			{
				if (eai.bullhorned > 0	&&			// Is surrendering!
					eai.unitName != "Techno Witch")	// Can't arrest the Techno Witch!
				{
					eai.isFallingDown = true;
					Destroy(enemyList[i], 0.3f);
					enemyList.RemoveAt(i);
					return true;
				}
			}
		}
		return false;
	}
	public bool hasTechnoWitch ()
	{
		EnemyAI eai;
		for (int i = 0; i < enemyList.Count; i++)
		{
			eai = enemyList[i].GetComponent<EnemyAI>();
			if (eai.unitName == "Techno Witch")
			{
				return true;
			}
		}
		return false;
	}
	public void killAllEnemies ()
	{
		enemyList.Clear();
	}
}