namespace WorkTracker.Services.Festivities;

public sealed class ItalianFestivitySource : IItalianFestivitySource
{
    public IReadOnlyCollection<DateOnly> GetFestivities(int year)
    {
        var easterSunday = GetEasterSunday(year);
        var easterMonday = easterSunday.AddDays(1);

        return new HashSet<DateOnly>
        {
            new(year, 1, 1),
            new(year, 1, 6),
            easterSunday,
            easterMonday,
            new(year, 4, 25),
            new(year, 5, 1),
            new(year, 6, 2),
            new(year, 8, 15),
            new(year, 11, 1),
            new(year, 12, 8),
            new(year, 12, 25),
            new(year, 12, 26)
        };
    }

    private static DateOnly GetEasterSunday(int year)
    {
        var a = year % 19;
        var b = year / 100;
        var c = year % 100;
        var d = b / 4;
        var e = b % 4;
        var f = (b + 8) / 25;
        var g = (b - f + 1) / 3;
        var h = (19 * a + b - d - g + 15) % 30;
        var i = c / 4;
        var k = c % 4;
        var l = (32 + 2 * e + 2 * i - h - k) % 7;
        var m = (a + 11 * h + 22 * l) / 451;
        var month = (h + l - 7 * m + 114) / 31;
        var day = ((h + l - 7 * m + 114) % 31) + 1;

        return new DateOnly(year, month, day);
    }
}
