#!/usr/env/bin python
"""
RefChef Menu - Genome References Management Software
-----------------------------------------------------
This scrpit is used to print a table with information
about the available references in the system.

"""
import os
import argparse
import oyaml as yaml
from refchef.table_utils import *
from refchef.utils import *
from refchef import config

def main():
    parser = argparse.ArgumentParser(description='Get and filter references available in the system.')

    parser.add_argument("--filter", type=str, help="Field:value pair to filter menu on.")
    # parser.add_argument("--regex", "-r",
    #                     help="Whether value passed to filter is a regex expression.",
    #                     action="store_true")
    parser.add_argument('--master', '-f', type=str, help='Path do to master.yaml')
    parser.add_argument('--config', '-c', type=str, help='Path do to config file in .yaml or .ini format.')
    parser.add_argument('--full', action='store_true', help='Whether to show full table, including location and names of files.')
    parser.add_argument('--meta', '-m', type=str, help='Return metadata for specific reference.')
    # Parse arguments
    arguments = parser.parse_args()

    if arguments.config:
        try:
            d = config.yaml(arguments.config)
        except:
            d = config.ini(arguments.config)
        conf = config.Config(**d)

        master = read_yaml(os.path.join(conf.git_local, 'master.yaml'))

    if arguments.master:
        master = read_yaml(os.path.expanduser(arguments.master))


    menu = get_full_menu(master)

    if (arguments.meta is not None):
        pretty_print(get_metadata(menu, arguments.meta))
    else:
        if (arguments.filter is not None):
            filtered = multiple_filter(menu, arguments.filter)
            if arguments.full:
                pretty_print(filtered)
            else:
                partial = filtered[['name', 'organism', 'component', 'description', 'uuid']]

                pretty_print(partial)
        else:
            if arguments.full:
                pretty_print(menu)
            else:
                partial = menu[['name', 'organism', 'component', 'description', 'uuid']]
            pretty_print(partial)

if __name__ == '__main__':
    main()
