#!/usr/bin/env python

import sys

from argparse import ArgumentParser

from m4db_database.export_json_utilities import export_material


def get_command_line_parser():
    r"""
    Retrieve a command line parser object for this script.

    Returns:
        A command line parser object for this script.

    """
    parser = ArgumentParser()

    parser.add_argument("dir_path",
                        help="output directory, will produce a file called 'material.json'")
    parser.add_argument("-l", "--list", action="append",
                        help="a list of material name, temperature (delimited by colon), i.e 'magnetite:20.000")
    parser.add_argument("-f", "--formatted", action="store_true",
                        help="flag to indicate whether the output should be formatted")
    return parser


def main():
    parser = get_command_line_parser()
    args = parser.parse_args()

    print("Exporting database materials to '{}'...".format(args.dir_path))
    if args.list is not None:
        name_temperature_list = []
        for item in args.list:
            name_temperature = item.split(':')
            if len(name_temperature) != 2:
                print("The name/temperature input '{}' is not correct format.".format(item))
                sys.exit(1)
            name = name_temperature[0].strip()
            temperature  = name_version[1].strip()
            name_temperature_list.append((name, temperature))
        export_material(args.dir_path, name_temperatuure_list, args.formatted)
    else:
        export_materals(args.dir_path, args.list, args.formatted)
    print("done!")


if __name__ == "__main__":
    main()
