__author__ = 'talluri'
# This script shows how to use the client in anonymous mode
# against jira.atlassian.com.

# from six import print_ as print

from jira.client import GreenHopper
from jira.client import JIRA
from subprocess import Popen, PIPE

empty_file = "/Users/talluri/emptyTasks.txt"
mail_id = {'shwetaag' : 'Shweta.Agarwal@citrix.com',
           't_manasave' : 'manasa.veloori@citrix.com',
           'sanjeevn' : 'sanjeev.neelarapu@citrix.com',
           'sureshsa' : 'Suresh.Sadhu@citrix.com',
           'prashantkm' : 'prashantkumar.mishra@citrix.com',
           'pavanb' : 'Pavan.Bandarupally@citrix.com',
           't_gauravar' : 'gaurav.aradhye@citrix.com',
           'srikantest' : 'srikanteswararao.talluri@citrix.com',
           't_ashutosk' : 'ashutosh.kelkar@citrix.com',
           'rpullela' : 'raja.pullela@citrix.com'
           }

def get_email_addr(jiraid):
    issue = jira.issue(id=jiraid)
    return issue.fields.assignee.emailAddress
def print_report(fh, issue):
    fh.write("#####################################\n")
    fh.write("task: %s \n" % issue.key)
    fh.write("status: %s\n" % issue.status.name)
    fh.write("username: %s\n" % issue.assignee)
    fh.write("Name: %s\n" % issue.assigneeName)
    fh.write("Email: %s\n" % mail_id[issue.assignee])
    fh.write("summary: %s\n" % issue.summary)
    if hasattr(issue, 'issue.estimateStatistic.statFieldValue.value'):
        fh.write("timeEstimated: %s\n" % issue.estimateStatistic.statFieldValue.value)
    if hasattr(issue, 'issue.trackingStatistic.statFieldValue.text'):
        fh.write("remainingtime: %s\n", issue.trackingStatistic.statFieldValue.text)
    fh.write("\n#####################################\n")
    #print issue.assignee, ":", get_email_addr(issue.key), ","


# By default, the client will connect to a JIRA instance started from the Atlassian Plugin SDK
# (see https://developer.atlassian.com/display/DOCS/Installing+the+Atlassian+Plugin+SDK for details).
# Override this with the options parameter.
# GreenHopper is a plugin in a JIRA instance
options = {
    'server': 'https://issues.citrite.net'
}

jira = JIRA(options)
gh = GreenHopper(options)

# Get all boards viewable by anonymous users.
# boards = gh.boards()
board_id = ""
board_name = ""
# for board in boards:
#     if 'scrum-hyd' in board.name.lower():
#         board_id = board.id
#         board_name = board.name
#         break
# Get the sprints in a specific board
board_id = 440
board_name = 'CCP QA Scrum'
sprint_id = 1578
print("GreenHopper board: %s (%s)" % (board_name, board_id))
#sprints = gh.sprints(board_id)
sprint = gh.sprint_info(board_id, sprint_id)

# List the incomplete issues in each sprint
#for sprint in sprints:
sprint_id = sprint['id']
print("Sprint: %s" % sprint['name'])
incompleted_issues = gh.incompleted_issues(board_id, sprint_id)
#print("Incomplete issues: %s" % ', '.join(issue.key for issue in incompleted_issues))
for person in mail_id.keys():
    count = 0
    person_details = person + "_mail.txt"
    for issue in incompleted_issues:
        if issue.assignee == person:
            count = count + 1
            with open(person_details, 'a') as fh:
                print_report(fh, issue)
        # fh.write("\n\nThis is an autogenerated email")
        # fh.close()
    if count != 0:
        pass
        # output = Popen(['mail -s "Goleta Sprint- incomplete tasks[%s task(s)]" %s < %s' % (count, mail_id[person],
        #                                                                                  person_details)],
        #                stdout=PIPE,
        #                stderr=PIPE,
        #                shell=True)
        # (out, err) = output.communicate()
    else:
        output = Popen(['mail -s "Goleta Sprint- incomplete tasks[%s task(s)]" %s < %s' % (count, mail_id[person],
                                                                                         empty_file)],
                       stdout=PIPE,
                       stderr=PIPE,
                       shell=True)
        (out, err) = output.communicate()







