import rickled.__version__ as ver
import argparse

from rickled.tools import bcolors

def conv(args):
    from rickled.tools import Converter
    Converter(input_files=args.i,
              output_files=args.o,
              input_directories=args.d,
              default_output_type=args.t,
              silent=args.s).do_convert()

def serve(args):
    from rickled.net import serve_rickle_http, serve_rickle_https
    from rickled import Rickle

    rick = Rickle(args.f)

    if args.s and args.k:
        serve_rickle_https(rickle=rick,
                           path_to_certificate=args.s,
                           path_to_private_key=args.k,
                           port=args.p,
                           interface=args.i
                           )
    else:
        serve_rickle_http(rickle=rick,
                          port=args.p,
                          interface=args.i
                          )

def main():
    parser = argparse.ArgumentParser(
        prog='rickle',
        formatter_class=argparse.RawDescriptionHelpFormatter,
        description=f"""
-----------------------------------------------------
{bcolors.HEADER}YAML tools for Python{bcolors.ENDC} (version {ver}).
""",
        epilog="-----------------------------------------------------"
    )
    parser.add_argument(
        "--version",
        "-v",
        help="show version number",
        action="version",
        version=f'%(prog)s {ver}'
    )

    subparsers = parser.add_subparsers()

    #################### CONV #####################

    parser_conv = subparsers.add_parser('conv',
                                        help='Tool for converting files to or from YAML',
                                        description=f"""
---
{bcolors.HEADER}Tool for converting files to or from YAML{bcolors.ENDC}.
    """,
                                        epilog="-----------------------------------------------------"
                                        )

    parser_conv.add_argument('-i', type=str, help=f"{bcolors.OKBLUE}input file{bcolors.ENDC} to convert",
                             nargs='+', metavar='input')
    parser_conv.add_argument('-d', type=str, help=f"{bcolors.OKBLUE}directory{bcolors.ENDC} of files to convert",
                             default=None, nargs='+', metavar='dir')
    parser_conv.add_argument('-o', type=str, help=f"{bcolors.OKBLUE}output file{bcolors.ENDC} names",
                             nargs='+', metavar='output')
    parser_conv.add_argument('-t', type=str, help=f"default output {bcolors.OKBLUE}file type{bcolors.ENDC} (JSON, YAML)",
                             default='yaml', metavar='type')
    parser_conv.add_argument('-s', action='store_true', help=f"{bcolors.OKBLUE}suppress{bcolors.ENDC} verbose output",)

    parser_conv.set_defaults(func=conv)

    #################### SERVE #####################

    parser_serve = subparsers.add_parser('serve',
                                        help='Tool for serving YAML via http(s)',
                                        description=f"""
    ---
    {bcolors.HEADER}Tool for serving YAML via http(s){bcolors.ENDC}.
        """,
                                        epilog="-----------------------------------------------------"
                                        )

    parser_serve.add_argument('-f', type=str,
                              help=f"{bcolors.OKBLUE}YAML{bcolors.ENDC} or {bcolors.OKBLUE}JSON{bcolors.ENDC} file to serve",
                              metavar='file')
    # TODO implement config
    # parser_serve.add_argument('-c', type=str, help=f"{bcolors.OKBLUE}config{bcolors.ENDC} file path",
    #                           default=None, metavar='config')
    parser_serve.add_argument('-i', type=str, help=f"{bcolors.OKBLUE}host{bcolors.ENDC} interface, address",
                              default='', metavar='host')
    parser_serve.add_argument('-p', type=int, help=f"{bcolors.OKBLUE}port{bcolors.ENDC} number",
                              default=8080, metavar='port')
    parser_serve.add_argument('-k', type=str, help=f"{bcolors.OKBLUE}private key{bcolors.ENDC} file path",
                              default=None, metavar='privkey')
    parser_serve.add_argument('-s', type=str, help=f"{bcolors.OKBLUE}SSL certificate{bcolors.ENDC} file path",
                              default=None, metavar='cert')
    parser_serve.set_defaults(func=serve)

    args = parser.parse_args()

    args.func(args)

if __name__ == "__main__":
    main()