# *****************************************************************************
# 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>
#   Anik Stark <anik.stark@psi.ch>
# *****************************************************************************

from frappy.core import StringIO, HasIO, Writable, Parameter, Property, FloatRange, IntRange, BoolType
from frappy.errors import CommunicationFailedError, HardwareError
from frappy.ctrlby import WrapControlledBy


class IO(StringIO):
    end_of_line = '\n'
    identification = [('ID?', '0\t.*')]  # senden: ID?<lf>, Antwort: 0<ht> str version information<lf>


class Heater(HasIO, Writable):

    ioClass = IO    # define IO class for automatic creation of the IO module

    channel = Property('channel (source number)', IntRange(1, 3))
    value = Parameter('current reading', FloatRange(0, 0.1, unit='A'))
    target = Parameter('current target value', FloatRange(0, 0.1, unit='A'), readonly=False)
    on = Parameter('turn current on/off', BoolType(), readonly=False, default=False)

    def query_status(self):
        reply, txtvalue = self.communicate('STATUS?').split('\t')
        if reply != '0':
            raise CommunicationFailedError(f'Bad reply: {reply}')
        return txtvalue.split(',')

    def write_on(self, value):
        for _ in range(2):
            txtvalue = self.query_status()
            on_idx = (self.channel - 1) * 4 + 2
            if txtvalue[on_idx + 1] == str(int(value)):
                break
            setup = ['0'] * 12
            setup[on_idx] = '1'  # toggle
            answer = ','.join(setup)
            reply = self.communicate(f'SETUP {answer}')
            if reply != '0':
                raise CommunicationFailedError(f'Bad reply: {reply}')
        else:
            raise HardwareError('unable to turn device on/off')

    def write_target(self, target):
        reply = self.communicate(f'SETDAC {self.channel} 0 {int(target * 1e6)}')  # 0: autorange
        if reply != '0':  # not as in manual
            raise CommunicationFailedError(f'Bad reply: {reply!r}')

    def read_value(self):
        txtvalue = self.query_status()
        current_range = txtvalue[(self.channel - 1) * 4 + 1]
        current = txtvalue[(self.channel - 1) * 4 + 1 + 1]   # percent of range
        multipliers = {'1': 1e-4, '2': 1e-3, '3': 1e-2, '4': 1e-1}
        value = float(current) / 100 * float(multipliers[current_range])
        return value


class WrappedHeater(WrapControlledBy, Heater):
    pass
