using UnityEngine;
using System.Collections;

public static class CombatLog  {
	public static string[] name = new string[32];
	public static int[] score = new int[32];
	public static int[] kills= new int[32];
	public static int[] deaths= new int[32];
	
	public static bool initialized=false;
	public static void Init(int units)
	{
		if(!initialized)
		{
			/*score = new int[units];
			kills = new int[units];
			deaths = new int[units];
			initialized=true;*/
		}
	}
	
	public static void Clear()
	{
		name.Equals("");
		score.Equals(0);
		kills.Equals(0);
		deaths.Equals(0);
	}
	
	
	public static void Killed(int who, int whom)
	{
		if(who>-1 & whom>-1)
		{
			if(who!=whom)
			{
				score[who]+=1;
				kills[who]+=1;
			}else{
				score[who]--;
			}
			
			deaths[whom]+=1;
			
			
			
			//Debug.Log(who+" owned "+whom);
			if(GameObject.FindGameObjectWithTag("MainCamera"))
			{
				GameObject.FindGameObjectWithTag("MainCamera").transform.GetComponent<CombatHUD>().AddCombat(GetName(who),GetName(whom));
			}
		}
	}
	
	public static void DrawTable()
	{
		float xSizePercent = Screen.width / 1680.0f;
		float ySizePercent = Screen.height / 1050.0f;	
		
		
		Rect rect = new Rect(Screen.width*0.125f,Screen.height*0.125f,Screen.width*0.75f,Screen.height*0.75f);
		GUI.Box(rect,"");
		int y = GUI.skin.label.fontSize;
		GUI.skin.label.fontSize = 24;
		
		rect.y+=5;
		rect.x = Screen.width*0.125f + 10;
		GUI.Label(rect,"Name");
		rect.x += 150*xSizePercent;
		GUI.Label(rect,"Score");
		rect.x += 150*xSizePercent;
		GUI.Label(rect,"Kills");
		rect.x += 150*xSizePercent;
		GUI.Label(rect,"Deaths");
		rect.x += 150*xSizePercent;
		GUI.Label(rect,"ID");
		rect.y+=75*ySizePercent;
		for(int i=0;i<GameBrain.unitCount;i++)
		{
			rect.y+=50*ySizePercent;
			rect.x = Screen.width*0.125f + 10;
			GUI.Label(rect,name[i]);
			rect.x += 150*xSizePercent;
			GUI.Label(rect,score[i]+"");
			rect.x += 150*xSizePercent;
			GUI.Label(rect,kills[i]+"");
			rect.x += 150*xSizePercent;
			GUI.Label(rect,deaths[i]+"");
			rect.x += 150*xSizePercent;
			GUI.Label(rect,i+"");
			
		}
		
		GUI.skin.label.fontSize = y;
	}
	
	public static string GetName(int id)
	{
		foreach(UnitStatus b in GameBrain.units)
		{
			if(b.id==id)
			{
				return b.name;
			}
		}
		return "";
	}
	
	
	public static void SpreeUpdate(int who,int whom)
	{
		foreach(UnitStatus b in GameBrain.units)
		{
			if(b.id==who)
			{
				if(b.lastKill<10)
				{
					b.currentKills++;
				} else {
					b.currentKills=0;
				}
				
				b.spree++;
				b.lastKill=0;
			}
			
			if(b.id==whom)
			{
				b.spree=0;
				b.lastKill=0;
				b.currentKills=0;
			}
			
			if(who==0)
			{
				GameObject.FindGameObjectWithTag("MainCamera").GetComponent<CombatHUD>().KillingSpree(b.spree,true);
				GameObject.FindGameObjectWithTag("MainCamera").GetComponent<CombatHUD>().KillingSpree(b.currentKills,false);
			}

		}
	}
}
