#!/usr/bin/env python2.7

# 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 json
import email.utils
from pprint import pprint
import getpass
from smtplib import SMTP_SSL as SMTP
import datetime
from datetime import timedelta 
from dateutil.parser import parse
from email.mime.text import MIMEText

def findWednesday(d, weeks):
    days_ahead = 2 - d.weekday()
    if days_ahead < 0:
        days_ahead += weeks * 7
    else:
        days_ahead += (weeks - 1) * 7 #we're on or after wednesday, give less time
    return d + datetime.timedelta(days_ahead)

fp = open('email_monthly_template.txt', 'rb')
messageBody = fp.read()
fp.close()

with open('content/shepherd_assignments.json') as data_file:    
    data = json.load(data_file)

month = raw_input('Enter month, in the format of YYYY-MM: ')
username = raw_input('Enter your ASF Username: ')
password = getpass.getpass('Enter your ASF pw: ')

podlingsToReport = data[month].keys()

meFormat = '{}@apache.org'
emailTo = 'general@incubator.apache.org'

me = meFormat.format(username)

thisMonth = parse(month + "-01")

podlingReportsDue = findWednesday(thisMonth, 1)

boardMeeting = findWednesday(thisMonth, 3)

subjectDate = thisMonth.strftime("%B %Y")
wikiPage = thisMonth.strftime("%B%Y")
dueDateFormat = podlingReportsDue.strftime("%a, %B %d")
boardMeetingFormat = boardMeeting.strftime("%a, %d %B %Y")

print dueDateFormat
print boardMeetingFormat

dates = (
    podlingReportsDue.strftime("%a %B %d") + " - Podling reports due by end of day\n" +
    (podlingReportsDue + timedelta(days=4)).strftime("%a %B %d") + " - Shepherd reviews due by end of day\n" +
    (podlingReportsDue + timedelta(days=4)).strftime("%a %B %d") + " - Summary due by end of day\n" +
    (podlingReportsDue + timedelta(days=6)).strftime("%a %B %d") + " - Mentor signoff due by end of day\n" +
    (podlingReportsDue + timedelta(days=7)).strftime("%a %B %d") + " - Report submitted to Board\n" +
    (podlingReportsDue + timedelta(days=14)).strftime("%a %B %d") + " - Board meeting\n" 
)

toReport = ""
for podling in podlingsToReport:
    toReport = toReport + " - " + podling + "\n"

emailBodyString = messageBody.format(dates, toReport, wikiPage)

s = SMTP('mail-relay.apache.org')
s.login(username, password)
print "connected"
mailTo = "general@incubator.apache.org"
msgId = email.utils.make_msgid()
msg = MIMEText(emailBodyString)
msg['Subject'] = ' [MENTORS] ' + subjectDate.split(' ')[0] + ' report timeline - reports due ' + subjectDate
msg['From'] = me
msg['Date'] = email.utils.formatdate()
msg['To'] = emailTo
msg['Message-ID'] = msgId
s.sendmail(me, [emailTo], msg.as_string())
print(' sent ',msgId,' to ',mailTo,' via ',me)
s.quit()

