from stimuli.AbstractStimulus import AbstractStimulus
from warnings import warn


class AbstractModel:

    # TODO what information about the model does the ModelParser need to be able to simulate the right kind of data
    #  for further analysis in cell_data/fi_curve etc.

    DEFAULT_VALUES = {}

    def __init__(self, params: dict = None):
        self.parameters = {}
        if params is None:
            self._set_default_parameters()
        else:
            self.set_parameters(params)

    def simulates_voltage_trace(self) -> bool:
        raise NotImplementedError("NOT IMPLEMENTED")

    def simulates_frequency(self) -> bool:
        raise NotImplementedError("NOT IMPLEMENTED")

    def simulates_spiketimes(self) -> bool:
        raise NotImplementedError("NOT IMPLEMENTED")

    def simulate(self, stimulus: AbstractStimulus, total_time_s):
        """
        Simulate the given stimulus in the model
        and simulate up to the given total time
        and saves the simulated data in the model.

        :param stimulus: given stimulus
        :param total_time_s: time to simulate
        :return: depending on availability: [voltage, spiketimes, frequency]
        """
        raise NotImplementedError("NOT IMPLEMENTED")

    def get_voltage_trace(self):
        raise NotImplementedError("NOT IMPLEMENTED")

    def get_spiketimes(self):
        raise NotImplementedError("NOT IMPLEMENTED")

    def get_frequency(self):
        raise NotImplementedError("NOT IMPLEMENTED")

    def min_stimulus_strength_to_spike(self):
        """
        return the minimal stimulus strength needed for the model to spike
        :return: min stimulus strength to spike
        """
        raise NotImplementedError("NOT IMPLEMENTED")

    def get_sampling_interval(self):
        """
        return the "sampling" interval of the model: the time step the model simulates by
        :return: the sampling interval
        """
        raise NotImplementedError("NOT IMPLEMENTED")

    def set_parameters(self, params):
        self._test_given_parameters(params)

        for k in params.keys():
            self.parameters[k] = params[k]

        for key in self.DEFAULT_VALUES.keys():
            if key not in self.parameters.keys():
                self.parameters[key] = self.DEFAULT_VALUES[key]

    def get_parameters(self):
        return self.parameters

    def set_variable(self, key, value):
        if key not in self.DEFAULT_VALUES.keys():
            raise ValueError("Given key is unknown!\n"
                             "Please check spelling and refer to list LIFAC.KEYS.")
        if "tau" in key and value <= 0:
            warn("Time constants cannot be zero or negative! Setting " + str(key) + " to 0.5ms")
            self.parameters[key] = 0.00005
            return
        self.parameters[key] = value

    def _set_default_parameters(self):
        self.parameters = self.DEFAULT_VALUES

    def _test_given_parameters(self, params):
        for k in params.keys():
            if k not in self.DEFAULT_VALUES.keys():
                err_msg = "Unknown key in the given parameters:" + str(k)
                raise ValueError(err_msg)

            if "tau" in k and params[k] < 0:
                warn("Time constants cannot be negative setting" + str(k) + "0.5ms")
                params[k] = 0.00005