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

public class InventoryHandler : MonoBehaviour
{
    public int value;

    private bool is_item_there;

    private Transform playerLoc;

    public string target_item_name;

    public GameObject mainCanvas;
    public UnityEngine.UI.Image mainInventoryDisplay;


    private GameObject currentCollider;
    private GameObject picked_up_item;

    [SerializeField] protected GameObject grim_user_char;
	[SerializeField] protected Image item_slot;
	[SerializeField] protected PlayerController grimJunior;

	[SerializeField] protected Button pickSelect;
	[SerializeField] protected Button dropSelect;
	[SerializeField] protected Button interactSelect;
	[SerializeField] protected Button scareSelect;

    private Sprite inventory_item;

    public enum _Items
    {
        Backpack,//0
        Cigarette,//1
        Cleaning_Supplies,//2
        Computer,//3
        Coat_hanger,//4
        Hairdryer,//5
        Lamp,//6
        Lightbulb,//7
        Painting,//8
        Plant,//9
        Rug,//10
        Sleeping_Pills,//11
        Smoke_Detector,//12
        Thermostat,//13
        Toothbrush,//14
        Toothpaste,//15
        Towel,//16
        TV,//17
        Water_Cup//18
    }


    #region Sprite_Array
    public Sprite[] _Sprites = new Sprite[]
    {
        Backpack,
        Cigarette,
        Cleaning_Supplies,
        Computer,
        Coat_hanger,
        Hairdryer,
        Lamp,
        Lightbulb,
        Painting,
        Plant,
        Rug,
        Sleeping_Pills,
        Smoke_Detector,
        Thermostat,
        Toothbrush,
        Toothpaste,
        Towel,
        TV,
        Water_Cup
    };
    #endregion


    [SerializeField]
    static Sprite Backpack;
    [SerializeField]
    static Sprite Cleaning_Supplies;
    [SerializeField]
    static Sprite Cigarette;
    [SerializeField]
    static Sprite Computer;
    [SerializeField]
    static Sprite Coat_hanger;
    [SerializeField]
    static Sprite Hairdryer;
    [SerializeField]
    static Sprite Lamp;
    [SerializeField]
    static Sprite Lightbulb;
    [SerializeField]
    static Sprite Painting;
    [SerializeField]
    static Sprite Plant;
    [SerializeField]
    static Sprite Rug;
    [SerializeField]
    static Sprite Sleeping_Pills;
    [SerializeField]
    static Sprite Smoke_Detector;
    [SerializeField]
    static Sprite Towel;
    [SerializeField]
    static Sprite TV;
    [SerializeField]
    static Sprite Water_Cup;
    [SerializeField]
    static Sprite Toothpaste;
    [SerializeField]
    static Sprite Toothbrush;
    [SerializeField]
    static Sprite Thermostat;
    [SerializeField]
    static Sprite Empty;



    void Start()
    {
        if (grim_user_char == null)
        {
            grim_user_char = GameObject.FindWithTag("Player");
        }

        inventory_item = Empty;

        //Check if item is being rendered
        //var target_item_renderer = GameObject.Find(target_item.name).GetComponent<Renderer>();
        //item_renderer = this.gameObject.renderer;
        //is_item_there = item_renderer;

		pickSelect.onClick.AddListener (PickUp_Item);
		dropSelect.onClick.AddListener (Drop_Item);
		scareSelect.onClick.AddListener (Initiate_Scare);
		//pickSelect.onClick.AddListener (PickUp_Item);
    }


    void OnTriggerEnter(Collider other)
    {
		currentCollider = other.gameObject;

		if (other.GetComponent<HumanController> () != null && !other.GetComponent<HumanController>().isDead ) {
			//can scare this person or interact with this person
			scareSelect.gameObject.SetActive(true);
		}
		else if(other.GetComponent<InventoryItem>() != null)
		{
			//can pick up the item
			pickSelect.gameObject.SetActive(true);

		}
    }

	void OnTriggerExit(Collider other)
	{
		interactSelect.gameObject.SetActive (false);
		scareSelect.gameObject.SetActive(false);
		pickSelect.gameObject.SetActive(false);
		dropSelect.gameObject.SetActive (false);
	}




    void PickUp_Item()
    {
		currentCollider.SetActive (false);

		item_slot.sprite = _Sprites [(int)currentCollider.GetComponent<InventoryItem> ().type];
		pickSelect.gameObject.SetActive (false);
    }



    //void Drop_Item(GameObject drop_item, Sprite desired_sprite)
    void Drop_Item()
    {
        Debug.Log("Attempting to Drop the item");

        if (!is_item_there)
        //if (Input.GetKey(KeyCode.E) && !is_item_there)
        {
            Debug.Log("Detected 'E' Press. We are past the logic test.");

            playerLoc = grim_user_char.GetComponent<Transform>();

            Debug.Log("Placing at the player location of: (" + playerLoc.right + "(r), " + playerLoc.up + " (u), " + playerLoc.forward + " (f) )");

            Vector3 new_item_loc = (playerLoc.position);

            //inventory_item = Empty;

            //Debug.Log("Item we are trying to drop is: " + picked_up_item.name);


            picked_up_item.SetActive(true);

            Instantiate(picked_up_item, new_item_loc, Quaternion.identity);
        }
    }

	void Initiate_Scare()
	{
		currentCollider.GetComponent<HumanController> ().ScareHuman ();
		if (currentCollider.GetComponent<HumanController> ().isDead)
			scareSelect.gameObject.SetActive (false);
	}


    /// <summary>
    /// Switch Statement that attempts to use the 'target_item' to get the appropriate Sprite out of the 'Sprite' Array
    /// Returns null if not found
    /// </summary>
    public Sprite get_item_sprite()
    {
        int index = 0;
        Sprite sprite_pic = null;
        string target = target_item_name;
        Debug.Log("The target item name is: '" + target_item_name + "' (switch)");
        switch(target)
        {
            case "Backpack":
                index = (int)_Items.Backpack;
                sprite_pic = _Sprites[index];
                return sprite_pic;
            case "Cigarette":
                index = (int)_Items.Cigarette;
                sprite_pic = _Sprites[index];
                return sprite_pic;
            case "Cleaning_Supplies":
                index = (int)_Items.Cleaning_Supplies;
                sprite_pic = _Sprites[index];
                return sprite_pic;
            case "Computer":
                index = (int)_Items.Computer;
                sprite_pic = _Sprites[index];
                return sprite_pic;
            case "Coat_Hanger":
                index = (int)_Items.Coat_hanger;
                sprite_pic = _Sprites[index];
                return sprite_pic;
            case "Hairdryer":
                index = (int)_Items.Hairdryer;
                sprite_pic = _Sprites[index];
                return sprite_pic;
            case "Lamp":
                index = (int)_Items.Lamp;
                sprite_pic = _Sprites[index];
                return sprite_pic;
            case "Lightbulb":
                index = (int)_Items.Lightbulb;
                sprite_pic = _Sprites[index];
                return sprite_pic;
            case "Painting":
                index = (int)_Items.Painting;
                sprite_pic = _Sprites[index];
                return sprite_pic;
            case "Plant":
                index = (int)_Items.Plant;
                sprite_pic = _Sprites[index];
                return sprite_pic;
            case "Rug":
                index = (int)_Items.Rug;
                sprite_pic = _Sprites[index];
                return sprite_pic;
            case "Sleeping_Pills":
                Debug.Log("Found the right case!");
                index = (int)_Items.Sleeping_Pills;
                Debug.Log("Index value is: " + index.ToString());
                sprite_pic = _Sprites[index];
                Debug.Log("Desired_Sprite we got is: '" + sprite_pic.ToString() + "' (Switch)");
                return sprite_pic;
            case "Smoke_Detector":
                index = (int)_Items.Smoke_Detector;
                sprite_pic = _Sprites[index];
                return sprite_pic;
            case "Thermostat":
                index = (int)_Items.Thermostat;
                sprite_pic = _Sprites[index];
                return sprite_pic;
            case "Toothbrush":
                index = (int)_Items.Toothbrush;
                sprite_pic = _Sprites[index];
                return sprite_pic;
            case "Toothpaste":
                index = (int)_Items.Toothpaste;
                sprite_pic = _Sprites[index];
                return sprite_pic;
            case "Towel":
                index = (int)_Items.Towel;
                sprite_pic = _Sprites[index];
                return sprite_pic;
            case "TV":
                index = (int)_Items.TV;
                sprite_pic = _Sprites[index];
                return sprite_pic;
            case "Water_Cup":
                index = (int)_Items.Water_Cup;
                sprite_pic = _Sprites[index];
                return sprite_pic;

        }

        return sprite_pic;

    }



    //void OnGUI()
    //{

    //}

    void Update()
    {
        //mainInventoryDisplay.sprite = inventory_item;

        //Drop_Item(picked_up_item, inventory_item);

        if (Input.GetKey(KeyCode.E))
        {
            Drop_Item();
        }
        
    }




}