using UnityEngine;
using System.Collections;

public class UnitStatus : MonoBehaviour {
	public string name;
	
	public int health;
	public int armor;
	
	public int[] Ammo;
	
	public int id;
	public float reviveTime;
	
	private float time;
	public bool alive;
	
	public int spree;
	public int currentKills;
	public float lastKill;
	
	void Awake()
	{
		if(!transform.GetComponent<AIscript>())
		{
			if(transform.GetComponent<AIscript>())
		{
			name = GameBrain.names[Random.Range(0,GameBrain.names.Length-1)];
		}
		
		
		id = GameBrain.lastUnitID;
		
		GameBrain.units.Add(this);
		GameBrain.unitCount++;
		CombatLog.name[id] = name;
		GameBrain.lastUnitID++;
		alive=true;
		
		CombatLog.Init(GameBrain.units.Count);
		
		}
	}
	
	// Use this for initialization
	void Start () {
		
		if(transform.GetComponent<AIscript>())
		{
			if(transform.GetComponent<AIscript>())
			{
				name = GameBrain.names[Random.Range(0,GameBrain.names.Length-1)];
			}
			
			
			id = GameBrain.lastUnitID;
			
			GameBrain.units.Add(this);
			GameBrain.unitCount++;
			CombatLog.name[id] = name;
			GameBrain.lastUnitID++;
			alive=true;
			
			CombatLog.Init(GameBrain.units.Count);
			
			
			//HOW MANY PLAYERS
			if(GameBrain.unitCount>GameOptions.players & transform.GetComponent<AIscript>())
			{
				Death(-1);
				this.enabled=false;
				GameBrain.unitCount--;
				DestroyImmediate(gameObject);
			}
		}
	}
	
	// Update is called once per frame
	void Update () {
		lastKill+=Time.deltaTime;
		
		if(alive==false)
		{
			time-=Time.deltaTime;
			if(time<0)
				time=0;
		
			if(time==0)
			{
				Revive();
			}
		}
	}
	
	public void ApplyDamage(float damage,int id2)
	{
		if(id == id2)
			damage/=4;
		
		if(armor>0)
		{
			health-= (int)(damage*0.5f);
			armor-=(int)damage;
		}else
		{
			health-= (int)damage;
		}
		
		if(health<=0)
		{
			Death(id2);
		}
		if(armor<0)
			armor=0;
		
		if(transform.GetComponent<PlayerControll>())
			transform.GetComponent<HUD>().Auch();
			
			
	}
	
	public void ApplyBonus(int hp,bool hu, int arm, int[] amo)
	{
		health+=hp;
		if( health>100 & hu==false)
			health-=hp;
		armor+=arm;
		
		for(int i=0;i<Ammo.Length;i++)
		{
			Ammo[i]+=amo[i];	
		}
		
		if(health>200)
			health=200;
		if(armor>200)
			armor=200;
	}
	
	public void Death(int id2)
	{	
		if(transform.GetComponent<AIscript>())
		{
			Destroy(transform.GetComponent<AIscript>().preview);
		}else{
			
		}
		audio.Stop();
		CombatLog.Killed(id2,id);
		CombatLog.SpreeUpdate(id2,id);
		
		GameBrain.units.Remove(this);
		
		transform.collider.enabled = false;
		if(transform.rigidbody)
			transform.rigidbody.useGravity = false;
		
		alive=false;
		time=reviveTime;
		
		if(transform.GetComponent<AIscript>())
		{
			transform.GetComponent<AIscript>().walkStep=-1;
			transform.GetComponent<AIscript>().enabled=false;
		}
		
		if(transform.GetComponent<PlayerControll>())
		{
			transform.GetComponent<PlayerControll>().enabled=false;
			transform.GetComponent<HUD>().enabled=false;
			transform.FindChild("camera").gameObject.camera.enabled = false;
			transform.FindChild("camera").GetComponent<AudioListener>().enabled=false;
			
			GameObject.FindGameObjectWithTag("MainCamera").camera.active = true;
			GameObject.FindGameObjectWithTag("MainCamera").GetComponent<MouseLook>().enabled = true;
			GameObject.FindGameObjectWithTag("MainCamera").GetComponent<FreeCam>().enabled = true;
			GameObject.FindGameObjectWithTag("MainCamera").GetComponent<AudioListener>().enabled=true;
		}
		
		if(transform.renderer)
		{
			transform.renderer.enabled=false;
		}
		else
		{
			for(int i=0;i<transform.GetChildCount();i++)
			{
				if(transform.GetChild(i).renderer)
					transform.GetChild(i).renderer.enabled=false;	
			}
		}
	}
	
	public void Revive()
	{	
		GameBrain.units.Add(this);
		
		
		//revivePoint
		Transform m = GameObject.FindGameObjectWithTag("Game").transform;
		transform.position = m.FindChild("revivePoint_"+Random.Range(0,m.GetChildCount())).transform.position;
		
		transform.collider.enabled = true;
		if(transform.rigidbody)
			transform.rigidbody.useGravity = true;
		
		if(transform.GetComponent<AIscript>())
		{
			transform.GetComponent<AIscript>().enabled=true;
		}
		
		alive=true;
		health=100;
		armor=0;
		for(int y=0;y<Ammo.Length;y++)
		{
			Ammo[y]=0;
		}
		Ammo[0] = 150;
		
		
		if(transform.GetComponent<AIscript>())
		{
			transform.GetComponent<AIscript>().enabled=true;
		}
		else if(transform.GetComponent<PlayerControll>())
		{
			transform.GetComponent<PlayerControll>().SetWeapon();
			
			transform.GetComponent<PlayerControll>().enabled=true;
			transform.GetComponent<HUD>().enabled=true;
			transform.FindChild("camera").gameObject.camera.enabled = true;
			transform.FindChild("camera").GetComponent<AudioListener>().enabled=true;
			
			//GameObject.FindGameObjectWithTag("MainCamera").camera.active = false;
			GameObject.FindGameObjectWithTag("MainCamera").GetComponent<MouseLook>().enabled = false;
			GameObject.FindGameObjectWithTag("MainCamera").GetComponent<FreeCam>().enabled = false;
			GameObject.FindGameObjectWithTag("MainCamera").GetComponent<AudioListener>().enabled=false;
		}
		
		
		if(transform.renderer)
		{
			transform.renderer.enabled=true;
		}
		else
		{
			for(int i=0;i<transform.GetChildCount();i++)
			{
				if(transform.GetChild(i).renderer)
					transform.GetChild(i).renderer.enabled=true;	
			}
		}
	}
}
