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

public class RecollectionSectionManager : MonoBehaviour
{

    public UIBeginEndZone zones;
    public RectTransform fire;
    public Slider[] progressBars;
    public Text[] displayProgress;
    public Button[] buttons;
    public float timer;
    public int[] globalResourceCounter;
    public int[] resourceObjective;
    public float animationSpeed;
    public float endZoneOffset;
    public int initialCookies;
    public int[] displayValue;
    float t;
    Vector2 fireStart;
    Vector2 endZoneStart;
    bool percent66 = false;
    bool percent33 = false;

    // Start is called before the first frame update
    void Start()
    {
        zones.endZone = new Vector2(fire.anchoredPosition.x-endZoneOffset, fire.anchoredPosition.y);
        fireStart = fire.anchoredPosition;
        endZoneStart = zones.endZone;
        globalResourceCounter = new int[(int)ResourceType.Lenght];
        globalResourceCounter[0] = initialCookies;
        for (int i = 0; i < resourceObjective.Length; ++i) {
            progressBars[i].maxValue = resourceObjective[i];
            displayValue[i] = 0;
            displayProgress[i].text =""+ displayValue[i];
        }
    }

    public void transformResource(int resource) {
		displayValue[resource] -= 1;
        displayProgress[resource].text = "" + displayValue[resource];
        if (displayValue[resource] <= 0) {
            buttons[resource].interactable = false;
        }
        KangarooGameManager.Instance.StartNewMelody((ResourceType) resource);
    }

    public void IncreaseResourceCounter(int value, int i)
    {
        globalResourceCounter[i] += 1;
        StartCoroutine(AnimationProgressBar(i));
    }

    IEnumerator AnimationProgressBar(int i)
    {
        bool finished = false;
        float time = 0;
        while (!finished)
        {
            time += Time.deltaTime;
            progressBars[i].value = Mathf.Lerp(progressBars[i].value, globalResourceCounter[i], time / animationSpeed);
            if (time >= animationSpeed)
            {
                finished = true;
            }
            yield return null;
        }
    }

    // Update is called once per frame
    void Update()
    {
        t += Time.deltaTime/timer;
        fire.anchoredPosition = Vector2.Lerp(fireStart, zones.endFire, t);
        float fireCoverage = fire.anchoredPosition.x * 100 / zones.endFire.x;
        if (!percent66 && fireCoverage <= 66f) {
            percent66 = true;
            MusicManager.Instance.SetMusicType(MusicManager.musicType.medium);
        }
        if (!percent33 && fireCoverage <= 33f)
        {
            percent33 = true;
            MusicManager.Instance.SetMusicType(MusicManager.musicType.hard);
        }
        if(fireCoverage <= 100f)
        {
            KangarooGameManager.Instance.SetLoseGame();
        }
        zones.endZone = Vector2.Lerp(endZoneStart, zones.endFire, t);
        for (int i = 0; i < progressBars.Length; ++i) {
            if (progressBars[i].value == resourceObjective[i])
            {
                buttons[i].interactable = true;
                displayValue[i] += 1;
                displayProgress[i].text = ""+displayValue[i];
                progressBars[i].value = 0;
                globalResourceCounter[i] = 0;
            }
        }
    }
}
