package javacodebook.core.holiday;

import java.util.Calendar;
import java.util.GregorianCalendar;

/**
 * Berechnung beweglicher Feiertage für die Jahre
 * 1901 bis 2078.
 */
public class Holiday {
    /**
     * Berechnet Osterdatum für ein bestimmtes Jahr
     */
    public static Calendar calculateEaster(int year) {
        return calculate(year, 0);
    }
    
    /**
     * Berechnung Christi Himmelfahrt in einem bestimmten 
     * Jahr
     */
    public static Calendar calculateAscensionDay(int year)
    {
        return calculate(year, 39);
    }
    
    /**
     * Berechnung Pfingstsonntag in einem bestimmten Jahr
     */
    public static Calendar calculatePentecost(int year)
    {
        return calculate(year, 49);
    }
    
    /**
     * Berechnung Rosenmontag in einem bestimmten Jahr
     */
    public static Calendar calculateCarnivalMonday(int year)
    {
        return calculate(year, -48);
    }
    
    /**
     * Berechnung Fronleichname in einem bestimmten Jahr
     */
    public static Calendar calculateCorpusChristi(int year)
    {
        return calculate(year, 60);
    }
    
    /**
     * Die eigentliche Berechung. Es kann ein Offset
     * übergeben werden. Dieser wird in das berechnete
     * Datum eingefügt.
     */
    private static Calendar calculate(int year, int off)
    {
        int a = year % 19;
        int b = year % 4;
        int c = year % 7;
        int m = (year/100);
        int d = (19 * a + 24) % 30;
        int e = (2 * b + 4 * c + 6 * d + 5) % 7;
        int day = 22 + d + e;
        int month = 2;
        if (day>31) {
            day = d + e - 9;
            month = 3;
        }
        if (day==26 && month==3){
            day = 19;
        }
        if (day==25 && month==3 && d==28 && e==6 && a>10 ) {
            day = 18;
        }
        
        return new GregorianCalendar(year, month, day+off);
    }
}