from lrm.exceptions import lrm_slot
import json
from ButtonsHandler import Buttons


class ComboboxHandler:
    def __init__(self, parent):
        self.parent = parent
        self.buttons = Buttons(self.parent, self)
        self.unit_combo_box = self.parent.unit_combo_box
        self.create_unit_combobox()
        self.buttons.new_screen_button()
        self.buttons.load_screen_button()
        self.buttons.generate_code_button()

    def config_loader(self):
        with open("configs/units.json", "r") as config:
            return json.load(config)

    def unit_finder(self):
        units = []
        for key, value in self.config_loader().items():
            units.append(key)
        
        return units

    @lrm_slot
    def create_unit_combobox(self):
        self.unit_combo_box.clear()
        self.unit_combo_box.addItems(self.unit_finder())
        self.unit_combo_box.setCurrentIndex(-1)
        self.unit_combo_box.currentIndexChanged.connect(self.buttons.new_screen_button)
        self.unit_combo_box.currentIndexChanged.connect(self.buttons.load_screen_button)
