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

public class UIAbility : MonoBehaviour
{
    [Range(0,3)]
    public int PlayerIndex = 0;

    [Range(0,3)]
    public int AbilityIndex = 0;

    public GameObject Panel;

    private Player m_Owner = null;
    private Ability m_Ability = null;

    private Text m_Countdown;
    private Image m_Image;


    void Start ()
    {
        m_Image = transform.Find("AbilityIcon").GetComponent<Image>();
        m_Countdown = transform.Find("AbilityCountdown").GetComponent<Text>();
    }

    void FindMyAbility()
    {
        PlayerManager pm = GameObject.FindObjectOfType<PlayerManager>();

        if (!pm)
            return;

        m_Owner = pm.FindPlayerWithIndex(PlayerIndex);

        if (m_Owner == null)
            return;

        m_Ability = m_Owner.m_Ability;

        if (m_Ability == null)
            return;

        m_Image.sprite = Resources.Load<Sprite>(m_Ability.Icon);

        //m_Image.sprite = Resources.Load<Sprite>("icon-confuse");
        
    }

    // Update is called once per frame
    void Update()
    {
        if (m_Ability == null)
        {
            FindMyAbility();
            return;
        }

        Color c = m_Image.color;


        // try to catch ability ingame
        if (m_Ability.TimeLeft > 0)
        {
            c.a = 0.2f;
            m_Countdown.text = m_Ability.TimeLeft.ToString("F0");
        }
        else
        {
           c.a = 1.0f;
            m_Countdown.text = "";
        }

        m_Image.color = c;
    }
}
