#!/usr/bin/env python
# -*- coding: utf-8

import sys
import argparse

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

from anvio.errors import ConfigError


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

current_version = '8'
next_version    = '9'

def update_contigs_db(contigs_db_path, project_name, description=None, 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 current version matches
    contigs_db = db.DB(contigs_db_path, None, ignore_version = True)
    if str(contigs_db.get_version()) != current_version:
        raise ConfigError("Version of this contigs database is not %s (hence, this script cannot really do anything)." % current_version)

    if not just_do_it:
        try:
            run.warning("This script will try to upgrade your contigs database from v%s to v%s. The reason for this change\
                         is our late realization that it would be much better if we had project names associated with our\
                         contigs databases. This upgrade will simply set the project name you provided to this script in\
                         your contigs database. We also added an option to add descriptions into the contigs databases. So,\
                         OPTIONALLY, you can proivde a description for your contigs databae while you are upgrading it. But\
                         this is not mandatory. If you think you are ready, just press ENTER to continue.\
                         If you want to cancel the upgrade and think more about it, press CTRL+C now. If you want to avoid\
                         this message the next time, use '--just-do-it'." % (current_version, next_version))
            input("Press ENTER to continue...\n")
        except:
            print()
            sys.exit()

    if not project_name:
        raise ConfigError('You must provide a project name. Please see the help menu.')

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

    contigs_db.remove_meta_key_value_pair('project_name')
    contigs_db.set_meta_value('project_name', project_name)
    contigs_db.commit()

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

    # gene name changes:
    contigs_db._exec('''UPDATE %s SET genes = replace(genes, '%s', '%s') WHERE source LIKE 'Rinke_et_al';''' % (t.hmm_hits_info_table_name, 'Ribosomal_S12', 'Ribosom_S12_S23'))
    contigs_db._exec('''UPDATE %s SET genes = replace(genes, '%s', '%s') Where source LIKE 'Rinke_et_al';''' % (t.hmm_hits_info_table_name, 'UPF0027', 'RtcB'))
    contigs_db._exec('''UPDATE %s SET genes = replace(genes, '%s', '%s') Where source LIKE 'Rinke_et_al';''' % (t.hmm_hits_info_table_name, '‐', '-')) # first - is not ASCII

    # bye
    contigs_db.disconnect()

    # bye to you too
    progress.end()

    if description:
        dbops.update_description_in_db_from_file(contigs_db_path, description)

    run.info_single("The contigs database successfully upgraded from version %s to %s!" % (current_version, next_version))


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='A simple script to upgrade contigs database from version %s to version %s' % (current_version, next_version))
    parser.add_argument('contigs_db', metavar = 'CONTIGS_DB', help = 'Contigs database')
    parser.add_argument(*anvio.A('project-name'), **anvio.K('project-name'))
    parser.add_argument(*anvio.A('description'), **anvio.K('description'))
    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(args.contigs_db, args.project_name, args.description, just_do_it = args.just_do_it)
    except ConfigError as e:
        print(e)
        sys.exit(-1)
