using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Text;

public class Inventory : MonoBehaviour
{
	public Image[] itemImages = new Image[numItemSlots];
	public Item[] items = new Item[numItemSlots];
	public const int numItemSlots = 25;

	void Awake()
	{
		//PlayerPrefs.SetString("inventory", "");
		string savedString = PlayerPrefs.GetString("inventory");

		string[] itemNames = savedString.Split(' ');
		Debug.Log (itemNames [itemNames.Length-1]);
		for (int i = 0; i < itemNames.Length - 1; i++)
		{
			Sprite sprite = (Resources.Load<Sprite>(itemNames[i] + "_sprite"));
			Item itm = new Item ();
			itm.name = itemNames [i];
			itm.sprite = sprite;
			items[i] = itm;
			itemImages[i].sprite = itm.sprite;
			itemImages[i].enabled = true;
		}
	}

	public void AddItem(Item itemToAdd)
	{
		StringBuilder invList = new StringBuilder ();
		invList.Append(PlayerPrefs.GetString ("inventory"));
		invList.Append(itemToAdd.name).Append(" ");
		PlayerPrefs.SetString ("inventory", invList.ToString());

		for (int i = 0; i < items.Length; i++)
		{
			if (items[i] == null)
			{
				items[i] = itemToAdd;
				itemImages[i].sprite = itemToAdd.sprite;
				itemImages[i].enabled = true;
				return;
			}
		}
	}

	public void AddItem(string itemName)
	{
		StringBuilder invList = new StringBuilder ();
		invList.Append(PlayerPrefs.GetString ("inventory"));
		invList.Append(itemName).Append(" ");
		PlayerPrefs.SetString ("inventory", invList.ToString());

		for (int i = 0; i < items.Length; i++)
		{
			if (items[i] == null)
			{
				Sprite sprite = (Resources.Load<Sprite>(itemName + "_sprite"));
				Item itm = new Item ();
				itm.name = itemName;
				itm.sprite = sprite;
				items[i] = itm;
				itemImages[i].sprite = itm.sprite;
				itemImages[i].enabled = true;
				return;
			}
		}
	}

	public void RemoveItem (Item itemToRemove)
	{
		StringBuilder invList = new StringBuilder ();
		invList.Append(PlayerPrefs.GetString ("inventory"));
		int index = invList.ToString ().IndexOf (itemToRemove.name + " ");
		invList.Remove(index, (itemToRemove.name + " ").Length);
		PlayerPrefs.SetString ("inventory", invList.ToString());
		GameObject instance = (GameObject)Instantiate(Resources.Load(itemToRemove.name));
		instance.name = itemToRemove.name;

		for (int i = 0; i < items.Length; i++)
		{
			if (items[i].name == itemToRemove.name)
			{
				items[i] = null;
				itemImages[i].sprite = null;
				itemImages[i].enabled = false;
				return;
			}
		}
	}

	public void RemoveItem (string itemName)
	{
		StringBuilder invList = new StringBuilder ();
		invList.Append(PlayerPrefs.GetString ("inventory"));
		int index = invList.ToString ().IndexOf (itemName + " ");
		invList.Remove(index, (itemName + " ").Length);
		PlayerPrefs.SetString ("inventory", invList.ToString());
		GameObject instance = (GameObject)Instantiate(Resources.Load(itemName));
		instance.GetComponent<DragAndDrop> ().inventory = this;
		instance.name = itemName;

		for (int i = 0; i < items.Length - 1; i++)
		{
			if (items [i] != null) {
				Debug.Log (items [i].name + " == " + itemName);
				if (items [i].name == itemName) {
					items [i] = null;
					itemImages [i].sprite = null;
					itemImages [i].enabled = false;
					return;
				}
			}
		}

	}
}