import requests
import json
import time
from datetime import date

key = '63f38f069567d2468f1be592301eb096acb2682b'

OWNER = 'glass-bead-labs'
REPO = 'sensor-group'

# Parameters for the GET request of issues. 
parameters = {'state': 'all', 'per_page': 100}

# GET request to get all issues from a repo.
request_issues = requests.get('https://api.github.com/repos/' + OWNER +
                     '/' + REPO + '/issues', auth=(key, 'x-oauth-basic'), params=parameters).json()

print(requests.get('https://api.github.com/repos/' + OWNER +
                     '/' + REPO + '/issues', auth=(key, 'x-oauth-basic'), params=parameters).headers)

def get_issue_with_index(i):
    # Returns the number assigned to the issue when made. 
    return str(request_issues[i]['number'])

def get_issue_with_title(title):
    # Returns the number assigned to the issue when made.
    for i in range(len(request_issues) - 1):
        if title == str(request_issues[i]['title']):
            return str(request_issues[i]['number'])





parameters = {'per_page': 100, 'direction': 'desc', 'sort': 'created'}

# GET request to get all the comments specific to a repo.
request_repo_comments = requests.get('https://api.github.com/repos/'
                    + OWNER + '/' + REPO + '/issues/comments', auth=(key, 'x-oauth-basic'), params=parameters).headers

def get_comments_issue(num):
    # GET request to get the all the comments specific to an issue.
    return requests.get('https://api.github.com/repos/' + OWNER +
        '/' + REPO + '/issues/' + str(num) + '/comments', auth=(key, 'x-oauth-basic'),
        params=parameters).json()


def get_all_comments():
    # Returns a list with all comments in dictionaries (most recent to oldest). 
    parameters = {'state': 'all', 'per_page': 100}
    comments_page = request_repo_comments['link'].split(';')[1].split(',')[1][2:-1]
    comments_page_URL = comments_page[:-1]
    last_page_number = int(comments_page[-1:])
    all_comments = []
    for i in range(1, last_page_number + 1):
        comments_URL = comments_page_URL + str(i)
        all_comments += requests.get(comments_URL, params=parameters).json()
    return all_comments

def get_creator(i):
    # Returns who created the comment.
    return str(get_all_comments()[i]['user']['login'])

def get_message(i):
    return str(get_all_comments()[i]['body'])

def get_comments_creator(creator):
    index = []
    for i in range(len(get_all_comments())):
        if get_creator(i) == creator:
            index.append(i)
    return index



# def get_labels(i):
#     labels = []
#     if request[i]['labels'] != []:
#         for label in request[i]['labels']:
#             labels.append(str(label['name']))
#     return labels

# def get_date_created(i):
#     time = request[i]['created_at']
#     if (time != None):
#         time_stamp = str(time)
#         month = int(time_stamp[5:7])
#         day = int(time_stamp[8:10])
#         return str(month) + '/' + str(day)

# def get_date_closed(i):
#     time = request[i]['closed_at']
#     if (time != None):
#         time_stamp = str(time)
#         month = int(time_stamp[5:7])
#         day = int(time_stamp[8:10])
#         return str(month) + '/' + str(day)

# def get_date_updated(i):
#     time = request[i]['updated_at']
#     if (time != None):
#         time_stamp = str(time)
#         month = int(time_stamp[5:7])
#         day = int(time_stamp[8:10])
#         return str(month) + '/' + str(day)


# def get_num_comments(i):
#     return int(request[i]['comments'])

# # Date of the first issue created. 
# startDate = get_date_created(len(request) - 1)

# # Date of the most recent issue created. 
# endDate = get_date_created(0)

# def dates_of_issues():
#     # Returns a list of all the dates issues were made from oldest to newest date. 
#     dates = [get_date_created(0)]
#     for i in range(1, len(request) - 1):
#         if (get_date_created(i) not in dates):
#             dates.append(get_date_created(i))
#     return dates[::-1]


# def comments_per_day():
#     # Returns a list of the total number of per day an issue was made.
#     num_comments = []
#     comments = 0
#     for k in range(len(dates_of_issues())):
#         for i in range(len(request) - 1):
#             if (get_date_created(i) == dates_of_issues()[k]):
#                 comments += get_num_comments(i)
#         num_comments.append(comments)
#         comments = 0
#     return num_comments


# # Today's date.
# year = date.today().year
# month = str(date.today().month)
# day = str(date.today().day)


# Creates a list of all the data to be stored in a JSON file. 
# data = []
# for i in range(len(request_repo_comments) - 1):
#     data.append({'issue': i, 'creator': get_creator(i)})


# # Creates the JSON file. 
# with open ('github_comments.json', 'w') as outfile:
#     json.dump(data, outfile, indent=4)


 