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


public class Inventory : MonoBehaviour 
{
	public List<Item> inv = new List<Item>();
	public bool showInv;
	private int _selectedIndex;
	public Item selectedItem;
	public bool fire = false, bridge = false, sling = false;
	public Vector3 playerPos;
	public GameObject plankMesh, fireMesh;
	public Texture2D grey;
	public GameObject targetBridge;
	// Use this for initialization
	void Start () 
	{
		inv.Add(ItemGen.CreateItem(0));
		inv.Add(ItemGen.CreateItem(1));
		inv.Add(ItemGen.CreateItem(2));
		inv.Add(ItemGen.CreateItem(3));
		inv.Add(ItemGen.CreateItem(4));


		inv.Add(ItemGen.CreateItem(100));
		inv.Add(ItemGen.CreateItem(101));
		inv.Add(ItemGen.CreateItem(102));

		for(int i = 0; i < inv.Count; i++)
		{
			inv[i].Amount = 0;
		}
	/*	for(int i = 0; i < 5; i++)
		{
			inv[i].Amount = 10;
		}*/
		fireMesh = Resources.Load("Prefab/"+inv[5].MeshName)as GameObject;
	}
	
	// Update is called once per frame
	void Update () 
	{
		if(Input.GetKeyDown(KeyCode.C))
		{
			ToggleInv();
			playerPos = new Vector3(this.transform.position.x,0.01f,this.transform.position.z);

		}

	}
	public bool ToggleInv()
	{
		if(showInv)
		{
			showInv = false;
			GameObject.Find("Camera").GetComponent<MouseClick>().enabled = true;
			fire = false; bridge = false; sling = false;
			Time.timeScale = 1;
			return (false);
		}
		else
		{
			showInv = true;
			GameObject.Find("Camera").GetComponent<MouseClick>().enabled = false;
			Time.timeScale = 0;
			return (true);
		}
	}
	void OnGUI()
	{
		float scrW = Screen.width/16;
		float scrH = Screen.height/9;
		if(showInv)
		{

			#region Craftables
			if ((!fire && !bridge && !sling))
			{

				if(GUI.Button(new Rect(0.5f*scrW,5f*scrH,0.75f*scrW,0.75f*scrH),inv[5].Icon))
				{
					fire = true;
					bridge = false;
					sling = false;


				}
				
				
				if(GUI.Button(new Rect(0.5f*scrW,6f*scrH,0.75f*scrW,0.75f*scrH),inv[6].Icon))
				{
					fire = false;
					bridge = true;
					sling = false;				
				}
				
				if(inv[7].Amount == 1)
				{
					GUI.DrawTexture(new Rect(0.5f*scrW,7f*scrH,0.75f*scrW,0.75f*scrH),inv[7].Icon);
				}
				else
				{
					if(GUI.Button(new Rect(0.5f*scrW,7f*scrH,0.75f*scrW,0.75f*scrH),inv[7].Icon))
					{				
						fire = false;
						bridge = false;
						sling = true;					
					}
				}


			}
			// // // // // // //
				if(fire && !(bridge || sling))
				{
				GUI.DrawTexture(new Rect(0.5f*scrW,5f*scrH,4.75f*scrW,2.75f*scrH),grey);

				GUI.DrawTexture(new Rect(0.5f*scrW,5f*scrH,0.75f*scrW,0.75f*scrH),inv[5].Icon);

					
				GUI.DrawTexture(new Rect(2.875f*scrW,5.25f*scrH,0.75f*scrW,0.75f*scrH),inv[5].Icon);

				GUI.DrawTexture(new Rect(1.78f*scrW,6.75f*scrH,0.75f*scrW,0.75f*scrH),inv[0].Icon);
				GUI.DrawTexture(new Rect(2.875f*scrW,6.75f*scrH,0.75f*scrW,0.75f*scrH),inv[2].Icon);
				GUI.DrawTexture(new Rect(3.92f*scrW,6.75f*scrH,0.75f*scrW,0.75f*scrH),inv[4].Icon);

				GUI.Box(new Rect(1.78f*scrW,6.75f*scrH,0.75f*scrW,0.75f*scrH),"3");
				GUI.Box(new Rect(2.875f*scrW,6.75f*scrH,0.75f*scrW,0.75f*scrH),"2");
				GUI.Box(new Rect(3.92f*scrW,6.75f*scrH,0.75f*scrW,0.75f*scrH),"1");

				if(inv[5].Amount > 0)
				{
					//use button
					if(GUI.Button(new Rect(3.92f*scrW,5.25f*scrH,1f*scrW,0.5f*scrH),"Use"))
					{
						Instantiate(fireMesh,playerPos,Quaternion.identity );
						inv[5].Amount -= 1;

					}
					//instantiate at player feet
					//make fire orange
				}
				if(inv[0].Amount >= 3 && inv[2].Amount >= 2 && inv[4].Amount >=1)
				{
					//crate button
					if(GUI.Button(new Rect(3.92f*scrW,6f*scrH,1f*scrW,0.5f*scrH),"Create"))
					{
						//remove items
						inv[0].Amount -= 3;
						inv[2].Amount -= 2;
						inv[4].Amount -=1;
						//add item
						inv[5].Amount += 1;
					}

				}
				//5.25 - 75 58
					if(GUI.Button(new Rect(0.5f*scrW,6f*scrH,0.75f*scrW,0.75f*scrH),inv[6].Icon))
					{
						fire = false;
						bridge = true;
						sling = false;				
					}
					if(inv[7].Amount == 1)
					{
						GUI.DrawTexture(new Rect(0.5f*scrW,7f*scrH,0.75f*scrW,0.75f*scrH),inv[7].Icon);
					}
					else
					{
						if(GUI.Button(new Rect(0.5f*scrW,7f*scrH,0.75f*scrW,0.75f*scrH),inv[7].Icon))
						{				
							fire = false;
							bridge = false;
							sling = true;					
						}
					}
				}
			else if(bridge && !(fire || sling))
				{
				GUI.DrawTexture(new Rect(0.5f*scrW,5f*scrH,4.75f*scrW,2.75f*scrH),grey);


				
				GUI.Box(new Rect(2.875f*scrW,5.25f*scrH,0.75f*scrW,0.75f*scrH),inv[6].Icon);
				
				GUI.DrawTexture(new Rect(1.78f*scrW,6.75f*scrH,0.75f*scrW,0.75f*scrH),inv[1].Icon);
				GUI.DrawTexture(new Rect(2.875f*scrW,6.75f*scrH,0.75f*scrW,0.75f*scrH),inv[2].Icon);
				GUI.DrawTexture(new Rect(3.92f*scrW,6.75f*scrH,0.75f*scrW,0.75f*scrH),inv[3].Icon);

				GUI.Box(new Rect(1.78f*scrW,6.75f*scrH,0.75f*scrW,0.75f*scrH),"4");
				GUI.Box(new Rect(2.875f*scrW,6.75f*scrH,0.75f*scrW,0.75f*scrH),"5");
				GUI.Box(new Rect(3.92f*scrW,6.75f*scrH,0.75f*scrW,0.75f*scrH),"2");

				if(inv[6].Amount > 0 && targetBridge != null)
				{
					//use button
					if(GUI.Button(new Rect(3.92f*scrW,5.25f*scrH,1f*scrW,0.5f*scrH),"Use"))
					{
						targetBridge.GetComponent<Bridge>().bridge = true;
						targetBridge = null;
						//Instantiate(fireMesh,playerPos,Quaternion.identity );
						inv[6].Amount -= 1;
						
					}

				}
				if(inv[1].Amount >= 4 && inv[2].Amount >= 5 && inv[3].Amount >=2)
				{
					//crate button
					if(GUI.Button(new Rect(3.92f*scrW,6f*scrH,1f*scrW,0.5f*scrH),"Create"))
					{
						//remove items
						inv[1].Amount -= 4;
						inv[2].Amount -= 5;
						inv[3].Amount -=2;
						//add item
						inv[6].Amount += 1;
					}
					
				}

					if(GUI.Button(new Rect(0.5f*scrW,5f*scrH,0.75f*scrW,0.75f*scrH),inv[5].Icon))
					{
						fire = true;
						bridge = false;
						sling = false;
						
						
					}
					GUI.DrawTexture(new Rect(0.5f*scrW,6f*scrH,0.75f*scrW,0.75f*scrH),inv[6].Icon);
					if(inv[7].Amount == 1)
					{
						GUI.DrawTexture(new Rect(0.5f*scrW,7f*scrH,0.75f*scrW,0.75f*scrH),inv[7].Icon);
					}
					else
					{
						if(GUI.Button(new Rect(0.5f*scrW,7f*scrH,0.75f*scrW,0.75f*scrH),inv[7].Icon))
						{				
							fire = false;
							bridge = false;
							sling = true;					
						}
					}
					
				}
				else if(sling &&!(fire||bridge))
				{
				GUI.DrawTexture(new Rect(0.5f*scrW,5f*scrH,4.75f*scrW,2.75f*scrH),grey);
				
				
				
				GUI.DrawTexture(new Rect(2.875f*scrW,5.25f*scrH,0.75f*scrW,0.75f*scrH),inv[7].Icon);
				
				GUI.DrawTexture(new Rect(1.78f*scrW,6.75f*scrH,0.75f*scrW,0.75f*scrH),inv[0].Icon);
				GUI.DrawTexture(new Rect(2.875f*scrW,6.75f*scrH,0.75f*scrW,0.75f*scrH),inv[1].Icon);
				GUI.DrawTexture(new Rect(3.92f*scrW,6.75f*scrH,0.75f*scrW,0.75f*scrH),inv[2].Icon);

				GUI.Box(new Rect(1.78f*scrW,6.75f*scrH,0.75f*scrW,0.75f*scrH),"3");
				GUI.Box(new Rect(2.875f*scrW,6.75f*scrH,0.75f*scrW,0.75f*scrH),"2");
				GUI.Box(new Rect(3.92f*scrW,6.75f*scrH,0.75f*scrW,0.75f*scrH),"1");

				if(inv[7].Amount > 0 && inv[0].Amount >= 1)
				{
					bool show = false;
					//use button
					if(GUI.Button(new Rect(3.92f*scrW,5.25f*scrH,1f*scrW,0.5f*scrH),"Use"))
					{
						show = true;
						//Instantiate(fireMesh,playerPos,Quaternion.identity );
						//inv[6].Amount -= 1;
						//inv[2].Amount -= 1;
					}
					if(show = true)
					{
						GUI.Box(new Rect(0.5f*scrW,4f*scrH,4.75f*scrW,0.75f*scrH),"Left Click to Use in Game");

					}
					if(! sling)
					{
						show = false;
					}
					//instantiate at player feet
					//make fire orange
				}
				if(inv[7].Amount <= 0 && inv[0].Amount >= 3 && inv[1].Amount >= 2 && inv[2].Amount >=1)
				{
					//crate button
					if(GUI.Button(new Rect(3.92f*scrW,6f*scrH,1f*scrW,0.5f*scrH),"Create"))
					{
						//remove items
						inv[0].Amount -= 3;
						inv[1].Amount -= 2;
						//inv[2].Amount -=1;
						//add item
						inv[7].Amount += 1;
					}
					
				}
					if(GUI.Button(new Rect(0.5f*scrW,5f*scrH,0.75f*scrW,0.75f*scrH),inv[5].Icon))
					{
						fire = true;
						bridge = false;
						sling = false;
						
						
					}
					if(GUI.Button(new Rect(0.5f*scrW,6f*scrH,0.75f*scrW,0.75f*scrH),inv[6].Icon))
					{
						fire = false;
						bridge = true;
						sling = false;				
					}
					GUI.DrawTexture(new Rect(0.5f*scrW,7f*scrH,0.75f*scrW,0.75f*scrH),inv[7].Icon);

				}



			#endregion
		}

		#region Items
		GUI.DrawTexture(new Rect(0.5f*scrW,8.0f*scrH,0.75f*scrW,0.75f*scrH),inv[0].Icon);
		GUI.DrawTexture(new Rect(1.5f*scrW,8.0f*scrH,0.75f*scrW,0.75f*scrH),inv[1].Icon);
		GUI.DrawTexture(new Rect(2.5f*scrW,8.0f*scrH,0.75f*scrW,0.75f*scrH),inv[2].Icon);
		GUI.DrawTexture(new Rect(3.5f*scrW,8.0f*scrH,0.75f*scrW,0.75f*scrH),inv[3].Icon);
		GUI.DrawTexture(new Rect(4.5f*scrW,8.0f*scrH,0.75f*scrW,0.75f*scrH),inv[4].Icon);

		GUI.Box(new Rect(0.5f*scrW,8.0f*scrH,0.75f*scrW,0.75f*scrH),inv[0].Amount.ToString());
		GUI.Box(new Rect(1.5f*scrW,8.0f*scrH,0.75f*scrW,0.75f*scrH),inv[1].Amount.ToString());
		GUI.Box(new Rect(2.5f*scrW,8.0f*scrH,0.75f*scrW,0.75f*scrH),inv[2].Amount.ToString());
		GUI.Box(new Rect(3.5f*scrW,8.0f*scrH,0.75f*scrW,0.75f*scrH),inv[3].Amount.ToString());
		GUI.Box(new Rect(4.5f*scrW,8.0f*scrH,0.75f*scrW,0.75f*scrH),inv[4].Amount.ToString());

		#endregion
	}
}






