#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim: ai ts=4 sts=4 et sw=4
# Alan Viars

import argparse
import functools
import hashlib
import json
import os
import time
import string
import sys
from collections import OrderedDict
import ndjson
import requests


def ndjsonurl2fhir(ndjson_url, fhir_base_url):
    """Return a response_dict with a summary of ndjson2fhir transaction."""
    # print("Start the import of", ndjson_url, "into", fhir_base_url)
    response_dict = OrderedDict()
    fileindex = 0
    index = 0
    error_list = []
    ids = []
    response = requests.get(ndjson_url)
    items = response.json(cls=ndjson.Decoder, object_pairs_hook=OrderedDict)

    for item in items:
        # print("item",item)
        try:
            if not isinstance(item, type(OrderedDict())):
                error_message = "File " + \
                    str(jsonfile) + " did not contain a JSON object, i.e. {}."
                error_list.append(error_message)
            # insert the item/document using an HTTP POST to a FHIR server
            
            resource_url = "%s%s" % (fhir_base_url, item["resourceType"])
            
            r = requests.post(resource_url, json=item, headers = {'Content-type': 'application/json'})
            if r.status_code not in (201,):
                error_message = "Item with ID " + \
                    item['id'] + " failed to POST/CREATE in the FHIR server."
                error_list.append(error_message)
            else:
                ids.append({item['id']:r.json()['id']})
                index += 1
        except:
            # print(sys.exc_info())
            error_message = "File " + \
                str(item) + " did not contain valid JSON."
            error_list.append(error_message)

    if error_list:
        response_dict['ndjson_url'] = ndjson_url
        response_dict['fhir_base_url'] = fhir_base_url
        response_dict['num_fhir_resources_created'] = index
        response_dict['ids'] = ids
        response_dict['num_upload_errors'] = len(error_list)
        response_dict['errors'] = error_list
        response_dict['code'] = 400
        response_dict['message'] = "Completed with errors."

    else:
        response_dict['ndjson_url'] = ndjson_url
        response_dict['fhir_base_url'] = fhir_base_url   
        response_dict['num_fhir_resources_created'] = index
        response_dict['ids'] = ids
        response_dict['num_file_errors'] = len(error_list)
        response_dict['code'] = 200
        response_dict['message'] = "Completed without errors."

    return response_dict

if __name__ == "__main__":

    # Parse args
    parser = argparse.ArgumentParser(
        description='Load in the NDJSON url into a FHIR service by performing POST/CREATE')
    parser.add_argument(
        dest='input_ndjson_url',
        action='store',
        help='Input the NDJSON url to load here')
    parser.add_argument(
        dest='fhir_base_url',
        action='store',
        help="Enter the FHIR base URL where you want to create new new FHIR resources.")

    args = parser.parse_args()
    ndjson_url = args.input_ndjson_url
    fhir_base_url = args.fhir_base_url

    result = ndjsonurl2fhir(ndjson_url, fhir_base_url)

    # output the JSON transaction summary
    print(json.dumps(result, indent=4))
