#!/usr/bin/python3

import argparse
import csv
import datetime
import json

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('-y', '--year', type=int, default=2018,
                        help="select year to process")
    args = parser.parse_args()
    year = args.year

    sioadata = {}
    pubrecords = []
    mapping = []
    
    stats = {
        'sioa-match': 0,
        'sioa-nomatch': 0,
        'sioa-nomatch-pbe': 0,
        'sioa-not-npl': 0,
        'npl-count': 0,
        'npl-count-sioa': 0,
    }

    jsonname = 'postjournal-sioa-%d-map.json' % year
    with open(jsonname) as json_data:
        mapping = json.load(json_data)

    jsonname = 'npl-sioa.json'
    with open(jsonname) as json_data:
        j = json.load(json_data)
        pubrecords = j['documents']

    # Convert to datetime object and store the preferred date as _docdate
    for record in pubrecords:
#        print(record)
        for field in ('document_recorded_date', 'document_date'):
            if field in record:
                record[field] = datetime.datetime.strptime(record[field],
                                                           "%Y-%m-%d").date()
                record['_docdate'] = record[field]

    csvname = "postjournal-sioa-%d.tsv" % year
    with open(csvname, 'r') as csvfile:
        csvdata = csv.reader(csvfile, delimiter='\t', quotechar='"')
        header = next(csvdata)
        for row in csvdata:
            docid = int(row[0])
            if docid in sioadata:
                print("error: ignoring entry with duplicate document id %s in %s" % (docid, csvname))
            else:
                row[1] = datetime.datetime.strptime(row[1].split()[0],
                                                    "%d.%m.%Y").date()
                sioadata[docid] = row[1:]
#    print(sioadata)

    for docid in sioadata.keys():
        docdate = sioadata[docid][0]

        # See if we already found a mapping
        match = None
        for map in mapping:
            for mapdocid in map['sioa']:
#                print("Checking if docid %d matches %d" % (docid, mapdocid))
                if docid == mapdocid:
                    match = []
                    if 'npl' in map:
                        for id in map['npl']:
                            found = False
                            for record in pubrecords:
                                if record['id'] == id:
                                    record['_flag'] = '*'
                                    match.append(record)
                                    found = True
                            if not found:
                                record = {
                                    'id': id,
                                    '_flag': '*',
                                    '_docdate': sioadata[docid][0],
                                    'document_title': 'unknown',
                                    'entity_id': 'unknown',
                                }
                                match.append(record)
                                stats['sioa-not-npl'] += 1
                    break
            if match:
                break
        if match:
            stats['sioa-match'] += 1
        else:
            stats['sioa-nomatch'] += 1
            # Count PBE related documents while PBE is missing from NPL
            if -1 != sioadata[docid][1].lower().find("pbe"):
                stats['sioa-nomatch-pbe'] += 1
#        print(match)
        if not match:
            match = []
            for record in pubrecords:
                inrange = False
                if neardate(docdate, record['_docdate'], 31):
                    match.append(record)

        print('')
        print("%s SiOA #%d %s %s" % (
            "x" if 1 == len(match) and '_flag' in match[0] else " ",
            docid, docdate, sioadata[docid][1])
        )

        for record in sorted(match, key=lambda k: k['_docdate']):
            print(" %s %s %s (%s \"%s\")" % (
                '*' if '_flag' in record else ' ',
                record['_docdate'],
                record['document_title'],
                record['entity_id'],
                record['id'])
            )

    # Count public records to SiOA between first and last flagged entry
    mindate = datetime.date(year=year, month=1, day=1)
    maxdate = datetime.date(year=year, month=12, day=31)
    for record in sorted(pubrecords, key=lambda k: k['_docdate']):
        if '_flag' in record:
            if mindate > record['_docdate']:
                mindate = record['_docdate']
            if maxdate < record['_docdate']:
                maxdate = record['_docdate']
        if mindate <= record['_docdate'] and record['_docdate'] <= maxdate:
            stats['npl-count'] += 1
            if '_flag' in record:            
                stats['npl-count-sioa'] += 1
    print("Period measured for NPL: %s - %s" % (mindate, maxdate))
    print(stats)

def neardate(date, test, spread):
    delta = datetime.timedelta(days=spread)
    return test < date + delta and test > date - delta

if '__main__' == __name__:
    main()

