#!/usr/bin/env python3

# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import sys
if sys.version_info < (3, 2): 
        raise Exception("Python 3.2 or above is required")

import datetime
import argparse
import calendar

from ics import Calendar, Event

class Timeline(object):
    """Timeline of events associated with an Incubator Board report."""

    def __init__(self, month, year):
        self._month = month
        self._year = year
        now = datetime.datetime.now()
        if month < now.month:
            now.year + 1
        for day in range(1, 8):
            first_wed = datetime.date(day=day, month=self._month,
                                      year=self._year)
            if first_wed.weekday() == 2:
                break


        wiki = "https://cwiki.apache.org/confluence/display/INCUBATOR/" + calendar.month_name[month] + str(year)
        board = "https://whimsy.apache.org/board/agenda/"

        self._events = []
        self._date = first_wed.replace(day=first_wed.day + 14)
        self._add_event(desc="ASF Podling reports due by end of day",
                        date=first_wed,
                        url=wiki)
        self._add_event(desc="ASF Shepherd reviews due by end of day",
                        date=first_wed.replace(day=first_wed.day + 4),
                        url=wiki)
        self._add_event(desc="ASF Summary due by end of day",
                        date=first_wed.replace(day=first_wed.day + 4),
                        url=wiki)
        self._add_event(desc="ASF Mentor signoff due by end of day",
                        date=first_wed.replace(day=first_wed.day + 6),
                        url=wiki)
        self._add_event(desc="ASF Report submitted to Board",
                        date=first_wed.replace(day=first_wed.day + 7),
                        url=board)
        self._add_event(desc="ASF Board meeting",
                        date=first_wed.replace(day=first_wed.day + 14),
                        url=board)

    def _add_event(self, desc, date, url):
        self._events.append({"desc": desc, "date": date, "url": url})


    def to_ics(self):
        c = Calendar()
        c.events
        for event in self._events:
            e = Event()
            e.name = event['desc']
            e.begin = event['date'].strftime("%Y-%m-%d 00:00:00")
            e.url = event['url']
            c.events.add(e)
        return  c

def process_cli_args():
    parser = argparse.ArgumentParser()
    parser.add_argument('--month', type=int, default=0,
                        help="month number (1-12)")
    parser.add_argument('--year', type=int, default=0,
                        help="4 digit year number")
    options = parser.parse_args()
    if options.month == 0:
        now = datetime.datetime.now()
        options.month = (now.month % 12) + 1
    if options.year == 0:
        options.year = now.year
    return options

def main():
    options = process_cli_args()
    timeline = Timeline(month=options.month, year=options.year)
    print(timeline.to_ics())

if __name__ == '__main__':
    main()

