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

public class AC : MonoBehaviour {

    public static AC instance;

    public GameObject fadeToWhite;
    public AudioSource music;

    public int joules;
    public float secondsPerJoule;
    public float secondsToAnswer;
    public float timeWithoutPower;

    private void Awake()
    {
        instance = this;
    }

    private void Start()
    {
        StartCoroutine(reduceJoules());
    }

    private void Update()
    {
        if (secondsUntilAnswer <= 0)
        {
            Time.timeScale = 0;
            fadeToWhite.SetActive(true);
        }
    }

    public float secondsRemaining
    {
        get
        {
            return secondsPerJoule * joules;
        }
    }

    public float secondsUntilAnswer
    {
        get
        {
            return secondsToAnswer;
        }
    }

    IEnumerator reduceJoules()
    {
        while (true)
        {
            yield return new WaitForSeconds(secondsPerJoule);
            if (joules > 0)
            {
                secondsToAnswer -= secondsPerJoule;
                joules--;
                timeWithoutPower = 0;
                if (!music.isPlaying)
                    music.UnPause();
            }
            if (joules <= 0)
            {
                OutputController.instance.Log("Cosmic ∀C entropy at mxImóm. Deposit enRrgy.", OutputController.MessageType.Error);
                if (music.isPlaying)
                    music.Pause();
                timeWithoutPower += secondsPerJoule;
            }
        }
    }
}
