from lnx.logicnode.lnx_nodes import *


class SetElementDataNode(LnxLogicTreeNode):
    """Set Element Data"""
    bl_idname = 'LNSetElementDataNode'
    bl_label = 'Set Element Data'
    bl_description = 'Set Element Data'
    lnx_category = 'HTML'
    lnx_version = 1


    @staticmethod
    def get_enum_id_value(obj, prop_name, value):
        return obj.bl_rna.properties[prop_name].enum_items[value].identifier

    @staticmethod
    def get_count_in(type_name):
        return {
            'attribute': 0,
            'property': 1
        }.get(type_name, 0)

    def get_enum(self):
        return self.get('property0', 0)

    def set_enum(self, value):
        select_current = self.get_enum_id_value(self, 'property0', value)
        select_prev = self.property0

        if select_prev != select_current:
            for i in self.inputs:
                self.inputs.remove(i)

            if (self.get_count_in(select_current) == 0):            
                self.add_input('LnxNodeSocketAction', 'In')
                self.add_input('LnxDynamicSocket', 'Element')
                self.add_input('LnxStringSocket', 'Attribute')
                self.add_input('LnxDynamicSocket', 'Data')

            if (self.get_count_in(select_current) == 1):            
                self.add_input('LnxNodeSocketAction', 'In')
                self.add_input('LnxDynamicSocket', 'Element')
                self.add_input('LnxStringSocket', 'Property')
                self.add_input('LnxDynamicSocket', 'Data')

        self['property0'] = value


    property0: HaxeEnumProperty(
        'property0',
        items = [('attribute', 'Attribute', 'Set Element Attribute'),
                ('property', 'Property', 'Set Element Property')],
        name='', 
        default='attribute',
        set=set_enum, 
        get=get_enum)


    def __init__(self):
        array_nodes[str(id(self))] = self


    def init(self, context):
        self.add_input('LnxNodeSocketAction', 'In')
        self.add_input('LnxDynamicSocket', 'Element')
        self.add_input('LnxStringSocket', 'Attribute')
        self.add_input('LnxDynamicSocket', 'Data')

        self.add_output('LnxNodeSocketAction', 'Out')

    def draw_buttons(self, context, layout):
        layout.prop(self, 'property0')

def register():
    add_category('HTML', icon='SEQ_STRIP_META')
    SetElementDataNode.on_register()