# Calendaring is a simple CMF/Plone calendaring implementation.
# Copyright (C) 2004 Enfold Systems
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
"""
$Id: _cal.py,v 1.2 2004/09/16 13:46:09 dreamcatcher Exp $
"""
import calendar
from dateutil.relativedelta import relativedelta
from datetime import timedelta, datetime, date

WEEK = relativedelta(days=7)
WEEKDAY = relativedelta(days=5)
DAY = relativedelta(days=1)
HOUR = relativedelta(hours=1)
MONTH = relativedelta(months=1)
YEAR = relativedelta(years=1)
SECOND = relativedelta(seconds=1)

def yearRange(dt):
    start = dt.replace(month=1, day=1, hour=0, minute=0, second=0)
    end = start + YEAR
    delta = YEAR
    return start, end, delta

def monthRange(dt):
    start = dt.replace(day=1, hour=0, minute=0, second=0)
    delta = MONTH
    end = start + delta
    return start, end, delta

def threeMonthRange(dt):
    previous = dt - MONTH
    start = previous.replace(day=1, hour=0, minute=0, second=0)
    next = dt + MONTH
    next = next.replace(day=1, hour=0, minute=0, second=0)
    end = next + timedelta(calendar.monthrange(next.year, next.month)[1])
    delta = relativedelta(end, start)
    return start, end, delta

def weekRange(dt):
    start = dt - timedelta(dt.weekday())
    end = dt + timedelta(7 - dt.weekday())
    start = start.replace(hour=0, minute=0, second=0)
    end = end.replace(hour=0, minute=0, second=0)
    delta = relativedelta(end, start)
    return start, end, delta

def workdaysRange(dt):
    start = dt - timedelta(dt.weekday())
    end = dt + timedelta(5 - dt.weekday())
    start = start.replace(hour=0, minute=0, second=0)
    end = end.replace(hour=0, minute=0, second=0)
    delta = relativedelta(end, start)
    return start, end, delta

def threeWeekRange(dt):
    start = (dt - timedelta(dt.weekday())) - WEEK
    end = (dt + timedelta(7 - dt.weekday())) + WEEK
    start = start.replace(hour=0, minute=0, second=0)
    end = end.replace(hour=0, minute=0, second=0)
    delta = relativedelta(end, start)
    return start, end, delta

def dayRange(dt):
    start = dt
    end = dt + DAY
    start = start.replace(hour=0, minute=0, second=0)
    end = end.replace(hour=0, minute=0, second=0)
    return start, end, DAY

def hourRange(dt):
    start = dt.replace(minute=0, second=0)
    end = dt.replace(minute=0, second=0) + HOUR
    return start, end, HOUR

RANGES = {'day':dayRange,
          'week':weekRange,
          '3week':threeWeekRange,
          'month':monthRange,
          '3month':threeMonthRange,
          'year':yearRange,
          'workdays':workdaysRange,
          'hour':hourRange}

def makeRange(dt, res):
    func = RANGES[res]
    return func(dt)
