# coding: utf-8
from __future__ import unicode_literals, absolute_import, print_function

from datetime import date, timedelta


def easter_day(shift):
    def inner(year):
        day, month = EASTERS[year]
        easter = date(year, month, day)
        the_date = easter + timedelta(shift)
        return the_date.day, the_date.month
    return inner


def wednesday_before_november_23(year):
    november_23 = date(year, 11, 23)
    shift = november_23.weekday() - 2
    if shift <= 0:
        shift += 7
    wednesday = november_23 - timedelta(shift)
    return wednesday.day, wednesday.month


EASTERS = {
    2014: (20, 4),
    2015: (5, 4),
    2016: (27, 3),
    2017: (16, 4),
    2018: (1, 4),
    2019: (21, 4),
    2020: (12, 4),
}

# Assuming that there are no gaps in EASTERS
MAXIMUM_KNOWN_YEAR = max(EASTERS.iterkeys())

REGIONS = {
    'BW': 'Baden-Württemberg',
    'BY': 'Freistaat Bayern',
    'BY-AU': 'Freistaat Bayern: Augsburg',
    'BY-MU': 'Freistaat Bayern: München',
    'BE': 'Berlin',
    'BB': 'Brandenburg',
    'HB': 'Freie Hansestadt Bremen',
    'HH': 'Hamburg',
    'HE': 'Hessen',
    'MV': 'Mecklenburg-Vorpommern',
    'NI': 'Niedersachsen',
    'NW': 'Nordrhein-Westfalen',
    'RP': 'Rheinland-Pfalz',
    'SL': 'Saarland',
    'SN': 'Sachsen',
    'ST': 'Sachsen-Anhalt',
    'SH': 'Schleswig-Holstein',
    'TH': 'Thüringen',
}

HOLIDAYS = {
    'Neujahrstag': ((1, 1), None),
    'Heilige Drei Könige': ((6, 1), {'BW', 'BY', 'BY-AU', 'BY-MU', 'ST'}),
    'Karfreitag': (easter_day(-2), None),
    'Ostermontag': (easter_day(1), None),
    'Tag der Arbeit': ((1, 5), None),
    'Christi Himmelfahrt': (easter_day(39), None),
    'Pfingstmontag': (easter_day(50), None),
    'Fronleichnam': (easter_day(60), {'BW', 'BY', 'BY-AU', 'BY-MU', 'HE', 'NW', 'RP', 'SL'}),
    'Friedensfest': ((8, 8), {'BY-AU'}),
    'Mariä Himmelfahrt': ((15, 8), {'BY-AU', 'BY-MU', 'SL'}),
    'Tag der Deutschen Einheit': ((3, 10), None),
    'Reformationstag': ((31, 10), {'BB', 'MW', 'SN', 'ST', 'TH'}),
    'Allerheiligen': ((1, 11), {'BW', 'BY', 'BY-AU', 'BY-MU', 'NW', 'RP', 'SL'}),
    'Buß- und Bettag': (wednesday_before_november_23, {'SN'}),
    'Weihnachtstag': ((25, 12), None),
    'Zweiter Weihnachtsfeiertag': ((26, 12), None),
}

GLOBAL_EXCEPTIONS = {
    'Reformationstag': [2017],
}
@@ -122,16 +122,244 @@
 delta%0A%0A%0A
+EASTERS = %7B%0A    2016: (27, 3),%0A    2017: (16, 4),%0A    2018: (1, 4),%0A    2019: (21, 4),%0A    2020: (12, 4),%0A%7D%0A%0AFIRST_ADVENTS = %7B%0A    2016: (27, 11),%0A    2017: (3, 12),%0A    2018: (2, 12),%0A    2019: (1, 12),%0A    2020: (29, 11),%0A%7D%0A%0A%0A
 def east
@@ -581,16 +581,23 @@
 r%0A%0A%0Adef 
+second_
 wednesda
@@ -609,139 +609,201 @@
 ore_
-november_23(year):%0A    november_23 = date(year, 11, 23)%0A    shift = november_23.weekday() - 2%0A    if shift %3C= 0:%0A        shift += 7
+the_first_advent(year):%0A    first_advent_day, first_advent_month = FIRST_ADVENTS%5Byear%5D%0A    first_advent = date(year, first_advent_month, first_advent_day)%0A    # First advent is always on Sunday
 %0A   
@@ -815,27 +815,28 @@
 esday = 
-november_23
+first_advent
  - timed
@@ -836,29 +836,26 @@
 - timedelta(
-shift
+11
 )%0A    return
@@ -892,154 +892,8 @@
 h%0A%0A%0A
-EASTERS = %7B%0A    2014: (20, 4),%0A    2015: (5, 4),%0A    2016: (27, 3),%0A    2017: (16, 4),%0A    2018: (1, 4),%0A    2019: (21, 4),%0A    2020: (12, 4),%0A%7D%0A%0A
 # As
@@ -2275,16 +2275,23 @@
 ttag': (
+second_
 wednesda
@@ -2303,19 +2303,24 @@
 ore_
-november_23
+the_first_advent
 , %7B'

Fix Repentance and Prayer day