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

public class InventoryScript : MonoBehaviour {

    public Image MyImage;

    public Sprite FireGlyph;
    public Sprite WaterGlyph;
    public Sprite WindGlyph;
    public Sprite EarthGlyph;

    private bool LateInit = false;

	// Use this for initialization
	void Start () {
        
	}	

    void PlayerCharacter_GlyphStackChanged(System.Collections.Generic.Stack<DamageType> stack)
    {
        var nextValues = Mathf.Min(stack.Count, 4);

        for (int i = 0; i < transform.childCount; i++)
        {
            Destroy(transform.GetChild(i).gameObject);
        }

        int numberOfGlyphs = 0;
        foreach (var glyphType in stack)
        {
            var sprite = GetGlyphChildUi(glyphType);

            var image = (Image) Instantiate(MyImage);

            image.sprite = sprite;

            image.transform.SetParent( transform );
            numberOfGlyphs += 1;
            if (numberOfGlyphs == 6)
            {
                break;
            }
        }

    }
	
    Sprite GetGlyphChildUi(DamageType glyphType)
    {
        switch (glyphType)
        {
            case DamageType.Earth:
                return EarthGlyph;
            case DamageType.Fire:
                return FireGlyph;
            case DamageType.Water:
                return WaterGlyph;
            case DamageType.Wind:
                return WindGlyph;
        }
        return null;
    }

    // Update is called once per frame
    void Update()
    {
        if (!LateInit)
        {
            // der playercharacter hat sich nicht unbedingt schon angemeldet, wenn man das hier in
            // der Start() methode macht...
            CharacterManager.instance.PlayerCharacter.GlyphStackChanged += PlayerCharacter_GlyphStackChanged;
            LateInit = true;
        }
	}
}
