from glob import iglob

from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QPushButton
from ui.crud import Cell, Combo_Box_Cell_Widget, Crud
from ui.dialog import Dialog
from ui.leak_step_editor import Leak_Step_Editor
from ui.vision_step_editor import Vision_Step_Editor
from ui.widget import Widget


class Step_Spec_JEDECW(QPushButton, Cell):
    def __init__(self, action=None, readonly=True, autocomplete=None, field_name=None, field_alias=None, field=None, row_number=None, crud=None):
        self.editors = {
            "vision": Vision_Step_Editor(cell_widget=self),
            "leak_1": Leak_Step_Editor(cell_widget=self),
            "leak_2": Leak_Step_Editor(cell_widget=self),
        }
        self.editor = None
        self.editor_type = None
        self.dialog = Dialog()
        self.dialog.setAttribute(Qt.WA_DeleteOnClose, on=False)
        super().__init__("\u238B modifica")
        Cell.__init__(self, action=action, readonly=readonly, autocomplete=autocomplete, field_name=field_name, field_alias=field_alias, field=field, row_number=row_number, crud=crud)
        self.update_editor()
        self.crud().modified.connect(self.update_editor)
        self.dialog.setWindowTitle(self.field_alias)
        self.clicked.connect(self.dialog.show)

    def update_editor(self):
        self.editor_type = self.crud().db_tw.cellWidget(self.row_number, self.crud().select_index["type"])._parse()
        self.editor = self.editors[self.editor_type]
        if self.dialog.centralWidget is not self.editor:
            self.dialog.setCentralWidget(self.editor)
            self.dialog.adjustSize()

    def set_readonly(self, readonly=True):
        for editor in self.editors.values():
            editor.set_readonly(readonly=readonly)

    def do_autocomplete(self, autocomplete):
        for editor_type, editor in self.editors.items():
            if autocomplete is None:
                editor.do_autocomplete(None)
            elif editor_type in autocomplete:
                editor.do_autocomplete(autocomplete[editor_type])

    def connect_modified(self):
        for editor in self.editors.values():
            editor.connect_modified(self.check_modified)

    def render(self, data, field_name=None, row_number=None, crud=None):
        self.editor.render(data, field_name=field_name, row_number=row_number, crud=crud)

    def parse(self, action=None, row_number=None, crud=None):
        return self.editor.parse(row_number=row_number, crud=crud)


class Steps_Management(Widget):
    def __init__(self):
        super().__init__()
        crud_aliases = {
            # "id": "Id",
            "name": "Fase di test",
            "type": "Tipo",
            "spec": "Specifica",
            "description": "Descrizione",
            "archived": "Archiviata",
        }
        self.crud = Crud(
            "steps",
            display_name="GESTIONE FASI DI TEST",
            # readonly=["id"],
            readonly=False,
            select=list(crud_aliases.keys()),
            fields_aliases=crud_aliases,
            autocomplete={
                "type": [
                    "leak_1",
                    "leak_2",
                    "vision",
                ],
                "spec": {
                    "vision": {
                        # "recipe": iglob("*.ini", root_dir="./config/vision/recipes/"), # only in python3.10
                        "recipe": list(iglob("./config/vision/recipes/*.ini")),
                    },
                },
                "archived": False,
            },
            widget_classes={
                "type": Combo_Box_Cell_Widget,
                "spec": Step_Spec_JEDECW,
            },
        )
        self.layout().addWidget(self.crud, 0, 0, -1, -1)
