from device_conf import class_comm_device_config

class class_menu_layer_info() :
    def __init__(self, 
                 menu_acitve : dict = None, 
                 config: class_comm_device_config = None) :
        
        self.menu_display_start = 0
        self.menu_select        = 0 
        self.menu_active        = menu_acitve
        self.config             = config
        
        if menu_acitve == None:
            self.menu_item_count  = 0
        else :
            self.menu_item_count  = len(menu_acitve)

class class_device_menu() :
    def __init__(self, 
                 device_remap = 0,
                 device_config : class_comm_device_config = None):
        
        self.config = device_config
        self.menu_table = None if device_config == None else device_config.get_menu_top() 
        self.alias_table = None if device_config == None else device_config.get_alias_table()
        self.mqtt_table = None if device_config == None else device_config.get_mqtt_table()
        if device_config == None :
            self.comm_table = None
            self.func_comm_table_trigger_item = None
        else :  
           self.comm_table,  self.func_comm_table_trigger_item  = device_config.get_comm_table()
        
        self.device_remap = device_remap
        self.menu_layers : dict = []
        self.menu_layer_index = 0
        menu_layer_item = class_menu_layer_info(self.menu_table, self.config)
        self.menu_layers.append(menu_layer_item)

    #更新菜单配置信息, 当设备发送变化时，菜单也会同步发生变化
    def menu_update_device_config(self,  device_config : class_comm_device_config) :
        self.config = device_config
        if device_config == None :
            self.menu_table = None
            self.alias_table = None
            self.mqtt_table = None
            self.comm_table = None
            self.func_comm_table_trigger_item = None
        else :
            self.menu_table = device_config.get_menu_top()
            self.alias_table = device_config.get_alias_table()
            self.mqtt_table = device_config.get_mqtt_table()
            self.comm_table,  self.func_comm_table_trigger_item  = device_config.get_comm_table()

    #修改菜单remap地址
    def menu_update_device_remap_addr(self, device_remap) :
        self.device_remap = device_remap


    #获取菜单层级信息
    def menu_get_layer_info(self) :
        return self.menu_layers[self.menu_layer_index]
    
    #获取当前的活动菜单
    def menu_get_active_menu(self) :
        active_layer : class_menu_layer_info = self.menu_get_layer_info()
        menu_active = active_layer.menu_active
        return menu_active
    
    #获取选中的有效菜单相
    def menu_get_active_item(self) :
        active_layer : class_menu_layer_info = self.menu_get_layer_info()
        menu_active = active_layer.menu_active
        menu_item = None
        if menu_active != None :
            if active_layer.menu_select < len(menu_active) :
                menu_item = menu_active[active_layer.menu_select]
        return menu_item

    #返回上一层菜单
    def menu_return_to_upper_layer(self) :        
        if self.menu_layer_index > 0 :
            self.menu_layer_index -= 1
            self.menu_layers.pop(-1)

        active_layer : class_menu_layer_info = self.menu_get_layer_info()
        if active_layer.config != self.config :
            self.menu_update_device_config(active_layer.config)
        return active_layer
    
    #返回最上一层菜单
    def menu_return_to_most_upper_layer(self) :
        while self.menu_layer_index > 0 :
            self.menu_layer_index -= 1
            self.menu_layers.pop(-1)
    
        active_layer : class_menu_layer_info = self.menu_get_layer_info()
        if active_layer.config != self.config :
            self.menu_update_device_config(active_layer.config)
        return self.menu_get_layer_info()

    #进入下一层菜单
    def menu_enter_sub_menu(self, sub_menu) :
        self.menu_layer_index += 1
        menu_layers = class_menu_layer_info(sub_menu, self.config)
        self.menu_layers.append(menu_layers)
        return self.menu_get_layer_info()
