# *****************************************************************************
# 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 2 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, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# Module authors:
#   Markus Zolliker <markus.zolliker@psi.ch>
# *****************************************************************************

"""interlocks for furnace"""

from frappy.core import Module, Writable, Attached, Parameter, FloatRange, Readable, \
     BoolType, ERROR, IDLE, Command, StringType
from frappy.errors import ImpossibleError, ConfigError
from frappy.ctrlby import WrapControlledBy
from frappy.lib import clamp, merge_status
from frappy_psi.picontrol import PImixin
from frappy_psi.convergence import HasConvergence
from frappy_psi.ionopimax import CurrentInput, LogVoltageInput


class Interlocks(Writable):
    value = Parameter('interlock o.k.', BoolType(), default=True)
    target = Parameter('set to true to confirm', BoolType(), readonly=False)
    input = Attached(Readable, 'the input module', mandatory=False)  # TODO: remove
    vacuum = Attached(Readable, 'the vacuum pressure', mandatory=False)
    wall_T = Attached(Readable, 'the wall temperature', mandatory=False)
    htr_T = Attached(Readable, 'the heater temperature', mandatory=False)
    main_T = Attached(Readable, 'the main temperature')
    extra_T = Attached(Readable, 'the extra temperature')
    control = Attached(Module, 'the control module')
    reg_T = Attached(Module, 'an other controlled module', mandatory = False)
    htr = Attached(Module, 'the heater module', mandatory=False)
    relais = Attached(Writable, 'the interlock relais', mandatory=False)
    flowswitch = Attached(Readable, 'the flow switch', mandatory=False)
    wall_limit = Parameter('maximum wall temperature', FloatRange(0, unit='degC'),
                           default = 50, readonly = False)
    vacuum_limit = Parameter('maximum vacuum pressure', FloatRange(0, unit='mbar'),
                             default = 0.1, readonly = False)
    htr_T_limit = Parameter('maximum htr temperature', FloatRange(0, unit='degC'),
                            default = 530, readonly = False)
    main_T_limit = Parameter('maximum main temperature', FloatRange(0, unit='degC'),
                             default = 530, readonly = False)
    extra_T_limit = Parameter('maximum extra temperature', FloatRange(0, unit='degC'),
                              default = 530, readonly = False)
    disabled_checks = Parameter('checks to disable', StringType(),
                                value = '', readonly = False)
    autoreset = Parameter('flag: reset on writing target', BoolType(),
                          value = False, readonly = False)

    _off_reason = None  # reason triggering interlock
    _violations = ''  # summary of reasons why locked now
    _all_violations = ''  # violations disregarding disabled_checks
    _sensor_checks = ()
    _controllers = ()
    _disable_flow_check = False
    __inside_switch_off = False

    SENSOR_MAP = {
        'wall_T': 'wall_limit',
        'main_T': 'main_T_limit',
        'extra_T': 'extra_T_limit',
        'htr_T': 'htr_T_limit',
        'vacuum': 'vacuum_limit',
    }

    def initModule(self):
        super().initModule()
        self._controllers = [self.control]
        if self.reg_T:
            self._controllers .append(self.reg_T)
        for mod in self._controllers:
            mod.callback_func = self.callback_func

    def doPoll(self):
        self.read_value() # this includes read_status
        if self._violations:
            if self.relais.value or any(c.control_active for c in self._controllers) or self.htr.target:
                self.switch_off()

    def callback_func(self, query=False, reset=False):
        """fujnction to be called from controlling modules

        query = True: return violations
        reset = True: try to reset
        default: try to reset if autoreset is on
        """
        if not self.read_value():
            if not query:
                if self.__inside_switch_off:
                    return 'switch_off'
                if self.autoreset or reset:
                    if not self._violations and not reset:
                        self.log.warning('reset interlock')
                    self.reset()
                else:
                    if self._violations:
                        return self._violations
                    return f'need reset after {self._off_reason}'
        return self._violations

    def read_value(self):
        self.read_status()
        return self.value

    def write_target(self, value):
        if value:
            self.read_status()
            if self._violations:
                raise ImpossibleError(f'not ready to start: {self._violations}')
            self._off_reason = None
            self.value = True
        elif self.value and not value:
            self.switch_off()
            self._off_reason = 'switched off'
            self.value = False
        self.read_status()

    def switch_off(self):
        if self.value:
            self._off_reason = self._violations
            self.value = False
        self.__inside_switch_off = True
        try:
            for mod in self._controllers:
                if mod.control_active:
                    try:
                        mod.write_control_active(False)
                    except Exception as e:
                        self.log.warning('can not shut %s', mod.name)
        finally:
            del self.__inside_switch_off
        if self.htr and self.htr.target:
            self.log.info('switch heater off')
            self.htr.write_target(0)
        if self.relais.value or self.relais.target:
            self.log.warning('switch off relais %r %r', self.relais.value, self.relais.target)
        self.relais.write_target(False)

    def write_disabled_checks(self, value):
        disabled = set(value.split())
        self._sensor_checks = []
        for att, limitname in self.SENSOR_MAP.items():
            obj = getattr(self, att)
            if obj:
                self._sensor_checks.append((obj, limitname, obj.name in disabled))
        self._disable_flow_check = 'flow' in disabled

    def get_violations(self):
        violations = {False: [], True: []}
        if self.flowswitch and self.flowswitch.value == 0:
            violations[self._disable_flow_check].append('no cooling water')
        for sensor, limitname, disabled in self._sensor_checks:
            if sensor.status[0] >= ERROR:
                violations[disabled].append(f'error at {sensor.name}: {sensor.status[1]}')
                continue
            if sensor.value > getattr(self, limitname):
                violations[disabled].append(f'above {sensor.name} limit')
                continue
        self._violations = ', '.join(violations[False])
        self._all_violations = ', '.join(violations[False] + violations[True])
        return violations[False]

    def read_status(self):
        self.get_violations()
        if self.value:
            return IDLE, self._all_violations
        return ERROR, self._off_reason

    @Command
    def reset(self):
        """reset interlock after error

        will fail if error conditions still apply
        """
        self.write_target(1)


class PI(HasConvergence, PImixin, Writable):
    input_module = Attached(Readable, 'the input module')
    callback_func = None

    def read_value(self):
        return self.input_module.value

    def read_status(self):
        if not self._interlock:
            return super().read_status()
        violations = self._interlock.interlock_callback(query=True)
        status = (ERROR, violations) if violations else super().read_status()
        return merge_status(status, self.input_module.status)

    def write_target(self, target):
        zero = self.parameters['target'].datatype.default
        if target == zero:
            target = zero
        elif self.callback_func:
            errtxt = self.callback_func()
            if errtxt == 'switch_off':
                target = zero
            elif errtxt:
                msg = f'can not set target: {errtxt}'
                self.log.warning(msg)
                raise ImpossibleError(msg)
        return super().write_target(target)

    @Command
    def reset(self):
        """reset interlock after error"""
        if self.callback_func:
            self.callback_func(reset=True)
        else:
            raise ConfigError('reset is not configured')


class PIctrl(WrapControlledBy, PI):
    pass


class PI2(PI):
    maxovershoot = Parameter('max. overshoot', FloatRange(0, 100, unit='%'), readonly=False, default=20)

    def doPoll(self):
        maxdif = 0.01 * self.maxovershoot * self.target
        self.output_max = self.target + maxdif
        self.output_min = self.target - maxdif
        if self.control_active:
            super().doPoll()

    def write_target(self, target):
        if not self.control_active:
            # guess start value based on current T difference
            maxdif = 0.01 * self.maxovershoot * target
            self.output_module.target = target + clamp(
                -maxdif, maxdif, self.output_module.value - self.input_module.value)
        super().write_target(target)


class PRtransmitter(CurrentInput):
    rawrange = (0.004, 0.02)
    extendedrange = (0.0036, 0.021)


class PKRgauge(LogVoltageInput):
    rawrange = (1.82, 8.6)
    valuerange = (5e-9, 1000)
    extendedrange = (0.5, 9.5)
    value = Parameter(unit='mbar')
