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

public class Spawner_01 : MonoBehaviour
{
	public int m_NrOfUnits = 100;
	public GameObject m_Unit;
	
	public GameObject 	m_HotChick;
	
	private List<Zone> m_Zones;
	
	// Use this for initialization
	void Start () 
	{
		//Create all the zones for the characters
		m_Zones = new List<Zone>();
		Zone temp;
		
		//Zone 0
		temp = new Zone(-250, 300, 200, 100);
		temp.AddNeighbour(2);
		m_Zones.Add(temp);
		
		//Zone 1
		temp = new Zone(150, 300, 150, 100);
		temp.AddNeighbour(5);
		temp.AddNeighbour(6);
		m_Zones.Add(temp);
		
		//Zone 2
		temp = new Zone(-250, 150, 200, 150);
		temp.AddNeighbour(0);
		temp.AddNeighbour(3);
		temp.AddNeighbour(7);
		temp.AddNeighbour(8);
		temp.AddNeighbour(9);
		m_Zones.Add(temp);
		
		//Zone 3
		temp = new Zone(-50, 150, 100, 150);
		temp.AddNeighbour(2);
		temp.AddNeighbour(4);
		temp.AddNeighbour(9);
		m_Zones.Add(temp);
		
		//Zone 4
		temp = new Zone(50, 150, 100, 150);
		temp.AddNeighbour(3);
		temp.AddNeighbour(5);
		m_Zones.Add(temp);
		
		//Zone 5
		temp = new Zone(150, 150, 50, 150);
		temp.AddNeighbour(1);
		temp.AddNeighbour(4);
		temp.AddNeighbour(6);
		m_Zones.Add(temp);
		
		//Zone 6
		temp = new Zone(200, 150, 100, 150);
		temp.AddNeighbour(1);
		temp.AddNeighbour(5);
		temp.AddNeighbour(11);
		m_Zones.Add(temp);
		
		//Zone 7
		temp = new Zone(-250, 50, 100, 100);
		temp.AddNeighbour(2);
		temp.AddNeighbour(8);
		temp.AddNeighbour(12);
		m_Zones.Add(temp);
		
		//Zone 8
		temp = new Zone(-150, 50, 50, 100);
		temp.AddNeighbour(2);
		temp.AddNeighbour(7);
		temp.AddNeighbour(9);
		temp.AddNeighbour(10);
		m_Zones.Add(temp);
		
		//Zone 9 
		temp = new Zone(-100, 100, 150, 50);
		temp.AddNeighbour(2);
		temp.AddNeighbour(3);
		temp.AddNeighbour(8);
		temp.AddNeighbour(10);
		m_Zones.Add(temp);
		
		//Zone 10 
		temp = new Zone(-100, 50, 150, 50);
		temp.AddNeighbour(8);
		temp.AddNeighbour(9);
		temp.AddNeighbour(13);
		temp.AddNeighbour(14);
		temp.AddNeighbour(15);
		m_Zones.Add(temp);
		
		//Zone 11 
		temp = new Zone(200, -50, 100, 200);
		temp.AddNeighbour(6);
		//temp.AddNeighbour(21);
		m_Zones.Add(temp);
		
		//Zone 12
		temp = new Zone(-250, -50, 100, 100);
		temp.AddNeighbour(7);
		temp.AddNeighbour(17);
		m_Zones.Add(temp);
		
		//Zone 13
		temp = new Zone(-100, -100, 100, 150);
		temp.AddNeighbour(10);
		temp.AddNeighbour(14);
		temp.AddNeighbour(18);
		m_Zones.Add(temp);
		
		//Zone 14
		temp = new Zone(0, -50, 50, 100);
		temp.AddNeighbour(10);
		temp.AddNeighbour(13);
		temp.AddNeighbour(15);
		temp.AddNeighbour(18);
		m_Zones.Add(temp);
		
		//Zone 15
		temp = new Zone(50, -50, 100, 150);
		temp.AddNeighbour(10);
		temp.AddNeighbour(14);
		temp.AddNeighbour(18);
		m_Zones.Add(temp);
		
		//Zone 16
		temp = new Zone(-400, -150, 150, 100);
		temp.AddNeighbour(17);
		m_Zones.Add(temp);
		
		//Zone 17
		temp = new Zone(-250, -150, 100, 100);
		temp.AddNeighbour(12);
		temp.AddNeighbour(16);
		temp.AddNeighbour(22);
		m_Zones.Add(temp);
		
		//Zone 18
		temp = new Zone(0, -100, 150, 50);
		temp.AddNeighbour(13);
		temp.AddNeighbour(14);
		temp.AddNeighbour(15);
		temp.AddNeighbour(19);
		temp.AddNeighbour(20);
		m_Zones.Add(temp);
		
		//Zone 19
		temp = new Zone(0, -150, 150, 50);
		temp.AddNeighbour(18);
		temp.AddNeighbour(20);
		temp.AddNeighbour(25);
		m_Zones.Add(temp);
		
		//Zone 20
		temp = new Zone(150, -250, 50, 200);
		temp.AddNeighbour(18);
		temp.AddNeighbour(19);
		temp.AddNeighbour(21);
		temp.AddNeighbour(25);
		m_Zones.Add(temp);
		
		//Zone 21
		temp = new Zone(200, -250, 100, 200);
		temp.AddNeighbour(11);
		temp.AddNeighbour(20);
		m_Zones.Add(temp);
		
		//Zone 22
		temp = new Zone(-250, -250, 100, 100);
		temp.AddNeighbour(17);
		temp.AddNeighbour(23);
		m_Zones.Add(temp);
		
		//Zone 23
		temp = new Zone(-150, -250, 100, 100);
		temp.AddNeighbour(22);
		temp.AddNeighbour(24);
		m_Zones.Add(temp);
		
		//Zone 24
		temp = new Zone(-50, -250, 50, 100);
		temp.AddNeighbour(23);
		temp.AddNeighbour(25);
		temp.AddNeighbour(26);
		m_Zones.Add(temp);
		
		//Zone 25
		temp = new Zone(0, -250, 150, 100);
		temp.AddNeighbour(19);
		temp.AddNeighbour(20);
		temp.AddNeighbour(24);
		temp.AddNeighbour(26);
		m_Zones.Add(temp);
		
		//Zone 26
		temp = new Zone(-50, -400, 200, 150);
		temp.AddNeighbour(24);
		temp.AddNeighbour(25);
		m_Zones.Add(temp);
		
		//Instantiate all the characters
		for(int t=0; t < m_NrOfUnits; ++t)
		{
			GameObject newUnit = (GameObject)Instantiate(m_Unit);
			
			int spawnZone = Random.Range (0, m_Zones.Count - 1);
			newUnit.GetComponent<Unit>().SetZones(m_Zones, spawnZone);
			newUnit.GetComponent<Unit>().m_IsMoving = true;
		}
	}
	
	void Update () 
	{
		int rand = Random.Range(0,1000);
		if(rand==1)
			BurnAUnit();
		if(rand>995 && GameObject.FindGameObjectWithTag("Chick")==null) // only one chick at a time
			SpawnHotChick();
	}
	
	private void BurnAUnit()
	{
		GameObject[] units = GameObject.FindGameObjectsWithTag("Unit");
		units[Random.Range(0,units.Length-1)].GetComponent<Unit>().StartFire();
	}
	
	private void SpawnHotChick()
	{
		GameObject newChick = (GameObject)Instantiate(m_HotChick);
			
		int spawnZone = Random.Range (0, m_Zones.Count);
		newChick.GetComponent<HotChick>().SetZones(m_Zones, spawnZone);
	}
	
}