using TMPro;
using UnityEngine;

public class TimeManager : MonoBehaviour
{
    private const int HOURS_PER_DAY = 24;
    private const int MINUTES_PER_HOUR = 60;
    public const int SECONDS_PER_MINUTE = 2;

    [SerializeField]
    private int currentHour = 12;
    private int currentMinute = 0;
    private float currentSecond = 0;

    // Update is called once per frame
    void Update()
    {
        StepTime();
    }

    private void StepTime()
    {
        currentSecond += Time.deltaTime;

        if (currentSecond > SECONDS_PER_MINUTE)
        {
            currentSecond = 0;
            currentMinute += 1;
        }

        if (currentMinute > MINUTES_PER_HOUR - 1)
        {
            currentMinute = 0;
            currentHour += 1;
        }

        if (currentHour > (HOURS_PER_DAY - 1))
        {
            currentHour = 0;
        }
    }

    /// <summary>
    /// Get the current second within the current minute
    /// </summary>
    /// <returns>Current second within the current minute</returns>
    public float GetCurrentSecond()
    {
        return currentSecond;
    }

    /// <summary>
    /// Get the current minute within the current hour
    /// </summary>
    /// <returns>Current minute within the current hour</returns>
    public int GetCurrentMinute()
    {
        return currentMinute;
    }

    /// <summary>
    /// Get the current hour within the current day
    /// </summary>
    /// <returns>Current hour within the current day</returns>
    public int GetCurrentHour()
    {
        return currentHour;
    }

    /// <summary>
    /// Gets a formatted time string
    /// </summary>
    /// <returns>Returns a formatted string with the hour, minute, and second</returns>
    public string GetTimeString()
    {
        string formattedHour = currentHour.ToString("00");
        string formattedMinute = currentMinute.ToString("00");
        string formattedSecond = currentSecond.ToString("00.00");

        return $"{formattedHour}:{formattedMinute}:{formattedSecond}";
    }

    #region Time Save/Load
    public void SaveTime(ref TimeSaveData data)
    {
        data.hour = currentHour;
        data.minute = currentMinute;
        data.second = currentSecond;
    }

    public void LoadTime(TimeSaveData data)
    {
        currentHour = data.hour;
        currentMinute = data.minute;
        currentSecond = data.second;
    }
    #endregion
}

[System.Serializable]
public struct TimeSaveData
{
    public int hour;
    public int minute;
    public float second;
}
