import os
import datetime
import logging
import json
import uuid

from installed_clients.WorkspaceClient import Workspace as Workspace
from installed_clients.KBaseReportClient import KBaseReport
from installed_clients.annotation_ontology_apiServiceClient import annotation_ontology_api

import MergeMetabolicAnnotations.utils.functions as f


class CompareAnnotationsUtil:

    def __init__(self, config):
        self.config = config
        self.timestamp = datetime.datetime.now().strftime("%Y_%m_%d_%H_%M_%S")
        self.callback_url = config['SDK_CALLBACK_URL']
        self.scratch = config['scratch']
        self.kbr = KBaseReport(self.callback_url)
        self.anno_api = annotation_ontology_api()
        self.ws_client = Workspace(config["workspace-url"])

    def run(self, ctx, params):
        get_ontology_results = self.anno_api.get_annotation_ontology_events({
            "input_ref": params['genome'],
            "workspace-url": self.config["workspace-url"]
        })

        ontology_selected = f.filter_selected_ontologies(
            get_ontology_results, params, workflow="compare")
        with open(os.path.join(self.scratch, "get_ontology_dump.json"), 'w') as outfile:
            json.dump(ontology_selected, outfile, indent=2)

        # make reports
        html_reports = []
        output_directory = os.path.join(self.scratch, str(uuid.uuid4()))
        os.mkdir(output_directory)

        event_summary = f.get_event_lists(ontology_selected)
        html_reports = f.compare_report_stack(html_reports, event_summary, output_directory)

        # finalize html reports
        report_params = {
            'message': '',
            'html_links': html_reports,
            'direct_html_link_index': 0,
            'workspace_name': params['workspace_name'],
            'report_object_name': f'compare_annotations_{uuid.uuid4()}'}

        report_output = self.kbr.create_extended_report(report_params)

        return {'report_name': report_output['name'],
                'report_ref': report_output['ref']}

[(21, 'installed_clients.KBaseReportClient.KBaseReport', 'KBaseReport', False, 'from installed_clients.KBaseReportClient import KBaseReport\n'), (22, 'installed_clients.annotation_ontology_apiServiceClient.annotation_ontology_api', 'annotation_ontology_api', False, 'from installed_clients.annotation_ontology_apiServiceClient import annotation_ontology_api\n'), (23, 'installed_clients.WorkspaceClient.Workspace', 'Workspace', True, 'from installed_clients.WorkspaceClient import Workspace as Workspace\n'), (31, 'MergeMetabolicAnnotations.utils.functions.filter_selected_ontologies', 'f.filter_selected_ontologies', True, 'import MergeMetabolicAnnotations.utils.functions as f\n'), (39, 'os.mkdir', 'os.mkdir', False, 'import os\n'), (41, 'MergeMetabolicAnnotations.utils.functions.get_event_lists', 'f.get_event_lists', True, 'import MergeMetabolicAnnotations.utils.functions as f\n'), (42, 'MergeMetabolicAnnotations.utils.functions.compare_report_stack', 'f.compare_report_stack', True, 'import MergeMetabolicAnnotations.utils.functions as f\n'), (34, 'json.dump', 'json.dump', False, 'import json\n'), (18, 'datetime.datetime.now', 'datetime.datetime.now', False, 'import datetime\n'), (33, 'os.path.join', 'os.path.join', False, 'import os\n'), (38, 'uuid.uuid4', 'uuid.uuid4', False, 'import uuid\n'), (50, 'uuid.uuid4', 'uuid.uuid4', False, 'import uuid\n')]