#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim: ai ts=4 sts=4 et sw=4
# Written by Alan Viars  - This software is public domain

import json
import sys
from baluhn import verify
from pdt.pjson.validate_affiliations import validate_affiliation_list

LUHN_PREFIX = "80840"


def validate_pjson(j):
    """
    Input a JSON object and an action. return a list of errors. If error list
    is empty then the file is valid.
    """

    errors = []
    warnings = []
    response = {"errors": [], "warnings": []}

    # Does the string contain JSON
    try:
        d = json.loads(j)
    except:
        error = "The string did not contain valid JSON."
        errors.append(error)
        response["errors"] = errors
        return response

    # Is it a dict {} (JSON object equiv)?
    if not isinstance(d, type({})):
        error = "The JSON string did not contain a JSON object i.e. {}."
        errors.append(error)
        response["errors"] = errors
        return response

    # Does it contain the top-level enumeration_type
    if "enumeration_type" not in d:
        error = "The JSON object does not contain an enumeration_type."
        errors.append(error)
        response["errors"] = errors
        return response

    # Check for enumeration number
    if "number" not in d:
        error = "The JSON object does not contain an enumeration number."
        errors.append(error)
        response["errors"] = errors
        return response

    # Is the enumeration_type a valid?
    if d["enumeration_type"] not in ("NPI-1", "NPI-2", "OEID", "HPID"):
        error = "enumeration_type must be one of these: ('NPI-1', 'NPI-2', 'OEID', 'HPID')"
        errors.append(error)
        response["errors"] = errors
        return response

    # Check if the Luhn checkdigit makes is correcense.
    if d['enumeration_type'] in ('NPI-1', 'NPI-2'):

        prefixed_number = "%s%s" % (LUHN_PREFIX, d['number'])
        luhn_verified = verify(prefixed_number)
        if not luhn_verified:
            error = "The number %s did not pass Luhn algorithm check digit sanitiy check." % (d[
                                                                                              'number'])
            errors.append(error)
            response["errors"] = errors
            return response

    affiliation_errors, affiliation_warnings = validate_affiliation_list(
        d.get('affiliations', []), d.get('enumeration_type'))

    errors = errors + affiliation_errors
    warnings = warnings + affiliation_warnings
    response["errors"] = errors
    response["warnings"] = warnings
    return response


if __name__ == "__main__":

    # Get the file from the command line
    if len(sys.argv) < 2:
        print("You must supply a a ProviderJSON file to validate")
        print("Usage: validate_pjson_affiliations [ProivderJSON]")
        sys.exit(1)
    else:
        pjson_file = sys.argv[1]

    # Try to open the file
    try:
        fh = open(pjson_file, 'r')

        j = fh.read()

        # Validate the provider JSON content
        errors = validate_pjson(j)
        # Print the errors and warnings as JSON to stout.
        errors_json = json.dumps(errors, indent=4)
        print(errors_json)
    except IOError:
        print("Could not open file %s." % (pjson_file))
        sys.exit(1)
