#!/usr/bin/env python
# -*- coding: utf-8
"""Remove stuff from misc data tables"""

import sys
import argparse

import anvio
import anvio.terminal as terminal

from anvio.errors import ConfigError, FilesNPathsError
from anvio.tables.miscdata import MiscDataTableFactory


__author__ = "Developers of anvi'o (see AUTHORS.txt)"
__copyright__ = "Copyleft 2015-2018, the Meren Lab (http://merenlab.org/)"
__credits__ = []
__license__ = "GPL 3.0"
__version__ = anvio.__version__
__email__ = "a.murat.eren@gmail.com"
__description__ = "Remove stuff from additional data or order tables in pan or profile databases for items or layers"


run = terminal.Run()


def main(args):
    table_for_additional_data = MiscDataTableFactory(args)

    if args.list_available_keys:
        table_for_additional_data.list_data_keys()
        sys.exit(0)

    keys_to_remove = [k for k in args.keys_to_remove.split(',')] if args.keys_to_remove else []
    groups_to_remove = [k for k in args.groups_to_remove.split(',')] if args.groups_to_remove else []

    table_for_additional_data.remove(data_keys_list=keys_to_remove, data_groups_list=groups_to_remove)


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description=__description__)

    parser.add_argument(*anvio.A('pan-or-profile-db'), **anvio.K('pan-or-profile-db'))
    parser.add_argument(*anvio.A('target-data-table'), **anvio.K('target-data-table', {'required': True}))
    parser.add_argument('--keys-to-remove', help="A comma-separated list of data keys to remove from the database. If you\
                                                  do not use this parameter, anvi'o will simply remove everything from the\
                                                  target data table immediately. Please note that you should not use this\
                                                  parameter together with `--groups-to-remove` in a single command.")
    parser.add_argument('--groups-to-remove', help="A comma-separated list of data groups to remove from the database. If you\
                                                  do not use this parameter, anvi'o will simply remove everything from the\
                                                  target data table immediately. Please note that you should not use this\
                                                  parameter together with `--keys-to-remove` in a single command.")
    parser.add_argument('--list-available-keys', action='store_true', help="Using this flag will list available data keys in\
                                                  the target data table and quit without doing anything else.")
    parser.add_argument(*anvio.A('just-do-it'), **anvio.K('just-do-it'))

    args = anvio.get_args(parser)

    try:
        main(args)
    except ConfigError as e:
        print(e)
        sys.exit(-1)
    except FilesNPathsError as e:
        print(e)
        sys.exit(-2)
