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

public class ComplexBar : MonoBehaviour {

    public List<Color> colors = new List<Color>();
    public List<string> labels = new List<string>();
    public List<int> values = new List<int>();
    private List<RectTransform> images = new List<RectTransform>();
    private RectTransform rectTransform;
    public CanvasScaler scaler;

    public Text descriptionLabel;
    public GameObject descriptionPanel;

    void Start()
    {
        descriptionPanel.gameObject.SetActive(false);
        rectTransform = GetComponent<RectTransform>();

        for (int i = 0; i < colors.Count; i++)
        {
            GameObject go = new GameObject("bar" + i.ToString());
            go.transform.SetParent(transform);
            go.transform.localScale = Vector3.one;
            images.Add(go.AddComponent<Image>().rectTransform);

            ComplexBarSubBar subBar = go.AddComponent<ComplexBarSubBar>();
            subBar.parentBar = this;
            subBar.index = i;

            images [i].GetComponent<Image>().color = colors [i];
            images [i].anchorMin = new Vector2(0, 0);
            images [i].anchorMax = new Vector2(0, 1f);
            images [i].pivot = new Vector2(0, 0.5f);
            images [i].SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, rectTransform.rect.height);
        }
    }

    public void UpdateValues(List<int> values, int total)
    {
        this.values = values;
        for (int i = 0; i < images.Count; i++)
        {
            if (i == 0)
                images [i].anchoredPosition = new Vector2(0, 0);
            else
                images [i].anchoredPosition = new Vector2(images [i - 1].anchoredPosition.x + images [i - 1].rect.width, 0);

            if (i < images.Count - 1)
                images [i].SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, rectTransform.rect.width * ((float)values [i] / (float)total));
            else
                images [i].SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, rectTransform.rect.width - (images [i - 1].anchoredPosition.x + images [i - 1].rect.width));
                
            //images [i].SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, rectTransform.rect.height);
        }
    }
	
}
