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

public class HUD : Singleton<HUD> {

    // Exposed variables & types
    //--------------------------------

    [SerializeField] GameObject m_blockIndicator;
    [SerializeField] GameObject m_blockPiecePrefab;

    [SerializeField] bool m_startFadeIn;
    [SerializeField] Animator m_fadeAnimator;

    [SerializeField] GameObject m_deathPopup;
	[SerializeField] GameObject m_holdEscToQuit;

    // Private variables 
    //--------------------------------

	int m_displayingCurrentBlocks;
    float m_escHeldTime = 0;

    List<GameObject> m_blocks = new List<GameObject>();
    
    // Public methods 
    //--------------------------------

    public void ShowDeathPopup()
    {
        m_deathPopup.gameObject.SetActive(true);
    }

    public void SetFade(bool fade)
    {
        if (fade)
        {
            m_fadeAnimator.Play("ToIn");
        }
        else
        {
            m_fadeAnimator.Play("ToOut");
        }
    }
    
    // Private methods 
    //--------------------------------

    void Awake()
    {
        m_deathPopup.gameObject.SetActive(false);
        m_fadeAnimator.gameObject.SetActive(true);

        if (m_startFadeIn)
        {
            m_fadeAnimator.Play("ToOut");
        }
    }
   	
    void Update () 
    {
        Player player = FindObjectOfType<Player>();
        BodyBlockManager bodyBlockManager = player.GetComponentInChildren<BodyBlockManager>();

        SetMaxBlocks(bodyBlockManager.GetMaxBlocks());
        SetCurrentBlocks(bodyBlockManager.GetCurrentBlocks(), bodyBlockManager.GetMaxBlocks());

        if ( Input.GetKey(KeyCode.Escape) )
        {
			m_holdEscToQuit.SetActive(true);
			m_escHeldTime += Time.deltaTime;
			if ( m_escHeldTime > 1.5f )
			{
				Application.Quit();
			}

        }
        else 
        {
			m_holdEscToQuit.SetActive(false);
			m_escHeldTime = 0;
        }
    }
    void SetCurrentBlocks(int blocks, int maxBlocks)
    {
        if (m_blocks.Count == 1 && maxBlocks == 1)
        {
            m_blocks[0].transform.FindChild("Full").gameObject.SetActive(false);
            m_blocks[0].transform.FindChild("Empty").gameObject.SetActive(false);
        }
        else
        {
            int blocksRemaining = maxBlocks - blocks;
            for (int i = 0; i < m_blocks.Count; i++)
            {
                if (i < blocksRemaining)
                {
                    m_blocks[i].transform.FindChild("Full").gameObject.SetActive(true);
                }
                else
                {
                    m_blocks[i].transform.FindChild("Full").gameObject.SetActive(false);                    
                }

                m_blocks[i].transform.FindChild("Empty").gameObject.SetActive(true);
            }
        }
    }

    void SetMaxBlocks(int blockCount)
    {
        if (blockCount > 1)
        {
            blockCount -= 1;
        }

        while (m_blocks.Count != blockCount)
        {
            if (blockCount > m_blocks.Count)
            {
                GameObject blockPiece = Instantiate(m_blockPiecePrefab);
                blockPiece.transform.SetParent(m_blockIndicator.transform, false);
                m_blocks.Add(blockPiece);
            }
            else if (blockCount < m_blocks.Count)
            {
                Destroy(m_blocks[0]);
                m_blocks.RemoveAt(0);
            }            
        }
    }
}
