using System;
using System.Collections;
using UnityEngine;

namespace Cute;

public class TimeData
{
	private long serverTime;

	private long connectClientTime;

	public void Set(long setServerTime)
	{
		serverTime = setServerTime;
		connectClientTime = (long)TimeNativePlugin.GetDeviceOperatingTime();
	}

	public DateTime GetNowTime()
	{
		return TimeUtil.GetNowTime(serverTime, connectClientTime);
	}

	public DateTime GetNowTime_UTC()
	{
		return TimeUtil.GetNowTime_UTC(serverTime, connectClientTime);
	}

	public float GetTimeLeftLong(long endTime)
	{
		return (float)TimeUtil.GetTimeLeft(serverTime, endTime, 0L).millisecond / 1000f;
	}

	[Obsolete("動作未検証", false)]
	public IEnumerator StartTimeLeft(MonoBehaviour obj, long endTime, Action<TimeUtil.TimeLeftParam> callback)
	{
		IEnumerator enumerator = timeLeftCoroutine(callback, endTime, 0L);
		obj.StartCoroutine(enumerator);
		return enumerator;
	}

	[Obsolete("動作未検証", false)]
	public IEnumerator StartTimeLeft(MonoBehaviour obj, long endTime, long consumingTime, Action<TimeUtil.TimeLeftParam> callback)
	{
		IEnumerator enumerator = timeLeftCoroutine(callback, endTime, consumingTime);
		obj.StartCoroutine(enumerator);
		return enumerator;
	}

	[Obsolete("動作未検証", false)]
	public string GetNowTimeString()
	{
		DateTime nowTime = GetNowTime();
		return $"{nowTime.Year:D4}-{nowTime.Month:D2}-{nowTime.Day:D2} {nowTime.Hour:D2}:{nowTime.Minute:D2}:{nowTime.Second:D2}";
	}

	[Obsolete("動作未検証", false)]
	private TimeUtil.TimeLeftParam GetTimeLeft(long endTime, long consumingTime = 0L)
	{
		return TimeUtil.GetTimeLeft(TimeUtil.ToUnixTime(GetNowTime()), endTime, consumingTime);
	}

	[Obsolete("動作未検証", false)]
	private IEnumerator timeLeftCoroutine(Action<TimeUtil.TimeLeftParam> callback, long endTime, long consumingTime = 0L)
	{
		while (true)
		{
			TimeUtil.TimeLeftParam timeLeft = GetTimeLeft(endTime, consumingTime);
			callback(timeLeft);
			if (timeLeft.isEnd)
			{
				break;
			}
			yield return null;
		}
	}
}
