# -*- coding: utf-8 -*-
#
#
# TheVirtualBrain-Framework Package. This package holds all Data Management, and
# Web-UI helpful to run brain-simulations. To use it, you also need do download
# TheVirtualBrain-Scientific Package (for simulators). See content of the
# documentation-folder for more details. See also http://www.thevirtualbrain.org
#
# (c) 2012-2020, Baycrest Centre for Geriatric Care ("Baycrest") and others
#
# This program is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software Foundation,
# either version 3 of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE.  See the GNU General Public License for more details.
# You should have received a copy of the GNU General Public License along with this
# program.  If not, see <http://www.gnu.org/licenses/>.
#
#
#   CITATION:
# When using The Virtual Brain for scientific publications, please cite it as follows:
#
#   Paula Sanz Leon, Stuart A. Knock, M. Marmaduke Woodman, Lia Domide,
#   Jochen Mersmann, Anthony R. McIntosh, Viktor Jirsa (2013)
#       The Virtual Brain: a simulator of primate brain network dynamics.
#   Frontiers in Neuroinformatics (7:10. doi: 10.3389/fninf.2013.00010)
#
#

import os
from abc import abstractmethod
from tvb.adapters.analyzers.matlab_worker import MatlabWorker
from tvb.basic.neotraits.api import Attr
from tvb.basic.profile import TvbProfile
from tvb.core.adapters.abcadapter import ABCAsynchronous, ABCAdapterForm
from tvb.core.entities.filters.chain import FilterChain
from tvb.adapters.datatypes.db.connectivity import ConnectivityIndex
from tvb.adapters.datatypes.db.graph import ConnectivityMeasureIndex
from tvb.adapters.datatypes.db.mapped_value import ValueWrapperIndex
from tvb.core.entities.model.model_operation import AlgorithmTransientGroup
from tvb.core.neotraits.forms import TraitDataTypeSelectField
from tvb.core.utils import extract_matlab_doc_string
from tvb.datatypes.connectivity import Connectivity
from tvb.datatypes.graph import ConnectivityMeasure

BCT_GROUP_MODULARITY = AlgorithmTransientGroup("Modularity Algorithms", "Brain Connectivity Toolbox", "bct")
BCT_GROUP_DISTANCE = AlgorithmTransientGroup("Distance Algorithms", "Brain Connectivity Toolbox", "bctdistance")

BCT_PATH = os.path.join(TvbProfile.current.EXTERNALS_FOLDER_PARENT, "externals/BCT")
BCT_PATH_ENV = 'BCT_PATH'
if BCT_PATH_ENV in os.environ and os.path.exists(os.environ[BCT_PATH_ENV]) and os.path.isdir(os.environ[BCT_PATH_ENV]):
    BCT_PATH = os.environ[BCT_PATH_ENV]

LABEL_CONNECTIVITY_BINARY = "Binary (directed/undirected) connection matrix"
LABEL_CONN_WEIGHTED_DIRECTED = "Weighted directed connection matrix"
LABEL_CONN_WEIGHTED_UNDIRECTED = "Weighted undirected connection matrix"


def bct_description(mat_file_name):
    return extract_matlab_doc_string(os.path.join(BCT_PATH, mat_file_name))


class BaseBCTForm(ABCAdapterForm):
    def __init__(self, prefix='', project_id=None, draw_ranges=True):
        super(BaseBCTForm, self).__init__(prefix, project_id, draw_ranges)
        self.connectivity = TraitDataTypeSelectField(Attr(field_type=Connectivity, label=self.get_connectivity_label()),
                                                     self, name="connectivity", conditions=self.get_filters(),
                                                     has_all_option=True)

    @staticmethod
    def get_required_datatype():
        return ConnectivityIndex

    @staticmethod
    def get_filters():
        return None

    @staticmethod
    def get_input_name():
        return "_connectivity"

    @staticmethod
    def get_connectivity_label():
        return "Connection matrix:"


class BaseUnidirectedBCTForm(BaseBCTForm):

    @staticmethod
    def get_filters():
        return FilterChain(fields=[FilterChain.datatype + '.undirected'], operations=["=="], values=['1'])

    @staticmethod
    def get_connectivity_label():
        return "Undirected connection matrix:"


class BaseBCT(ABCAsynchronous):
    """
    Interface between Brain Connectivity Toolbox of Olaf Sporns and TVB Framework.
    This adapter requires BCT deployed locally, and Matlab or Octave installed separately of TVB.
    """

    def __init__(self):
        ABCAsynchronous.__init__(self)
        self.matlab_worker = MatlabWorker()

    @staticmethod
    def can_be_active():
        return not not TvbProfile.current.MATLAB_EXECUTABLE

    def get_form_class(self):
        return BaseBCTForm

    def get_output(self):
        return [ConnectivityMeasureIndex, ValueWrapperIndex]

    def get_required_memory_size(self, **kwargs):
        # We do not know how much memory is needed.
        return -1

    def get_required_disk_size(self, **kwargs):
        return 0

    def execute_matlab(self, matlab_code, **kwargs):
        self.matlab_worker.add_to_path(BCT_PATH)
        self.log.info("Starting execution of MATLAB code:" + matlab_code)
        runcode, matlablog, result = self.matlab_worker.matlab(matlab_code, kwargs)
        self.log.debug("Code run in MATLAB: " + str(runcode))
        self.log.debug("MATLAB log: " + str(matlablog))
        self.log.debug("Finished MATLAB execution:" + str(result))
        return result

    def build_connectivity_measure(self, result, key, connectivity, title="", label_x="", label_y=""):
        # TODO H5
        measure = ConnectivityMeasure()
        measure.array_data = result[key]
        measure.connectivity = connectivity
        measure.title = title
        measure.label_x = label_x
        measure.label_y = label_y
        return measure

    def build_float_value_wrapper(self, result, key, title=""):
        value = ValueWrapperIndex()
        value.data_value = float(result[key])
        value.data_type = 'float'
        value.data_name = title
        return value

    def build_int_value_wrapper(self, result, key, title=""):
        value = ValueWrapperIndex()
        value.data_value = int(result[key])
        value.data_type = 'int'
        value.data_name = title
        return value

    @abstractmethod
    def launch(self, connectivity, **kwargs):
        pass


class BaseUndirected(BaseBCT):
    """
    """

    def get_form_class(self):
        return BaseUnidirectedBCTForm

    @abstractmethod
    def launch(self, connectivity, **kwargs):
        pass


class ModularityOCSM(BaseBCT):
    """
    """
    _ui_group = BCT_GROUP_MODULARITY
    _ui_connectivity_label = "Directed (weighted or binary) connection matrix:"

    _ui_name = "Optimal Community Structure and Modularity"
    _ui_description = bct_description("modularity_dir.m")
    _matlab_code = "[Ci,Q] = modularity_dir(CW);"

    def launch(self, connectivity, **kwargs):
        # Prepare parameters
        kwargs['CW'] = connectivity.weights
        # Execute the matlab code
        result = self.execute_matlab(self._matlab_code, **kwargs)
        # Gather results
        measure = self.build_connectivity_measure(result, 'Ci', connectivity, "Optimal Community Structure")
        value = self.build_float_value_wrapper(result, 'Q', title="Maximized Modularity")
        return [measure, value]


class ModularityOpCSMU(ModularityOCSM):
    """
    """
    _ui_name = "Optimal Community Structure and Modularity (Undirected)"
    _ui_description = bct_description("modularity_und.m")
    _matlab_code = "[Ci,Q] = modularity_und(CW);"


class DistanceDBIN(BaseBCT):
    """
    """
    _ui_group = BCT_GROUP_DISTANCE

    _ui_name = "Distance binary matrix"
    _ui_description = bct_description("distance_bin.m")
    _matlab_code = "D = distance_bin(A);"

    def launch(self, connectivity, **kwargs):
        kwargs['A'] = connectivity.weights
        result = self.execute_matlab(self._matlab_code, **kwargs)
        measure = self.build_connectivity_measure(result, 'D', connectivity, "Distance matrix")
        return [measure]


class DistanceDWEI(DistanceDBIN):
    """
    """
    _ui_connectivity_label = "Weighted (directed/undirected) connection matrix:"
    _ui_name = "Distance weighted matrix"
    _ui_description = bct_description("distance_wei.m")
    _matlab_code = "D = distance_wei(A);"


class DistanceRDM(DistanceDBIN):
    """
    """
    _ui_name = "Reachability and distance matrices (Breadth-first search)"
    _ui_description = bct_description("breadthdist.m")
    _matlab_code = "[R,D] = breadthdist(A);"

    def launch(self, connectivity, **kwargs):
        kwargs['A'] = connectivity.weights
        result = self.execute_matlab(self._matlab_code, **kwargs)

        measure1 = self.build_connectivity_measure(result, 'R', connectivity, "Reachability matrix")
        measure2 = self.build_connectivity_measure(result, 'D', connectivity, "Distance matrix")
        return [measure1, measure2]


class DistanceRDA(DistanceRDM):
    """
    """
    _ui_name = "Reachability and distance matrices (Algebraic path count)"
    _ui_description = bct_description("reachdist.m")
    _matlab_code = "[R,D] = reachdist(A);"


class DistanceNETW(DistanceDBIN):
    """
    """
    _ui_name = "Network walks"
    _ui_description = bct_description("findwalks.m")
    _matlab_code = "[Wq,twalk,wlq]  = findwalks(A);"

    def launch(self, connectivity, **kwargs):
        kwargs['A'] = connectivity.weights
        result = self.execute_matlab(self._matlab_code, **kwargs)

        measure1 = self.build_connectivity_measure(result, 'Wq', connectivity, "3D matrix")
        measure2 = self.build_connectivity_measure(result, 'wlq', connectivity, "Walk length distribution")
        value = self.build_float_value_wrapper(result, 'twalk', title="Total number of walks found")
        return [measure1, value, measure2]
