# Allen Institute Software License - This software license is the 2-clause BSD
# license plus a third clause that prohibits redistribution for commercial
# purposes without further permission.
#
# Copyright 2017. Allen Institute. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# 3. Redistributions for commercial purposes are not permitted without the
# Allen Institute's written permission.
# For purposes of this license, commercial purposes is the incorporation of the
# Allen Institute's software into anything for which you will charge fees or
# other compensation. Contact terms@alleninstitute.org for commercial licensing
# opportunities.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
from . import runner as single_cell
import logging
import os
import sys
import traceback
import subprocess
import logging.config as lc
from ..biophys_sim.config import Config
from pkg_resources import resource_filename  # @UnresolvedImport


class RunSimulate(object):
    _log = logging.getLogger('allensdk.model.biophysical.run_simulate')

    def __init__(self,
                 input_json,
                 output_json):
        self.input_json = input_json
        self.output_json = output_json
        self.app_config = None
        self.manifest = None

    def load_manifest(self):
        self.app_config = Config().load(self.input_json)
        self.manifest = self.app_config.manifest
        fix_sections = ['passive', 'axon_morph,', 'conditions', 'fitting']
        self.app_config.fix_unary_sections(fix_sections)

    def nrnivmodl(self):
        RunSimulate._log.debug("nrnivmodl")

        subprocess.call(['nrnivmodl', './modfiles'])

    def simulate(self):
        from allensdk.internal.api.queries.biophysical_module_reader \
            import BiophysicalModuleReader

        self.load_manifest()

        try:
            stimulus_path = self.manifest.get_path('stimulus_path')
            RunSimulate._log.info("stimulus path: %s" % (stimulus_path))
        except:
            raise Exception(
                'Could not read input stimulus path from input config.')

        try:
            out_path = self.manifest.get_path('output_path')
            RunSimulate._log.info("result NWB file: %s" % (out_path))
        except:
            raise Exception('Could not read output path from input config.')

        try:
            morphology_path = self.manifest.get_path('MORPHOLOGY')
            RunSimulate._log.info("morphology path: %s" % (morphology_path))
        except:
            raise Exception(
                'Could not read morphology path from input config.')

        single_cell.run(self.app_config)

        lims_upload_config = BiophysicalModuleReader()
        lims_upload_config.read_json(
            self.manifest.get_path('neuronal_model_run_data'))
        lims_upload_config.update_well_known_file(out_path)
        lims_upload_config.set_workflow_state('passed')
        lims_upload_config.write_file(self.output_json)


def main(command, lims_strategy_json, lims_response_json):
    ''' Entry point for module.
        :param command: select behavior, nrnivmodl or simulate
        :type command: string
        :param lims_strategy_json: path to json file output from lims.
        :type lims_strategy_json: string
        :param lims_response_json: path to json file returned to lims.
        :type lims_response_json: string
    '''
    rs = RunSimulate(lims_strategy_json,
                     lims_response_json)

    RunSimulate._log.debug("command: %s" % (command))
    RunSimulate._log.debug("lims strategy json: %s" % (lims_strategy_json))
    RunSimulate._log.debug("lims upload json: %s" % (lims_response_json))

    log_config = resource_filename('allensdk.model.biophysical.run_simulate',
                                   'logging.conf')
    lc.fileConfig(log_config)
    os.environ['LOG_CFG'] = log_config

    if 'nrnivmodl' == command:
        rs.nrnivmodl()
    else:
        rs.simulate()


if __name__ == '__main__':
    command, input_json, output_json = sys.argv[-3:]

    try:
        main(command, input_json, output_json)
        RunSimulate._log.debug("success")
    except Exception as e:
        RunSimulate._log.error(traceback.format_exc())
        exit(1)
