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

public class Timer : MonoBehaviour
{
    public static Timer Instance;
    
    public AlarmClock alarmClock;
    
    public static float gameHourInS = 12f;
    public static readonly DateTime startTime = new DateTime(2021, 4, 24, 23, 0, 0);
    public static readonly DateTime wakeUpTime = startTime + TimeSpan.FromHours(8);
    public DateTime gameTime;
    
    
    public Text timeText;
    public bool oversleeping;
    public Text oversleepingText;

    public static readonly TimeSpan TEN_MIN = new TimeSpan(0, 10, 0);

    private float timeUpdateCountdown;

    private void Awake()
    {
        Instance = this;
    }

    // Start is called before the first frame update
    void Start()
    {
        reset();
    }

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

        if (timeUpdateCountdown <= 0)
        {
            gameTime += TEN_MIN;
            timeUpdateCountdown = gameHourInS / 6f;
            alarmClock.updateTime();
            SleepDepth.addScore();
        }
        
        timeText.text = gameTime.ToShortTimeString();


        oversleeping = false;
        if (gameTime >= wakeUpTime)
        {
            if (SleepDepth.isAsleep())
            {
                oversleeping = true;
                oversleepingText.enabled = true;
            }
            else
            {
                NightsManager.endNight();
            }
        } 
    }

    public void reset()
    {
        gameTime = startTime;
        timeUpdateCountdown = gameHourInS / 6f;
        oversleepingText.enabled = false;
    }
}
