#!/usr/bin/python
# -*- coding: utf-8 -*-

#
# Copyright(c) 2011 Kristof Imre Szabo
# http://krisek.tumblr.com
# 
# This file may be licensed under the terms of of the
# GNU General Public License Version 3 (the ``GPL'').
# 
# Software distributed under the License is distributed
# on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
# express or implied. See the GPL for the specific language
# governing rights and limitations.
#

import datetime
import time
import string


class LiturgicalYear(object):
    easter_set = { '1990':'04-15', '1991':'03-31', '1992':'04-19', '1993':'04-11',
    '1994':'04-03', '1995':'04-16', '1996':'04-07', '1997':'03-30', '1998':'04-12',
    '1999':'04-04', '2000':'04-23', '2001':'04-15', '2002':'03-31', '2003':'04-20',
    '2004':'04-11', '2005':'03-27', '2006':'04-16', '2007':'04-08', '2008':'03-23',
    '2009':'04-12', '2010':'04-04', '2011':'04-24', '2012':'04-08', '2013':'03-31',
    '2014':'04-20', '2015':'04-05', '2016':'03-27', '2017':'04-16', '2018':'04-01',
    '2019':'04-21', '2020':'04-12', '2021':'04-04', '2022':'04-17', '2023':'04-09',
    '2024':'03-31', '2025':'04-20', '2026':'04-05', '2027':'03-28', '2028':'04-16',
    '2029':'04-01', '2030':'04-21', '2031':'04-13', '2032':'03-28', '2033':'04-17',
    '2034':'04-09', '2035':'03-25', '2036':'04-13', '2037':'04-05', '2038':'04-25',
    '2039':'04-10', '2040':'04-01', '2041':'04-21', '2042':'04-06', '2043':'03-29',
    '2044':'04-17', '2045':'04-09', '2046':'03-25', '2047':'04-14', '2048':'04-05',
    '2049':'04-18' } 
    max_year = max(easter_set.iterkeys()) 
    min_year = min(easter_set.iterkeys())

    def __init__(self, today = datetime.datetime.now()):
        #lithurgical colours

        day = 24*60*60;

        year = int(today.strftime("%Y"))

        holiday = ""
        holiday_color = "Green"
                
        if year > int(self.max_year):
            self.holiday = holiday
            self.holiday_color = holiday_color
            return
        
        easter_day = string.split(self.easter_set[str(year)],'-')
        easter_sec = time.mktime((year, int(easter_day[0]), int(easter_day[1]), 1, 0, 0, 0, 0, 0));

        xmas = datetime.datetime(year, 12, 25, 1, 0, 0, 0)
        xmas_sec = float(xmas.strftime("%s"))
        xmas_day = int(xmas.strftime("%w"))
        if xmas_day == 0:
            xmas_day = 7

        allsaints = datetime.datetime(year, 11, 1, 1, 0, 0, 0)
        allsaints_sec = float(allsaints.strftime("%s"))
        allsaints_day = int(allsaints.strftime("%w"))
        if allsaints_day == 0:
            allsaints_day = 7

        self.moving_dates = {
         'Ash Wednesday' :  easter_sec-(46*day),
         'Lent' :  easter_sec-(45*day),
         'Maundy Thursday' : easter_sec-(3*day),
         'Good Friday'  : easter_sec-(2*day),
         'Holy Saturday' : easter_sec-(1*day),
         'Easter' : easter_sec,
         'Ascension Day' : easter_sec+(39*day),
         'Pentecost Sunday' : easter_sec+(49*day),
         'Trinity Sunday' : easter_sec+(56*day),
         'Advent' : xmas_sec - ((xmas_day + 21) * day) 
        }

        self.holidays = (
            {'name':"Christmas NY" , "color" : "White","start" : time.mktime((year,1,1,2,0,0,0,0,0))},
            {'name':"Epiphany", "color" : "White", "start" : time.mktime((year,1,6,2,0,0,0,0,0))},
            {'name':"After Epiphany", "color" : "Green", "start" : time.mktime((year,1,7,2,0,0,0,0,0))},
            {'name':"Ash Wednesday", "color" : "Purple", "start" : self.moving_dates['Ash Wednesday']},
            {'name':"Lent", "color" : "Purple", "start" : self.moving_dates['Lent']},
            {'name':"Maundy Thursday", "color" : "Purple", "start" : self.moving_dates['Maundy Thursday']},
            {'name':"Good Friday", "color" : "Black", "start" : self.moving_dates['Good Friday']},
            {'name':"Holy Saturday","color" : "Black", "start" : self.moving_dates['Holy Saturday']},
            {'name':"Easter","color" : "White", "start" : self.moving_dates['Easter']},
            {'name':"Eastertide","color" : "White", "start" : self.moving_dates['Easter'] + day},
            {'name':"Ascension Day","color":"White", "start" : self.moving_dates['Ascension Day']},
            {'name':"Pentecost Sunday","color" : "Red", "start" : self.moving_dates['Pentecost Sunday']},
            {'name':"Trinity Sunday","color" : "White", "start" : self.moving_dates['Trinity Sunday']},
            {'name':"Ordinary Time","color" : "Green", "start" : self.moving_dates['Trinity Sunday'] + day},
            {'name':"Reformation Day","color" : "Red", "start" : time.mktime((year,10,31,2,0,0,0,0,0))},
            {'name':"All Saints Day or Sunday","color" : "White", "start" : time.mktime((year,11,1,2,0,0,0,0,0))},
            {'name':"Ordinary Time;","color" : "Green", "start" : allsaints_sec + ((8-allsaints_day) * day)}, #name should be unique
            {'name':"Advent","color" : "Purple", "start" : self.moving_dates['Advent']},
            {'name':"Christmas Eve" ,"color" : "Purple","start" : time.mktime((year,12,24,2,0,0,0,0,0))},
            {'name':"Christmas","color" : "White","start" : time.mktime((year,12,15,2,0,0,0,0,0))},
            {'name':"EOY","color" : "White","start" : time.mktime((year+1,1,1,2,0,0,0,0,0))} #dummy entry, just tho hit Xmas
        )


        now = float(today.strftime("%s"))
        #now = easter_sec + 3660

        prev_holiday = {"start":0}

        #linear search for the current holiday
        for i, item in enumerate(self.holidays):
            if(prev_holiday['start']):
                if(now >= prev_holiday['start'] and now <= item['start']):
                    holiday = prev_holiday['name']
                    holiday_color =  prev_holiday['color']
                    break

            prev_holiday = item

        self.holiday = holiday
        self.holiday_color = holiday_color
        
        return
    
    def printargs(self):
        """Method docstring."""
        print "holiday: " + self.holiday
        print "holiday color: " + self.holiday_color


if __name__ == "__main__":
    ly = LiturgicalYear()
    print ly.holiday

