#!/usr/bin/env python

import os
import argparse

__author__ = "Shyue Ping Ong"
__copyright__ = "Copyright 2016, The Materials Virtual Lab"
__version__ = "0.1"
__maintainer__ = "Shyue Ping Ong"
__email__ = "shyuep@gmail.com"
__date__ = "2/27/2016"


import time
import webbrowser


def run_server(args):
    os.environ["FLAMYNGO"] = args.config
    from flamyngo.app import app
    if args.browser:
        from multiprocessing import Process
        p = Process(target=app.run,
                     kwargs={"debug": args.debug, "host": args.host,
                             "port": args.port})
        p.start()
        time.sleep(2)
        webbrowser.open("http://{}:{}".format(args.host, args.port))
        p.join()
    else:
        app.run(debug=args.debug, host=args.host, port=args.port)


if __name__ == "__main__":

    parser = argparse.ArgumentParser(
        description="""flamyngo is a basic Flask frontend for querying
        MongoDB collections.""",
        epilog="Author: Shyue Ping Ong")

    parser.add_argument(
        "-c", "--config", dest="config", type=str, nargs="?",
        default=os.path.join(os.environ["HOME"], ".flamyngo.yaml"),
        help="YAML file where the config is stored")
    parser.add_argument(
        "-b", "--browser", dest="browser", action="store_true",
        help="Automatically launch in browser.")
    parser.add_argument(
        "-d", "--debug", dest="debug", action="store_true",
        help="Whether to run in debug mode.")
    parser.add_argument(
        "-hh", "--host", dest="host", type=str, nargs="?",
        default='0.0.0.0',
        help="Host in which to run the server. Defaults to 0.0.0.0.")
    parser.add_argument(
        "-p", "--port", dest="port", type=int, nargs="?",
        default=5000,
        help="Port in which to run the server. Defaults to 5000.")

    args = parser.parse_args()

    run_server(args)