using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//=============================================================================
// This script was created by Matt Parkin as a part of the Polygon Pilgrimage
// Go to www.youtube.com/mattparkin and subscribe for more awesome tutorials!
// You are free to use/modify/edit this script however you like.  Please give
// credit to the orginal creator and share your work!
//=============================================================================
public class BRS_InventoryManager : MonoBehaviour
{
	public int numSlots = 6;
	public InventoryItem[] ItemInv;
	public int nextFreeSlot;
	public GameObject newIcon;
	public GameObject InventoryUIArea;
	private Transform[] SlotCenters;

	public FN_ItemManager inItemMgr;
	public string inItemName;
	public bool canStack = false;
	public bool canAdd = false;

	private bool isViewingInventory = true;
	public GameObject InventoryOverlay;
	private Animator ThirdPersonAnimator;
	public GameObject TPC;
	private UnityStandardAssets.Characters.ThirdPerson.ThirdPersonUserControl PlayerControlScript;

	private GameObject _BRS_Mechanics;
	//private BRS_UIManager _UIM;

	// Use this for initialization
	void Start ()
	{
		InventoryOverlay = GameObject.Find("ViewingInventory");
		InventoryOverlay.SetActive(false);
		TPC = GameObject.Find ("ThirdPersonController");
		ItemInv = new InventoryItem[numSlots];
		_BRS_Mechanics = GameObject.Find ("BRS_Mechanics");
		//_UIM = GameObject.Find ("BRS_UI").GetComponent<BRS_UIManager> ();
		InventoryUIArea = GameObject.Find("InvUI");
		ThirdPersonAnimator = TPC.GetComponent<Animator> ();
		PlayerControlScript = TPC.GetComponent<UnityStandardAssets.Characters.ThirdPerson.ThirdPersonUserControl> ();
		SlotCenters = InventoryUIArea.GetComponentsInChildren<Transform> ();
	}

	// Update is called once per frame
	void Update ()
	{
		if (Input.GetKeyUp ("i"))
		{
			isViewingInventory = InvScreenViewing(isViewingInventory);
		}

		//if (Input.GetKeyUp ("k"))
		//{
		//	RemoveFromInventory (0);
		//}
	}

	public bool InvScreenViewing(bool viewState)
	{
		if (viewState == true)
		{
			//If we are going to show the inventory
			ThirdPersonAnimator.SetFloat("Forward", 0f);
			ThirdPersonAnimator.SetFloat("Turn", 0f);
			PlayerControlScript.enabled = false;

			//Show the overlay
			InventoryOverlay.SetActive(true);
		}
		else
		{
			//If we are NOT going to show the inventory
			PlayerControlScript.enabled = true;

			//Hide the overlay
			InventoryOverlay.SetActive(false);
		}
		return !viewState;
	}

	public InventoryItem AddToInventory(InventoryItem newItem)
	{
		canAdd = false;

		//Get a look at what type of item we are adding
		//inItemMgr = newItem.GetComponent<FN_ItemManager> ();

		//Get the name of the item
		inItemName = newItem.ItemName;
		Debug.Log (inItemName);

		//Do we already have one of those items?
		//TODO

		//Is it stackable?
		canStack = newItem.isStackable;

		//If we don't already have one, and it is stackable...
		if (canStack)
		{
			Debug.Log ("Increase the inventory for this item.");
			return newItem;
		}
		else
		{
			//If we don't already have one, and its NOT stackable...
			Debug.Log("Attempt to add it to our inventory if we have room");
		}
			
		//Do we have room?
		//If we get back a -1 that means that we are full up!
		if (NextFreeSlot () < 0)
		{
			Debug.Log ("You are overincumbered!");
			return null;
		}
		else
		{
			Debug.Log ("added to inventory");
			var newSlotID = NextFreeSlot ();

			ItemInv [newSlotID] = newItem;
			canAdd = true;

			//_UIM.AddInventoryIcon (newItem, newSlotID);
			AddInventoryIcon (newItem, newSlotID);
		}
		//canAdd = false;
		return newItem;
	}

	public void RemoveFromInventory(int SlotNum)
	{
		var dropItem = ItemInv [SlotNum].LootPickUp;
		ItemInv [SlotNum] = null;
		Instantiate(dropItem, TPC.transform.position, new Quaternion(0,0,0,0));
	}

	public void UpdateInventory(int SlotNum1, int SlotNum2)
	{
		var tempSlot = ItemInv [SlotNum2];
		ItemInv [SlotNum2] = ItemInv [SlotNum1];
		ItemInv [SlotNum1] = tempSlot;
	}

	public int NextFreeSlot()
	{
		nextFreeSlot = System.Array.IndexOf (ItemInv, null);
		//Return the next free slot index
		return nextFreeSlot;
	}

	public void AddInventoryIcon(InventoryItem item, int SlotNum)
	{
		newIcon = Instantiate (item.InvTile);
		newIcon.transform.SetParent (InventoryUIArea.transform);
		newIcon.transform.localScale = new Vector3 (1, 1, 1);

		var Magic = 40 + (SlotNum * 92);
		newIcon.transform.localPosition = new Vector3 ((Magic), 40, 0);
	}
}