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

public class HotChick : MonoBehaviour {
	
	public 	float 		m_RunTimer = 5;
	private float 		m_Timer;
	private List<Unit>	m_Units;
	
	public int 			m_Speed = 50;
	public Vector3 		m_Target;
	
	private List<Zone> 	m_Zones;
	private int 		m_CurrentZone;
	private int 		m_PreviousZone;
	
	//Animation
	public List<Material> m_Materials;
	private float m_AnimTimer; //1 loop per second
	private float m_TimePerFrame = 0.20f;
	public GameObject m_Explosion;
	
	//Following units
	private List<Unit> m_FollowingUnits;
	
	// Use this for initialization
	void Start () 
	{
		m_FollowingUnits = new List<Unit>();
		m_Units = new List<Unit>();
		GameObject[] tempObjects = GameObject.FindGameObjectsWithTag("Unit");
		foreach(GameObject tempObject in tempObjects)
		{
			m_Units.Add(tempObject.GetComponent<Unit>());	
		}
		
		//Let's be a random character
		int matId = Random.Range (0, m_Materials.Count);
		gameObject.GetComponent<MeshRenderer>().material = m_Materials[matId];
	}
	
	// Update is called once per frame
	void Update () 
	{
		//Are we running long enough?
		m_Timer += Time.deltaTime;
		if(m_Timer>=m_RunTimer)
		{
			DestroyMe();	
		}
		
		gameObject.GetComponent<Rigidbody>().velocity = new Vector3(0,0,0);
		
		Vector3 velocity, direction;
		direction = m_Target - transform.position;
		direction.Normalize();
		velocity = direction * m_Speed * Time.deltaTime;
	
		transform.position += velocity;
	
		//Make our character look at the direction he is walking in
		transform.LookAt(m_Target);
		
		if(IsAtTarget())
			m_Target = CreateRandomTarget();
		
		PullEverythingCloser();
		HandleAnimtion();
		
	}
	
	private bool IsAtTarget()
	{
		if(Vector3.Distance(transform.position,m_Target) < 10) 
			return true;
		return false;
	}
	
	public void SetZones(List<Zone> zones, int currentZone)
	{
		m_Zones = zones;
		m_CurrentZone = currentZone;
		
		//Set the position to a random target
		transform.position = CreateRandomTarget();
		
		//Then set the target
		m_Target = CreateRandomTarget();
		
	}
	
	private Vector3 CreateRandomTarget()
	{
		m_PreviousZone = m_CurrentZone;
		m_CurrentZone = m_Zones[m_CurrentZone].GetNeighbour(m_PreviousZone);

		int randX = (int)Random.Range(m_Zones[m_CurrentZone].GetSize().x +5, m_Zones[m_CurrentZone].GetSize().x + m_Zones[m_CurrentZone].GetSize().width-5 );
		int randY = (int)Random.Range(m_Zones[m_CurrentZone].GetSize().y +5, m_Zones[m_CurrentZone].GetSize().y + m_Zones[m_CurrentZone].GetSize().height-5 );
		
		return new Vector3(randX, 10, randY);
	}
	
	private void HandleAnimtion()
	{
		//Increase the animation timer
		m_AnimTimer += Time.deltaTime;
		
		//Once we have to swap frame
		if (m_AnimTimer >= m_TimePerFrame)
		{
			//Get the x offset of the texture & increase it
			float offset = renderer.material.GetTextureOffset("_MainTex").x;
			offset += 0.25f;
			if (offset >= 1.0f) offset = 0.0f;
			
			//Set the new offset
			renderer.material.SetTextureOffset("_MainTex", new Vector2(offset, 0.0f));
			
			//Reset timer
			m_AnimTimer = 0.0f;
		}
	}
	
	void PullEverythingCloser()
	{
		//update follow list
		m_FollowingUnits.Clear();
		List<int> neighbourZones = m_Zones[m_CurrentZone].m_Neighbour;
		
		foreach(Unit unit in m_Units)
		{
			if((unit!=null)&&(neighbourZones.Contains(unit.m_CurrentZone) || unit.m_CurrentZone == m_CurrentZone))
			{
				m_FollowingUnits.Add(unit);
			}
		}
		
		//make the unit follow my shiny boory
		foreach(Unit unit in m_FollowingUnits)
		{
			unit.m_Target = transform.position;
			unit.m_Speed = 100;
		}		
	}
	
	void DestroyMe()
	{
		GameObject.Find("Radio").GetComponent<Radio>().FemaleScream();
		foreach(Unit unit in m_FollowingUnits)
		{
			unit.m_Target = unit.CreateRandomTarget();
			unit.m_Speed = Random.Range(40,80);
		}	
		
		Vector3 pos = transform.position;
		pos.y = 0;
		Instantiate(m_Explosion, pos, transform.rotation);
		Destroy(gameObject);	
	}
	
}
