﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ItemDisplays : MonoBehaviour
{
	public static ItemDisplays itemDisplay;
	public List<GameObject> itemSlots, items;
	public List<Text> amounts;
	public List<Image> backgroundSlots;

	public PlayerBase playerBase;


	void Start () {
		if (!itemDisplay)
			itemDisplay = this;
	}

	public void AssignWandItem (GameObject item, NewWandScript wandScript) {

		if (!playerBase)
			playerBase = FindObjectOfType<PlayerBase> ();

        GameObject _model = wandScript.Model.gameObject;

        if (!_model)
            return;

		GameObject newItem = Instantiate (_model, itemSlots [playerBase.activeItemNum].transform);

		newItem.GetComponent<MeshRenderer> ().material = _model.GetComponent<MeshRenderer>().material;
		newItem.transform.localPosition = new Vector3 (-0.1f, -0.1f, 4.6f);
		newItem.transform.localEulerAngles = new Vector3 (0f, 30f, 130f);
        backgroundSlots [playerBase.activeItemNum].color = wandScript.IconColor;
		backgroundSlots [playerBase.activeItemNum].enabled = true;
        
        // Handle aura colors
	}

    private GameObject GetModelFromChildren(GameObject _item)
    {
        for (int c = 0; c < _item.transform.childCount; c++)
            if (_item.transform.GetChild(c).name == "Model")
                return _item.transform.GetChild(c).gameObject;
            else
                Debug.Log("Checked " + _item.transform.GetChild(c).name + " but was not named Model");

        return null;
    }

	// figure out how to use this for non-wand items//
	public void AssignItem (GameObject item, Material mat, PotionScript potionScript) {

		GameObject newItem = Instantiate (item, itemSlots [playerBase.activeItemNum].transform);
		newItem.GetComponent<DisplayPotion> ().mesh.material = mat;
		newItem.transform.localPosition = new Vector3 (0f, 0f, 4f);
		newItem.transform.localEulerAngles = new Vector3 (0f, 30f, -45f);
		backgroundSlots [playerBase.activeItemNum].color = potionScript.potion.iconColor;
		backgroundSlots [playerBase.activeItemNum].enabled = true;

		UpdateAmount (potionScript);
	}

	public void UpdateAmount (PotionScript potionScript) {
		amounts [playerBase.activeItemNum].text = potionScript.stackNumber.ToString ();
		amounts [playerBase.activeItemNum].enabled = true;
	}

	public void RemoveItem (int refNum) {
		
        if(itemSlots[refNum].transform.childCount > 1)
            Destroy(itemSlots[refNum].transform.GetChild(1).gameObject);
        if(itemSlots[refNum].transform.childCount > 2)
            Destroy (itemSlots [refNum].transform.GetChild (2).gameObject);

        backgroundSlots [playerBase.activeItemNum].color = Color.clear;
		backgroundSlots [playerBase.activeItemNum].enabled = false;
		amounts [playerBase.activeItemNum].text  = "1";
		amounts [playerBase.activeItemNum].enabled = false;
	}
}
