from lnx.logicnode.lnx_nodes import *


class LeenkxCloseConnectionNode(LnxLogicTreeNode):
    """Close connection on the network"""
    bl_idname = 'LNLeenkxCloseConnectionNode'
    bl_label = 'Close Leenkx Connection'
    lnx_version = 1
    lnx_category = 'Leenkx'
    ind = 2

    @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 {
            'client': 0,
            'peer': 1
        }.get(type_name, 0)

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

    def set_enum(self, value):
        # Checking the selection of each type
        select_current = self.get_enum_id_value(self, 'property0', value)
        select_prev = self.property0

        #Check if type changed
        if select_prev != select_current:

            for i in self.inputs:
                self.inputs.remove(i)

            # Arguements for type Host
            if (self.get_count_in(select_current) == 0):
                    self.add_input('LnxNodeSocketAction', 'In')
                    self.add_input('LnxDynamicSocket', 'Connection')
                    self.add_input('LnxStringSocket', 'Url')

            # Arguements for type Client
            if (self.get_count_in(select_current) == 1):
                    self.add_input('LnxNodeSocketAction', 'In')
                    self.add_input('LnxDynamicSocket', 'Connection')
                    self.add_input('LnxStringSocket', 'ID')

        self['property0'] = value


    property0: HaxeEnumProperty(
        'property1',
        items = [('client', 'Leenkx Client', 'Close the Leenkx Client connection on the network'),
                ('peer', 'Single Peer', 'Close a Peer connection on network')],
        name='', default='client',
        set=set_enum, 
        get=get_enum)


    def lnx_init(self, context):
        self.add_input('LnxNodeSocketAction', 'In')
        self.add_input('LnxDynamicSocket', 'Connection')
        self.add_input('LnxStringSocket', 'Url')

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


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

def register():
    add_category('Leenkx', icon='ORIENTATION_CURSOR')
    LeenkxCloseConnectionNode.on_register()