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

public class UI : MonoBehaviour
{
    // Start is called before the first frame update

    [SerializeField] private GameObject panel;
    [SerializeField] private GameObject selector;

    [SerializeField] private GameObject mana_bar;
    [SerializeField] private GameObject ability_cooldown;

    private List<GameObject> options = new List<GameObject>();
    private int n_plants = 0;
    private List<float> costs = new List<float>();
    private float ability_cost = 0f;
    private Vector3 bar_maxSize;
    private Vector3 cooldown_maxSize;

    private Image bar;

    void Awake()
    {
        bar = mana_bar.GetComponent<Image>();
        bar_maxSize = bar.transform.localScale;

        cooldown_maxSize = ability_cooldown.transform.localScale;

        n_plants = panel.transform.childCount;
        for (int i = 0; i < n_plants; i++)
        {
            options.Add(panel.transform.GetChild(i).gameObject);
        }
    }

    public void Init(CharacterController2D.Plant[] plants, float ability_cost)
    {
        for (int i = 0; i < plants.Length; i++)
        {
            costs.Add(plants[i].cost);
        }
        this.ability_cost = ability_cost;
    }

    public void SelectPlant(int i)
    {
        if (i < n_plants)
        {
            selector.transform.position = panel.transform.GetChild(n_plants - i - 1).transform.position;
        }
    }

    public void SetMana(float cur, float max)
    {
        float proportion = cur / max;
        bar.transform.localScale = new Vector3(proportion * bar_maxSize.x, bar_maxSize.y, bar_maxSize.z);
        for (int i = 0; i < n_plants; i++)
        {
            panel.transform.GetChild(n_plants - i - 1).GetChild(0).GetComponent<Image>().color = ((costs[i % costs.Count /*remove mod*/] > cur) ? Color.red : Color.white);
        }
        ability_cooldown.GetComponent<Image>().color = ((ability_cost > cur) ? Color.red : Color.white);
    }

    public void SetCooldown(float cur, float max)
    {
        float proportion = cur / max;
        ability_cooldown.transform.localScale = new Vector3(cooldown_maxSize.x, (1 - proportion) * cooldown_maxSize.y, cooldown_maxSize.z);
    }
}
