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

import os
import sys

import anvio
import anvio.dbops as dbops
import anvio.terminal as terminal
import anvio.dictio as d

from anvio.errors import ConfigError, FilesNPathsError
from anvio.auxiliarydataops import HDF5_IO


__author__ = "A. Murat Eren"
__copyright__ = "Copyright 2015, The anvio Project"
__credits__ = []
__license__ = "GPL 3.0"
__version__ = anvio.__version__
__maintainer__ = "A. Murat Eren"
__email__ = "a.murat.eren@gmail.com"


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


def create_HDF5_summary_from_summary_cp(args):
    summary_cp_path = args.summary_cp
    profile_db_path = args.profile_db

    dbops.is_profile_db(profile_db_path)

    profile_db = dbops.ProfileDatabase(profile_db_path)
    profile_db_hash = profile_db.db.get_meta_value('contigs_db_hash')
    profile_db.disconnect()

    progress.new("Importing summary files")
    progress.update('...')

    if not os.path.exists(summary_cp_path):
        raise ConfigError, "No SUMMARY.cp was found at '%s'" % summary_cp_path

    input_directory_prefix = os.path.dirname(summary_cp_path)
    output_file_name = os.path.join(os.path.dirname(summary_cp_path), 'AUXILIARY-DATA.h5')
    summary_cp_content = d.read_serialized_object(summary_cp_path)

    h = HDF5_IO(output_file_name, profile_db_hash, create_new = True)

    counter = 1
    for split_name in summary_cp_content:
        progress.update('Reading summary cp files for each split ... %d' % counter)
        c = d.read_serialized_object(os.path.join(input_directory_prefix, summary_cp_content[split_name]))

        for sample_name in c:
            h.add_integer_list('/data/coverages/%s/%s' % (split_name, sample_name), c[sample_name]['coverage'])

        counter += 1

    progress.end()

    h.close()

    run.info_single("Your SUMMARY.cp file was successfuly stored in 'AUXILIARY-DATA.h5'. Now you can delete 'SUMMARY.cp'\
                     and the 'SUMMARY/' directory from your analysis directory, since you have the same information\
                     stored in a better way! Yay for better design and increased speed and flexibility...")


if __name__ == '__main__':
    import argparse
    parser = argparse.ArgumentParser(description = "Create an AUXILIARY-DATA.h5 from SUMMARY.cp. For details, please see\
                                                    release notes for anvi'o version 1.2.0 and the related issue in the\
                                                    codebase (https://github.com/meren/anvio/issues/202).")

    parser.add_argument('profile_db')
    parser.add_argument('summary_cp')

    args = parser.parse_args()

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