#!/usr/bin/env python

# Copyright (c) 2015, Ecole Polytechnique Federale de Lausanne, Blue Brain Project
# All rights reserved.
#
# This file is part of NeuroM <https://github.com/BlueBrain/NeuroM>
#
# 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. Neither the name of the copyright holder nor the names of
#        its contributors may be used to endorse or promote products
#        derived from this software without specific prior written permission.
#
# 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.

'''Examples of basic data checks'''
from neurom.io.utils import load_neuron, get_morph_files
from neurom.check import ok as chk_ok
from neurom.check import morphology as morph_chk
from neurom.exceptions import SomaError, IDSequenceError
import argparse
import logging
import yaml
import os
import sys

DESCRIPTION = '''
NeuroM Morphology Checker
=========================
'''

EPILOG = '''
Description
-----------

Performs checks on reconstructed morphologies frim data contained in
morphology files.

Files are unloaded into neuron objects before testing. This means they must
have a soma and no format errors.

Default errors checked for
------------------
* No soma
* No axon
* No apical dendrite
* No basal dendrite
* Zero radius points
* Zero length segments
* Zero length sections

Default values for options
--------------
* nonzero_neurite_radii: 0.007
* nonzero_segment_lengths: 0.01
* nonzero_section_lengths: 0.01

Examples
--------
morph_check --help               # print this help
morph_check some/path/neuron.h5  # Process an HDF5 file
morph_check some/path/neuron.swc # Process an SWC file
morph_check some/path/           # Process all HDF5 and SWC files found in directory
'''

L = logging.getLogger(__name__)


def _setup_logging(debug, log_file):
    """ Set up logger """

    fmt = logging.Formatter('%(levelname)s: %(message)s')

    level = logging.DEBUG if debug else logging.INFO
    logging.basicConfig(level=level)
    log = logging.getLogger()
    log.handlers[0].setFormatter(fmt)

    if log_file:
        handler = logging.FileHandler(log_file)
        handler.setFormatter(fmt)
        log = logging.getLogger()
        log.addHandler(handler)
        log.setLevel(logging.DEBUG)


def parse_args():
    '''Parse command line arguments'''
    parser = argparse.ArgumentParser(description=DESCRIPTION,
                                     formatter_class=argparse.RawTextHelpFormatter,
                                     epilog=EPILOG)
    parser.add_argument('datapath',
                        help='Path to morphology data file or directory')

    parser.add_argument('-d', '--debug',
                        action='store_true',
                        help="Log DEBUG information")

    parser.add_argument('-l', '--log', dest='log_file',
                        default="", help="File to log to")

    parser.add_argument('-C', '--config', help='Configuration File')

    return parser.parse_args()


def log_msg(ok, msg, color=False):
    '''Helper to log message to the right level'''
    if color:
        CGREEN, CRED, CEND = '\033[92m', '\033[91m', '\033[0m'
    else:
        CGREEN = CRED = CEND = ''

    LOG_LEVELS = {False: logging.ERROR, True: logging.INFO}

    L.log(LOG_LEVELS[ok],
          '%35s %s' + CEND, msg, CGREEN + 'PASS' if ok else CRED + 'FAIL')


def test_file(f, config):
    '''Run tests on a morphology file'''

    L.info('Check file %s...', f)

    try:
        nrn = load_neuron(f)
        log_msg(True, 'Has valid soma?')
    except SomaError:
        log_msg(False, 'Has valid soma?')
        L.warning('Cannot continue without a soma... Aborting')
        return False

    result = True

    for check in config['neuron_checks']:

        if check in config['options']:
            fargs = config['options'][check]
            if isinstance(fargs, list):
                out = getattr(morph_chk, check)(nrn, *fargs)
            else:
                out = getattr(morph_chk, check)(nrn, fargs)
        else:
            out = getattr(morph_chk, check)(nrn)

        ok = chk_ok(out)
        log_msg(ok, check.replace('_', ' ').capitalize() + '?')

        try:
            if len(out) > 0:
                L.debug('%d failing sections detected: %s', len(out), out)
        except TypeError:
            pass

        result &= ok

    return result


if __name__ == '__main__':

    args = parse_args()
    _setup_logging(args.debug, args.log_file)
    data_path = args.datapath
    yaml_path = args.config
    SEPARATOR = '=' * 40

    if yaml_path:
        try:
            with open(yaml_path, 'r') as stream:
                _config = yaml.load(stream)

        except yaml.scanner.ScannerError as e:
            L.error('Invalid yaml file : \n %s', str(e))
            sys.exit(1)
    else:
        # set default checks
        _config = {'neuron_checks': ['has_basal_dendrite',
                                     'has_axon',
                                     'has_apical_dendrite',
                                     'nonzero_segment_lengths',
                                     'nonzero_section_lengths',
                                     'nonzero_neurite_radii'],
                   'options': {"nonzero_neurite_radii": 0.007,
                               "nonzero_segment_lengths": 0.01,
                               "nonzero_section_lengths": 0.01}}

    if 'color' in _config:
        import functools
        log_msg = functools.partial(log_msg, color=_config['color'])

    if os.path.isfile(data_path):
        files = [data_path]
    elif os.path.isdir(data_path):
        print 'Checking files in directory', data_path
        files = get_morph_files(data_path)
    else:
        L.error('Invalid data path %s', data_path)
        sys.exit(1)

    for _f in files:
        L.info(SEPARATOR)
        try:
            res = test_file(_f, _config)
            log_msg(res, 'Check result:')
        except IDSequenceError as e:
            print 'ID ERROR in file %s: %s' % (_f, e.message)
        except StandardError as e:
            print 'ERROR: Could not read file %s: %s' % (_f, e.message)

    L.info(SEPARATOR)
