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

public class SommoningUI : MonoBehaviour {

    public GameObject AttackPanel;
    public GameObject DefensePanel;
    public GameObject SpeedPanel;
    public GameObject DestroyGlyphPanel;
    public GameObject DestroyGlyphEffectPrefab;
    public Spawner SpawnerObj;

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

    public Image MyImage;

	// Use this for initialization
	void Start () {
        SpawnerObj.AddedGlyphToSummoningStack += SpawnerObj_AddedGlyphToSummoningStack;
        SpawnerObj.DestroyedGlyphFromSummoningStack += SpawnerObj_DestroyedGlyphFromSummoningStack;
        GameStateController.Instance.GameStateChanged += Instance_GameStateChanged;
	}

    void Instance_GameStateChanged(EGameState oldState, EGameState newState)
    {
        if (newState == EGameState.Summoming)
        {
            List<DamageType> emptyList = new List<DamageType>();
            rebuildStack(getStackChild(1), emptyList);
            rebuildStack(getStackChild(2), emptyList);
            rebuildStack(getStackChild(3), emptyList);

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

    IEnumerator DelayDestroyGylphPanel()
    {
        yield return new WaitForSeconds(3.0f * 0.05f); // because we can
        for (int i = 0; i < DestroyGlyphPanel.transform.childCount; i++)
        {
            Destroy(DestroyGlyphPanel.transform.GetChild(i).gameObject);
        }
        GameObject particle = GameObject.Instantiate(DestroyGlyphEffectPrefab, DestroyGlyphPanel.transform.position, Quaternion.identity) as GameObject;
        particle.transform.parent = DestroyGlyphPanel.transform.parent;
    }

    void SpawnerObj_DestroyedGlyphFromSummoningStack(List<DamageType> summoningStack, int listIndex, DamageType destroyedSummonGlyph, DamageType destroyedStackGlyph)
    {
        StopAllCoroutines();
        for (int i = 0; i < DestroyGlyphPanel.transform.childCount; i++)
        {
            Destroy(DestroyGlyphPanel.transform.GetChild(i).gameObject);
        }
        var summonsprite = GetGlyphChildUi(destroyedSummonGlyph);
        var summonimage = (Image)Instantiate(MyImage);
        summonimage.sprite = summonsprite;
        summonimage.color = new Color(1.0f, 0.5f, 0.5f);
        summonimage.transform.SetParent(DestroyGlyphPanel.transform);

        var stacksprite = GetGlyphChildUi(destroyedStackGlyph);
        var stackimage = (Image)Instantiate(MyImage);
        stackimage.sprite = stacksprite;
        stackimage.color = new Color(1.0f, 0.5f, 0.5f);
        stackimage.transform.SetParent(DestroyGlyphPanel.transform);

        StartCoroutine(DelayDestroyGylphPanel());


        rebuildStack(getStackChild(listIndex), summoningStack);
    }

    void SpawnerObj_AddedGlyphToSummoningStack(List<DamageType> summoningStack, int listIndex, DamageType glyph)
    {

        rebuildStack(getStackChild(listIndex), summoningStack);
    }

    void rebuildStack(GameObject stackChild, List<DamageType> stackList)
    {
        for (int i = 0; i < stackChild.transform.childCount; i++)
        {
            Destroy(stackChild.transform.GetChild(i).gameObject);
        }

        foreach (var glyphType in stackList)
        {
            var sprite = GetGlyphChildUi(glyphType);

            var image = (Image)Instantiate(MyImage);

            image.sprite = sprite;

            image.transform.SetParent(stackChild.transform);
        }
    }

    void DestroyGlyph(GameObject glyph)
    {

    }

    GameObject getStackChild(int stackIndex)
    {
        switch (stackIndex)
        {
            case 1: return AttackPanel;
            case 2: return DefensePanel;
            case 3: return SpeedPanel;
            default: return null;
        }
    }

    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 () {
	
	}
}
