namespace GmRelay.Shared.Domain;

public static class MoscowTime
{
    private static readonly TimeSpan MoscowOffset = TimeSpan.FromHours(3);

    public static DateTimeOffset Now => DateTimeOffset.UtcNow.ToOffset(MoscowOffset);

    public static DateTimeOffset ToMoscow(this DateTimeOffset utc) => utc.ToOffset(MoscowOffset);

    public static string FormatMoscow(this DateTimeOffset utc)
        => utc.ToOffset(MoscowOffset).ToString("d MMMM yyyy, HH:mm", System.Globalization.CultureInfo.GetCultureInfo("ru-RU"));

    public static DateTime ToMoscow(this DateTime utcDt) => DateTime.SpecifyKind(utcDt.Add(MoscowOffset), DateTimeKind.Unspecified);

    public static string FormatMoscow(this DateTime utcDt)
        => utcDt.Add(MoscowOffset).ToString("d MMMM yyyy, HH:mm", System.Globalization.CultureInfo.GetCultureInfo("ru-RU"));

    public static string FormatMoscowShort(this DateTime utcDt)
        => utcDt.Add(MoscowOffset).ToString("dd.MM");

    public static bool TryParseMoscow(string text, out DateTimeOffset utcTime)
    {
        if (DateTime.TryParseExact(text, new[] { "dd.MM.yyyy HH:mm", "dd.MM.yyyy H:mm", "d.MM.yyyy HH:mm" }, 
            System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out var localDt))
        {
            utcTime = new DateTimeOffset(localDt, MoscowOffset).ToUniversalTime();
            return true;
        }

        utcTime = default;
        return false;
    }
}
