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


public class GameManager : MonoBehaviour
{
    [SerializeField] SpriteRenderer livingLight_spriteRenderer;
    [SerializeField] SpriteRenderer livingDark_spriteRenderer;
    [SerializeField] private float countdownTime = 10f;
    [SerializeField] private TMP_Text countdownText;
    [SerializeField] private AudioSource audioSource; 
    [SerializeField] private AudioClip lightAudio;       
    [SerializeField] private AudioClip darkAudio;       
    private AudioClip newClip;       

    private float currentTime;                         
    private bool isCountingDown = false;

    private void Start() {
        livingLight_spriteRenderer.enabled = true;
        livingDark_spriteRenderer.enabled = false;
        newClip = lightAudio;
        ChangeAudio();
    }

    private void Update() {
        if (isCountingDown && currentTime > 0) {
            currentTime -= Time.deltaTime;

            if (currentTime <= 0) {
                currentTime = 0;
                isCountingDown = false;
                OnCountdownComplete();
            }

            DisplayTime((int)currentTime);
        }
    }

    public void ChangeTheme() {
        livingLight_spriteRenderer.enabled = !livingLight_spriteRenderer.enabled;
        livingDark_spriteRenderer.enabled = !livingDark_spriteRenderer.enabled;

        if(livingLight_spriteRenderer.enabled) {
            newClip = lightAudio;
        } else { 
            newClip = darkAudio;
        }

        ChangeAudio();
    }

    public void StartCountdown() {
        currentTime = countdownTime;
        isCountingDown = true;
        DisplayTime((int)currentTime);
    }

    private void OnCountdownComplete() {
        countdownText.text = "0";
    }

    private void DisplayTime(int timeToDisplay) {
        countdownText.text = timeToDisplay.ToString();
    }

    public void ChangeAudio() {
        audioSource.clip = newClip;
        audioSource.Play();         
    }

}
