# -*- coding: utf-8 -*-
#
# This file is part of the Simulatede2BbaSwitch project
#
# Elettra - Sincrotrone Trieste S.C.p.A.
#
# Distributed under the terms of the GPL license.
# See LICENSE.txt for more info.

"""
Simulatede2BbaSwitch

Simple boolen r/w variable.
The real thing will be a boolen attribute expoter by the Tango MPS interface
"""

# PROTECTED REGION ID(Simulatede2BbaSwitch.system_imports) ENABLED START #
# PROTECTED REGION END #    //  Simulatede2BbaSwitch.system_imports

# PyTango imports
import tango
from tango import DebugIt
from tango.server import run
from tango.server import Device
from tango.server import attribute, command
from tango.server import device_property
from tango import AttrQuality, DispLevel, DevState
from tango import AttrWriteType, PipeWriteType
# Additional import
# PROTECTED REGION ID(Simulatede2BbaSwitch.additionnal_import) ENABLED START #
# PROTECTED REGION END #    //  Simulatede2BbaSwitch.additionnal_import

__all__ = ["Simulatede2BbaSwitch", "main"]


class Simulatede2BbaSwitch(Device):
    """
    Simple boolen r/w variable.
    The real thing will be a boolen attribute expoter by the Tango MPS interface

    **Properties:**

    - Device Property
        MagnetNames
            - name of magnets with a BBA switch
            - Type:'DevVarStringArray'
    """
    # PROTECTED REGION ID(Simulatede2BbaSwitch.class_variable) ENABLED START #
    def add_attr(self, name):
        attr = tango.Attr(name, tango.DevBoolean, tango.READ_WRITE)
        self.add_attribute(attr, self.read_general, self.write_general)
        self._dynamic_attrs[name] = False
        dev_m_attr = self.get_device_attr()
        wattr = dev_m_attr.get_w_attr_by_name(name)
        wattr.set_write_value(False)
        
    def read_general(self, attr):
        name = attr.get_name()
        self.debug_stream("Reading attribute %s", name)
        attr.set_value(self._dynamic_attrs[name])
        
    def write_general(self, attr):
        name = attr.get_name()
        data = attr.get_write_value()
        self._dynamic_attrs[name] = data
        self.debug_stream("Writing into attribute %s the value %s", name, data)
    # PROTECTED REGION END #    //  Simulatede2BbaSwitch.class_variable

    # -----------------
    # Device Properties
    # -----------------

    MagnetNames = device_property(
        dtype='DevVarStringArray',
        doc="name of magnets with a BBA switch",
        mandatory=True
    )

    # ----------
    # Attributes
    # ----------

    generic = attribute(
        dtype='DevBoolean',
        access=AttrWriteType.READ_WRITE,
        label="bba enable",
        doc="true if enabled",
    )

    # ---------------
    # General methods
    # ---------------

    def init_device(self):
        """Initializes the attributes and properties of the Simulatede2BbaSwitch."""
        Device.init_device(self)
        self._generic = False
        # PROTECTED REGION ID(Simulatede2BbaSwitch.init_device) ENABLED START #
        dev_m_attr = self.get_device_attr()
        wattr = dev_m_attr.get_w_attr_by_name("generic")
        wattr.set_write_value(False)
        self.set_state(tango.DevState.ON)
        self.set_status("MPS BBA switches ready")
        
        
        
        # add dyn attributes
        self._dynamic_attrs = {}
        for magname in self.MagnetNames:
            self.add_attr(magname)
        # PROTECTED REGION END #    //  Simulatede2BbaSwitch.init_device

    def always_executed_hook(self):
        """Method always executed before any TANGO command is executed."""
        # PROTECTED REGION ID(Simulatede2BbaSwitch.always_executed_hook) ENABLED START #
        # PROTECTED REGION END #    //  Simulatede2BbaSwitch.always_executed_hook

    def delete_device(self):
        """Hook to delete resources allocated in init_device.

        This method allows for any memory or other resources allocated in the
        init_device method to be released.  This method is called by the device
        destructor and by the device Init command.
        """
        # PROTECTED REGION ID(Simulatede2BbaSwitch.delete_device) ENABLED START #
        # PROTECTED REGION END #    //  Simulatede2BbaSwitch.delete_device

    # ------------------
    # Attributes methods
    # ------------------

    def read_generic(self):
        # PROTECTED REGION ID(Simulatede2BbaSwitch.generic_read) ENABLED START #
        """Return the generic attribute."""
        return self._generic
        # PROTECTED REGION END #    //  Simulatede2BbaSwitch.generic_read
    def write_generic(self, value):
        # PROTECTED REGION ID(Simulatede2BbaSwitch.generic_write) ENABLED START #
        """Set the generic attribute."""
        pass
        # PROTECTED REGION END #    //  Simulatede2BbaSwitch.generic_write
    # --------
    # Commands
    # --------


# ----------
# Run server
# ----------

# PROTECTED REGION ID(Simulatede2BbaSwitch.custom_code) ENABLED START #
# PROTECTED REGION END #    //  Simulatede2BbaSwitch.custom_code


def main(args=None, **kwargs):
    """Main function of the Simulatede2BbaSwitch module."""
    # PROTECTED REGION ID(Simulatede2BbaSwitch.main) ENABLED START #
    return run((Simulatede2BbaSwitch,), args=args, **kwargs)
    # PROTECTED REGION END #    //  Simulatede2BbaSwitch.main

# PROTECTED REGION ID(Simulatede2BbaSwitch.custom_functions) ENABLED START #
# PROTECTED REGION END #    //  Simulatede2BbaSwitch.custom_functions


if __name__ == '__main__':
    main()