#!/opt/anaconda1anaconda2anaconda3/bin/python
# -*- coding: utf-8

import sys
import argparse

import anvio.db as db
import anvio.tables as t
import anvio.dbops as dbops
import anvio.terminal as terminal 

from anvio.errors import ConfigError


run = terminal.Run()
progress = terminal.Progress()


def update_contigs_db_from_v5_to_v6(contigs_db_path, just_do_it = False):
    if contigs_db_path is None:
        raise ConfigError("No database path is given.")

    # make sure someone is not being funny
    dbops.is_contigs_db(contigs_db_path)

    # make sure the version is 2
    contigs_db = db.DB(contigs_db_path, None, ignore_version = True)
    if str(contigs_db.get_version()) != '5':
        raise ConfigError("Version of this contigs database is not 5 (hence, this script cannot really do anything).")

    if not just_do_it:
        try:
            run.warning('This script will try to upgrade your contigs database. As a result of this, you will lose\
                         any existing annotations of taxonomy, and you will have to re-import them later. If you are\
                         OK with that, just press ENTER to continue. If you want to cancel the upgrade, press CTRL+C\
                         now. If you want to avoid this message the next time, use "--just-do-it" flag.')
            input("Press ENTER to continue...\n")
        except:
            print()
            sys.exit()

    progress.new("Trying to upgrade the contigs database")
    progress.update('...')

    # drop the old tables:
    try:
        contigs_db._exec('''DROP TABLE %s''' % (t.splits_taxonomy_table_name))
        contigs_db._exec('''DROP TABLE %s''' % (t.taxon_names_table_name))
        contigs_db._exec('''DROP TABLE %s''' % (t.genes_taxonomy_table_name))
    except:
        pass
    contigs_db.commit()

    # create new empty ones
    contigs_db.create_table(t.splits_taxonomy_table_name, t.splits_taxonomy_table_structure, t.splits_taxonomy_table_types)
    contigs_db.create_table(t.taxon_names_table_name, t.taxon_names_table_structure, t.taxon_names_table_types)
    contigs_db.create_table(t.genes_taxonomy_table_name, t.genes_taxonomy_table_structure, t.genes_taxonomy_table_types)

    # set the version
    contigs_db.remove_meta_key_value_pair('version')
    contigs_db.set_version('6')

    # bye
    contigs_db.disconnect()

    # bye
    progress.end()
    run.info_single("The contigs database successfully upgraded from version 5 to 6!")


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='A simple script to upgrade contigs database from version 5 to version 6')
    parser.add_argument('contigs_db', metavar = 'CONTIGS_DB', help = 'Contigs database')
    parser.add_argument('--just-do-it', default=False, action="store_true", help = "Do not bother me with warnings")
    args = parser.parse_args()

    try:
        update_contigs_db_from_v5_to_v6(args.contigs_db, just_do_it = args.just_do_it)
    except ConfigError as e:
        print(e)
        sys.exit(-1)
