# *****************************************************************************
# 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>
# *****************************************************************************
"""generic 3D vector"""

from frappy.core import Attached, Drivable, Readable, Parameter, Done
from frappy.datatypes import FloatRange, TupleOf, StatusType, Enum


class VectorRd(Readable):
    """generic readable vector"""
    value = Parameter(datatype=TupleOf(FloatRange(), FloatRange(), FloatRange()))
    x = Attached()
    y = Attached()
    z = Attached()
    pollFuncs = None
    components = None

    def initModule(self):
        super().initModule()
        members = []
        status_codes = {}  # collect all possible status codes
        components = []
        for name in 'xyz':
            component = getattr(self, name)
            members.append(component.parameters['value'].datatype.copy())
            components.append(component)
            for code in component.status[0].enum.members:
                status_codes[int(code)] = code.name
        self.parameters['value'].datatype = TupleOf(*members)
        self.parameters['status'].datatype = StatusType(Enum(
            'status', **{k: v for v, k in status_codes.items()}))
        self.components = components

    def doPoll(self):
        for component in self.components:
            component.doPoll()
            # update
            component.pollInfo.last_main = self.pollInfo.last_main
        self.value = self.merge_value()
        self.status = self.merge_status()

    def merge_value(self):
        return [c.value for c in self.components]

    def merge_status(self):
        status = -1, ''
        for c in self.components:
            if c.status[0] > status[0]:
                status = c.status
        return status

    def read_value(self):
        return tuple((c.read_value() for c in self.components))

    def read_status(self):
        [c.read_status() for c in self.components]
        return self.merge_status()


class Vector(Drivable, VectorRd):
    """generic drivable vector"""
    target = Parameter(datatype=TupleOf(FloatRange(), FloatRange(), FloatRange()))

    def initModule(self):
        super().initModule()
        members = []
        for component in self.components:
            members.append(component.parameters['target'].datatype.copy())
        self.parameters['target'].datatype = TupleOf(*members)

    def write_target(self, value):
        return tuple((c.write_target(v) for v, c in zip(value, self.components)))
