#!python
"""Creates and ontology from an excelfile.

The excel file must be in the formate provided by
ontology_template.xlsx
"""
import argparse
import sys
import os
import warnings
from ontopy.excelparser import create_ontology_from_excel, ExcelError
from ontopy.utils import write_catalog
from ontopy import get_ontology
import owlready2  # pylint: disable=C0411


def english(string):
    """Returns `string` as an English location string."""
    return owlready2.locstr(string, lang="en")


def main(argv: list = None):
    """Main run function.

    Parameters:
        argv: List of arguments, similar to `sys.argv[1:]`.
            Mainly for testing purposes, since it allows one to invoke the tool
            manually / through Python.

    """

    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument(
        "excelpath",
        help="path to excel book",
    )
    parser.add_argument(
        "--output",
        "-o",
        default="ontology.ttl",
        help="Name of output ontology, ´ontology.ttl´ is default",
    )
    parser.add_argument(
        "--force",
        "-f",
        action="store_true",
        help="Whether to force generation of ontology on non-fatal error.",
    )

    parser.add_argument(
        "--update",
        "-u",
        default=True,
        help="Whether to update the the ontology with new concepts "
        "or regenerate the full ontology."
        "Currently only supports adding new concepts"
        "Default is True.",
    )

    parser.add_argument(
        "--input_ontology",
        "-i",
        default=None,
        help="Path of previously generated ontology to update with"
        " new concepts."
        "Deafult is the same as 'output'.",
    )

    try:
        args = parser.parse_args(args=argv)
    except SystemExit as exc:
        sys.exit(exc.code)  # Exit without traceback on invalid arguments

    if args.update:
        try:
            if args.input_ontology:
                input_ontology = get_ontology(args.input_ontology).load()
            else:
                input_ontology = get_ontology(args.output).load()
        except FileNotFoundError as err:
            if args.force:
                warnings.warn(
                    "Did not find the input ontology, "
                    "will fully generate a new one."
                )
                input_ontology = None
            else:
                raise err

    try:
        ontology, catalog, _ = create_ontology_from_excel(
            os.path.abspath(args.excelpath),
            force=args.force,
            input_ontology=input_ontology,
        )
    except ExcelError as exc:
        parser.exit(1, f"ERROR: {exc}\n")

    # Save new ontology as turtle
    ontology.save(os.path.join(args.output), format="turtle", overwrite=True)
    dirname = os.path.dirname(args.output)
    if (not args.update) or (not os.path.exists(dirname + "/catalog-v001.xml")):
        write_catalog(catalog, directory=dirname)


if __name__ == "__main__":
    main()
