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

public class MainTimer : MonoBehaviour
{
    public Text timer;

    public float countdown;

    public GameObject sanity;
    public Image sanityImage;

    public PlayerInteract pInteract;

    private void Awake()
    {
        sanity = this.gameObject.transform.GetChild(0).GetChild(0).GetChild(0).gameObject;
    }

    private void Update()
    {
        if(countdown >= 0 && !pInteract.isGameOverW && !pInteract.isGameOverL)
        {
            countdown -= .0002f;
            SetSize(countdown);
            if (countdown <= .3f)
            {
                sanityImage.GetComponent<Image>().color = Color.red;
            }
        }
        if(countdown <= 0)
        {
            pInteract.isGameOverL = true;
            pInteract.onGameOver(false);
            SceneManager.LoadScene(2);
        }
    }

    public void SetSize(float sizeNormalized)
    {
        sanity.transform.localScale = new Vector3(sizeNormalized, 1f);
    }
}
