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

public class GameManager : MonoBehaviour
{
    //Playable Game Time in Secs
    public int playTime = 30;
    
    public GameObject time;

    private Slider slider;
    
    private Color fullTime =  new Color(0f,1f,0f,1f);
    private Color highMidTime =  new Color(1f,1f,0f,1f);
    private Color midTime =  new Color(1f,.5f,0f,1f);
    private Color lowTime =  new Color(1f,0f,0f,1f);
    
    private float currentTime;
    private int currentScore;

    private double fullTimeValue;
    private double highMidTimeValue;
    private double midTimeValue;
    private double lowTimeValue;
    
    private int EndGameIndex = 3;
    // Start is called before the first frame update
    void Start()
    {
        currentTime = playTime;
        currentScore = 0;
        
        highMidTimeValue = (playTime * .75);
        midTimeValue = (playTime * .50);
        lowTimeValue = (playTime * .25);
        
        slider = time.GetComponentInChildren<Slider>();
        slider.maxValue = currentTime;
        
        UpdateTimeSlider();
    }

    // Update is called once per frame
    void Update()
    {
        currentTime -= Time.deltaTime;
        
        UpdateTimeSlider();

        if (currentTime < 0)
        {
            EndGame();
        }
    }

    void EndGame()
    {
        //call end game stuff
        SceneManager.LoadScene(EndGameIndex);

    }

    void UpdateTimeSlider()
    {
        slider.value = currentTime;
        
        if (slider.value >= highMidTimeValue )
        {
            slider.GetComponentInChildren<Image>().color = fullTime;
        }
        else if (slider.value < highMidTimeValue && slider.value >= midTimeValue)
        {
            slider.GetComponentInChildren<Image>().color = highMidTime;
        }
        else if (slider.value < midTimeValue && slider.value >= lowTimeValue)
        {
            slider.GetComponentInChildren<Image>().color = midTime;
        }
        else if (slider.value < lowTimeValue)
        {
            slider.GetComponentInChildren<Image>().color = lowTime;
        }
    }
}
