# Path: aisikl/components/control.py
# class Control(Component):
#     def __init__(self, dialog, id, type, parent_id, properties, element):
#         super().__init__(dialog, id, type, parent_id, properties, element)
#         self.parent_id = parent_id
#         self.read_only = properties.get('ro', False)
#         self.tab_order = int(element.get('tabindex', '0'))
#         self.tool_tip_text = element.get('title')
# 
#     @property
#     def parent(self):
#         return self.dialog.components[self.parent_id]
# 
#     def is_really_enabled(self):
#         return super().is_really_enabled() and self.parent.is_really_enabled()
# 
#     def _ais_setReadOnly(self, value):
#         self.read_only = is_true(value)
#     def _ais_setTabOrder(self, value):
#         self.tab_order = int(value)
#     def _ais_setToolTipText(self, value):
#         self.tool_tip_text = value
# 
#     def _ais_setX(self, value): pass
#     def _ais_setY(self, value): pass
#     def _ais_setWidth(self, value): pass
#     def _ais_setHeight(self, value): pass
#     def _ais_setForeground(self, value): pass
#     def _ais_setBackground(self, value): pass
#     def _ais_setComponentStyle(self, value): pass
#     def _ais_setFontSize(self, value): pass
#     def _ais_setFontStyle(self, value): pass
# 
#     def changed_properties(self):
#         return ''
# 
#     def _build_changed_properties(self, **kwargs):
#         # TODO: This could be shorter.
#         result = []
#         write = result.append
# 
#         write("<changedProperties>")
#         write("<objName>" + self.id + "</objName>")
#         write("<propertyValues>")
# 
#         for name, value in kwargs.items():
#             is_xml = use_cdata = False
#             if isinstance(value, tuple): is_xml, use_cdata, value = value
#             if isinstance(value, bool): value = 'true' if value else 'false'
# 
#             if use_cdata: value = "<![CDATA[" + value + "]]>"
#             write("<nameValue>")
#             write("<name>" + name + "</name>")
#             if is_xml is not None:
#                 write("<isXml>" + str(bool(is_xml)).lower() + "</isXml>")
#             write("<value>" + value + "</value>")
#             write("</nameValue>")
# 
#         write("</propertyValues>")
#         write("<embObjChProps isNull='true'/>")
#         write("</changedProperties>")
# 
#         return ''.join(result)
# 
#     def update_value_interactives(self, type):
#         pass # TODO
# 
# Path: aisikl/events.py
# def selection_event(source, index=-1, selected=True):
#     source.dialog.try_interactive(source, 'SelectionEvent')
#     return Event(source, EVENT_SELCHANGE,
#         "<event class='avc.ui.event.AVCSelectionActionEvent'>" +
#         _tag("command", "SELECT" if selected else "UNSELECT") +
#         _tag("index", str(index)) +
#         "</event>")
# 
# Path: aisikl/exceptions.py
# class AISParseError(AisiklError):
#     '''We couldn't parse or properly process the response from AIS.
# 
#     This isn't just for syntax errors, but also for unknown or unsupported
#     attribute values, method names and such. The error can either mean:
# 
#     - AIS does something we doesn't support yet, or
#     - AIS changes their formats and our parser needs to be updated, or
#     - the AIS server returned an error with status 200 (should never happen).
#     '''


from collections import namedtuple
from .control import Control
from aisikl.events import selection_event
from aisikl.exceptions import AISParseError

    @property
    def selected_option():
        if self.selected_index == -1: return None
        return self.options[self.selected_index]

    # If self.always_selected == False, select(-1) unselects the current item.
    # (Votr always allows select(-1), but AIS might not like it.)
    def select(self, index):
        self.log('action', 'Selecting "{}" in {}'.format(
            self.options[index].title, self.id))
        self.selected_index = index
        self._fire_event()

    def _fire_event(self):
        self.dialog.component_changes(self, False)
        ev = selection_event(self, self.selected_index)
        self.dialog.app.send_events(ev)

    def changed_properties(self):
        index = '' if self.selected_index == -1 else self.selected_index
        cdata = ('<root><selection>'
            '<selectedIndexes>{}</selectedIndexes>'
            '</selection></root>').format(self.selected_index)
        return self._build_changed_properties(dataView=(True, True, cdata))

    def _ais_setDataView(self, id, body):
        data_view = body.find(id=id)
        container = data_view.find(id='radioBox_container', recursive=False)
        if not container:

# Path: aisikl/components/control.py
# class Control(Component):
#     def __init__(self, dialog, id, type, parent_id, properties, element):
#         super().__init__(dialog, id, type, parent_id, properties, element)
#         self.parent_id = parent_id
#         self.read_only = properties.get('ro', False)
#         self.tab_order = int(element.get('tabindex', '0'))
#         self.tool_tip_text = element.get('title')
# 
#     @property
#     def parent(self):
#         return self.dialog.components[self.parent_id]
# 
#     def is_really_enabled(self):
#         return super().is_really_enabled() and self.parent.is_really_enabled()
# 
#     def _ais_setReadOnly(self, value):
#         self.read_only = is_true(value)
#     def _ais_setTabOrder(self, value):
#         self.tab_order = int(value)
#     def _ais_setToolTipText(self, value):
#         self.tool_tip_text = value
# 
#     def _ais_setX(self, value): pass
#     def _ais_setY(self, value): pass
#     def _ais_setWidth(self, value): pass
#     def _ais_setHeight(self, value): pass
#     def _ais_setForeground(self, value): pass
#     def _ais_setBackground(self, value): pass
#     def _ais_setComponentStyle(self, value): pass
#     def _ais_setFontSize(self, value): pass
#     def _ais_setFontStyle(self, value): pass
# 
#     def changed_properties(self):
#         return ''
# 
#     def _build_changed_properties(self, **kwargs):
#         # TODO: This could be shorter.
#         result = []
#         write = result.append
# 
#         write("<changedProperties>")
#         write("<objName>" + self.id + "</objName>")
#         write("<propertyValues>")
# 
#         for name, value in kwargs.items():
#             is_xml = use_cdata = False
#             if isinstance(value, tuple): is_xml, use_cdata, value = value
#             if isinstance(value, bool): value = 'true' if value else 'false'
# 
#             if use_cdata: value = "<![CDATA[" + value + "]]>"
#             write("<nameValue>")
#             write("<name>" + name + "</name>")
#             if is_xml is not None:
#                 write("<isXml>" + str(bool(is_xml)).lower() + "</isXml>")
#             write("<value>" + value + "</value>")
#             write("</nameValue>")
# 
#         write("</propertyValues>")
#         write("<embObjChProps isNull='true'/>")
#         write("</changedProperties>")
# 
#         return ''.join(result)
# 
#     def update_value_interactives(self, type):
#         pass # TODO
# 
# Path: aisikl/events.py
# def selection_event(source, index=-1, selected=True):
#     source.dialog.try_interactive(source, 'SelectionEvent')
#     return Event(source, EVENT_SELCHANGE,
#         "<event class='avc.ui.event.AVCSelectionActionEvent'>" +
#         _tag("command", "SELECT" if selected else "UNSELECT") +
#         _tag("index", str(index)) +
#         "</event>")
# 
# Path: aisikl/exceptions.py
# class AISParseError(AisiklError):
#     '''We couldn't parse or properly process the response from AIS.
# 
#     This isn't just for syntax errors, but also for unknown or unsupported
#     attribute values, method names and such. The error can either mean:
# 
#     - AIS does something we doesn't support yet, or
#     - AIS changes their formats and our parser needs to be updated, or
#     - the AIS server returned an error with status 200 (should never happen).
#     '''


# Path: aisikl/components/radiobox.py
from collections import namedtuple
from .control import Control
from aisikl.events import selection_event
from aisikl.exceptions import AISParseError


    @property
    def selected_option():
        if self.selected_index == -1: return None
        return self.options[self.selected_index]

    # If self.always_selected == False, select(-1) unselects the current item.
    # (Votr always allows select(-1), but AIS might not like it.)
    def select(self, index):
        self.log('action', 'Selecting "{}" in {}'.format(
            self.options[index].title, self.id))
        self.selected_index = index
        self._fire_event()

    def _fire_event(self):
        self.dialog.component_changes(self, False)
        ev = selection_event(self, self.selected_index)
        self.dialog.app.send_events(ev)

    def changed_properties(self):
        index = '' if self.selected_index == -1 else self.selected_index
        cdata = ('<root><selection>'
            '<selectedIndexes>{}</selectedIndexes>'
            '</selection></root>').format(self.selected_index)
        return self._build_changed_properties(dataView=(True, True, cdata))

    def _ais_setDataView(self, id, body):
        data_view = body.find(id=id)
        container = data_view.find(id='radioBox_container', recursive=False)
        if not container:

            raise AISParseError("Expected radioBox_container")