﻿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 AssignItem (GameObject item, Material mat, WandScript wandScript) {

		GameObject newItem = Instantiate (item, itemSlots [playerBase.activeItemNum].transform);
		newItem.GetComponent<MeshRenderer> ().material = mat;
		newItem.transform.localPosition = new Vector3 (-0.1f, -0.1f, 4.6f);
		newItem.transform.localEulerAngles = new Vector3 (0f, 30f, 130f);
        backgroundSlots [playerBase.activeItemNum].color = wandScript.wand.iconColor;
		backgroundSlots [playerBase.activeItemNum].enabled = true;

		if (!newItem.transform.GetChild (0))
			return;

		if (!newItem.transform.GetChild (0).GetComponent<ParticleSystem> ())
			return;
		
		var ps1 = newItem.transform.GetChild (0).GetComponent<ParticleSystem> ().main;
		ps1.startColor = wandScript.wand.aura1Color;


		if (newItem.transform.childCount == 2) {
			var ps2 = newItem.transform.GetChild (1).GetComponent<ParticleSystem> ().main;
			ps2.startColor = wandScript.wand.aura2Color;
		} else if (newItem.transform.childCount == 3) {
			var ps3 = newItem.transform.GetChild (2).GetComponent<ParticleSystem> ().main;
			ps3.startColor = wandScript.wand.aura3Color;
		}
	}

	// 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;
	}
}
