import os
import csv


def filterPRreactionData(project):
    root = os.getcwd()
    input_path = root + '/PRmeta' + project + '.csv'
    input = open(input_path, 'r', encoding='utf-8', newline='')
    CSVreader = csv.reader(input, delimiter=',')
    next(CSVreader)

    output_path = root + '/PRmeta' + project + '_Filtered.csv'
    output = open(output_path, 'w', encoding='utf-8', newline='')
    CSVwriter = csv.writer(output, delimiter=',')
    header = ['PR_url', 'status', 'createdAt', 'closedAt', 'title', 'author_login', 'mergeCommitID', 'changedFiles',
              'commitMessage', 'Reaction', 'reactTime', 'reactAuthorLogin', 'reactAuthorEmail']
    CSVwriter.writerow(header)

    for row in CSVreader:
        status = row[1]
        if status == 'OPEN':
            row[3] = 'NA'

        createdAt, reaction = row[2], row[9]
        if createdAt > '2016-03-10T00:00:00Z' and reaction != 'NA':
            CSVwriter.writerow(row)


def writeFilteredRow(CSVwriter, row):
    status = row[1]
    if status == 'OPEN':
        row[3] = 'NA'
    createdAt, reaction = row[2], row[7]
    if createdAt > '2016-03-10T00:00:00Z' and reaction != 'NA':
        CSVwriter.writerow(row)


def filterPRCommentsReactionData(project):
    root = os.getcwd()

    output_path = root + '/PRmeta' + project + '_comments_Filtered.csv'
    output = open(output_path, 'w', encoding='utf-8', newline='')
    CSVwriter = csv.writer(output, delimiter=',')

    header = ['PR_url', 'status', 'createdAt', 'closedAt', 'comment', 'commentTime', 'commentAuthor', 'reaction', 'reactTime', 'reactAuthorLogin', 'reactAuthorEmail']
    CSVwriter.writerow(header)

    input_path = root + '/PRmeta' + project + '_comments.csv'
    input = open(input_path, 'r', encoding='utf-8', newline='')
    CSVreader = csv.reader(input, delimiter=',')
    next(CSVreader)

    index = 0

    while True:

        try:
            for row in CSVreader:
                index += 1
                writeFilteredRow(CSVwriter, row)

            break

        except:
            #get around encoding error
            input_alt = open(input_path, 'r', encoding='latin-1', newline='')
            CSVreader_alt = csv.reader(input_alt, delimiter=',')
            row = list(CSVreader_alt)[index]

            writeFilteredRow(CSVwriter, row)

            next(CSVreader)


def writeFilteredRowV2(CSVwriter, row):
    status = row[1]
    if status == 'OPEN':
        row[3] = 'NA'
    createdAt, reaction = row[2], row[8]
    if createdAt > '2016-03-10T00:00:00Z' and reaction != 'NA':
        CSVwriter.writerow(row)


def filterPRCommitsCommentsReactionData(project):
    root = os.getcwd()

    output_path = root + '/PRmeta' + project + '_CommitsComments_Filtered.csv'
    output = open(output_path, 'w', encoding='utf-8', newline='')
    CSVwriter = csv.writer(output, delimiter=',')

    header = ['PR_url', 'status', 'createdAt', 'closedAt', 'commitID', 'comment', 'commentTime', 'commentAuthor', 'reaction', 'reactTime', 'reactAuthorLogin', 'reactAuthorEmail']

    CSVwriter.writerow(header)

    input_path = root + '/PRmeta' + project + '_CommitsComments.csv'
    input = open(input_path, 'r', encoding='utf-8', newline='')
    CSVreader = csv.reader(input, delimiter=',')
    next(CSVreader)

    index = 0

    while True:

        try:
            for row in CSVreader:
                index += 1
                writeFilteredRowV2(CSVwriter, row)

            break

        except:
            #get around encoding error
            input_alt = open(input_path, 'r', encoding='latin-1', newline='')
            CSVreader_alt = csv.reader(input_alt, delimiter=',')
            row = list(CSVreader_alt)[index]

            writeFilteredRowV2(CSVwriter, row)

            next(CSVreader)


def writeFilteredRowV2(CSVwriter, row):
    status = row[1]
    if status == 'OPEN':
        row[3] = 'NA'
    createdAt, reaction = row[2], row[8]
    if createdAt > '2016-03-10T00:00:00Z' and reaction != 'NA':
        CSVwriter.writerow(row)


def filterPRReviewsCommentsReactionData(project):
    root = os.getcwd()

    output_path = root + '/PRmeta' + project + '_ReviewsComments_Filtered.csv'
    output = open(output_path, 'w', encoding='utf-8', newline='')
    CSVwriter = csv.writer(output, delimiter=',')

    header = ['PR_url', 'status', 'createdAt', 'closedAt', 'reviewUrl', 'comment', 'commentTime', 'commentAuthor', 'reaction', 'reactTime', 'reactAuthorLogin', 'reactAuthorEmail']

    CSVwriter.writerow(header)

    input_path = root + '/PRmeta' + project + '_ReviewsComments.csv'
    input = open(input_path, 'r', encoding='utf-8', newline='')
    CSVreader = csv.reader(input, delimiter=',')
    next(CSVreader)

    index = 0

    while True:

        try:
            for row in CSVreader:
                index += 1
                writeFilteredRowV2(CSVwriter, row)

            break

        except:
            #get around encoding error
            input_alt = open(input_path, 'r', encoding='latin-1', newline='')
            CSVreader_alt = csv.reader(input_alt, delimiter=',')
            row = list(CSVreader_alt)[index]

            writeFilteredRowV2(CSVwriter, row)

            next(CSVreader)


if __name__ == '__main__':
    projects = ["Cataclysm-DDA", "Julia", "Laravel", "Node", "RPCS3", "Rust"]
    #projects = ["Cataclysm-DDA"]

    for project in projects:

        filterPRreactionData(project)
        filterPRCommentsReactionData(project)
        filterPRCommitsCommentsReactionData(project)
        filterPRReviewsCommentsReactionData(project)
