#!/home/luis/repos/devops/indexwarrior/virtualenv/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2017 Bitergia
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Authors:
#     Luis Cañas-Díaz <lcanas@bitergia.com>
#

import argparse
import sys

from indexwarrior.errors import InvalidConfigurationFileError, \
    NotFoundConfigurationFileError, MissingShortcutConfigurationFileError, ESConnectionError
from indexwarrior.main import IndexWarrior

if '..' not in sys.path:
    sys.path.insert(0, '..')

DESC_MSG = """"Life among ES indexes is tough so am I"""


def main():
    """Main method."""
    args = parse_args()

    iwarrior = IndexWarrior(args.es_host, timeout=30)
    iwarrior.initialize()

    if args.subparser_name == 'index':
        if args.command == 'show':
            iwarrior.show_indices(args.less)
        elif args.command == 'compare':
            iwarrior.compare_data(args.index_a, args.index_b, args.buckets)
        elif args.command == 'drop':
            iwarrior.set_term(args.term)
            iwarrior.drop(args.date)
        elif args.command == 'delete':
            iwarrior.delete(args.index, args.date_field, args.origin, args.older_than)

    elif args.subparser_name == 'alias':
        if args.command == 'add':
            iwarrior.add_alias(args.index_names, args.alias_name)
        elif args.command == 'delete':
            iwarrior.delete_alias(args.index_names, args.alias_name)
        elif args.command == 'show':
            iwarrior.show_aliases(args.show_filters)
        elif args.command == 'switch':
            iwarrior.switch(args.old_index, args.new_index)
        elif args.command == 'autoupdate':
            iwarrior.autoupdate(args.alias_names)


def parse_args():
    """Parse arguments from the command line."""
    parser = argparse.ArgumentParser(description=DESC_MSG)

    parser.add_argument(dest='es_host',
                        help='ES host. When it does not start by http(s):// the tool understands you are using a shorcut stored at ~/.indexwarrior')

    subparsers = parser.add_subparsers(dest='subparser_name')

    ## index subcommands start here
    parser_index = subparsers.add_parser('index', help='index operations')
    index_subparser = parser_index.add_subparsers(dest='command')

    parser_show = index_subparser.add_parser('show', help='show indexes')
    parser_show.add_argument('--less', dest='less', action='store_true', help='displays less information, useful for slow ES instances')

    parser_compare = index_subparser.add_parser('compare', help='compare two indexes')
    parser_compare.add_argument(dest='index_a', help='first index to be compared')
    parser_compare.add_argument(dest='index_b', help='index to be compared with')
    parser_compare.add_argument('--buckets', required=False, default='10',
                                help='buckets (origins) to be returned')

    parser_drop = index_subparser.add_parser('drop', help='drop index by metadata__timestamp')
    parser_drop.add_argument(dest='date', help='expiration date in format 2017-04-05T07:41:44.993Z')
    parser_drop.add_argument('--term', dest='term', required=False,
                                help='match term with index name')

    parser_delete = index_subparser.add_parser('delete', help='delete index')
    parser_delete.add_argument(dest='index', help='index name')
    parser_delete.add_argument('--origin', help='origin of the docs to be removed')
    parser_delete.add_argument('--older_than', help='data older than this date will be removed')
    parser_delete.add_argument('--date_field', default='grimoire_creation_date',
                               help='date field used to filter data (by default grimoire_creation_date)')



    # alias subcommands start here
    parser_alias = subparsers.add_parser('alias', help='aliases operations')
    alias_subparser = parser_alias.add_subparsers(dest='command')

    parser_add = alias_subparser.add_parser('add', help='add alias')
    parser_add.add_argument(dest='alias_name', help='name of the alias')
    parser_add.add_argument(dest='index_names',  nargs='+', help='list of index names (e.g. index_a index_b)')

    parser_add = alias_subparser.add_parser('delete', help='delete alias')
    parser_add.add_argument(dest='alias_name', help='name of the alias')
    parser_add.add_argument(dest='index_names',  nargs='+', help='list of index names (e.g. index_a index_b)')

    parser_show = alias_subparser.add_parser('show', help='show aliases')
    parser_show.add_argument('--filters', dest='show_filters', action='store_true',
                        help='show aliases and their filters')


    parser_switch = alias_subparser.add_parser('switch', help='move aliases from one index to another')
    parser_switch.add_argument('-o', '--old-index', dest='old_index', required=True,
                        help='index to be switchd')
    parser_switch.add_argument('-n', '--new-index', dest='new_index', required=True,
                        help='new index to be linked')

    parser_autoupdate = alias_subparser.add_parser('autoupdate', help='update alias automaticaly if fresher & bigger index is found')
    parser_autoupdate.add_argument(dest='alias_names',  nargs='+', help='list of alias names to be updated')

    return parser.parse_args()


if __name__ == '__main__':
    try:
        main()
    except KeyboardInterrupt:
        s = "\n\nReceived Ctrl-C or other break signal. Exiting.\n"
        sys.stdout.write(s)
        sys.exit(0)
    except InvalidConfigurationFileError:
        s = "\n\nInvalid configuration file at ~/.indexwarrior. Exiting.\n"
        sys.stdout.write(s)
        sys.exit(0)
    except NotFoundConfigurationFileError:
        s = "\n\nMissing configuration file at ~/.indexwarrior. Exiting.\n"
        sys.stdout.write(s)
        sys.exit(0)
    except MissingShortcutConfigurationFileError:
        s = "\n\nMissing shortcut name at ~/.indexwarrior. Exiting.\n"
        sys.stdout.write(s)
        sys.exit(0)
    except ESConnectionError as e:
        s = "\n\n%s\n"
        sys.stdout.write(s % e.msg)
        sys.exit(0)
