# -*- coding: utf-8 -*-
import tkinter as tk
from typing import Optional, Any

from pymsc.gui.command_registry import (
    CHECKBOXES, COMBOBOXES, SPINBOXES, LABELS, LIST_CONTROLS
)
from pymsc.gui.components.command_widgets import (
    CommandFrameCheckBox, CommandFrameComboBox, CommandFrameSpinBox,
    CommandFrameLabels, CommandFrameControls
)

def create_widget_by_id(parent: tk.Widget, command_id: str) -> Optional[tk.Frame]:
    """
    Factory function to instantiate the correct widget based on the command ID.
    Searches through all registry categories.
    """
    # 1. Search in Checkboxes
    for item in CHECKBOXES:
        if item["command"] == command_id:
            return CommandFrameCheckBox(parent, item)

    # 2. Search in ComboBoxes
    for item in COMBOBOXES:
        if item["command"] == command_id:
            return CommandFrameComboBox(parent, item)

    # 3. Search in SpinBoxes
    for item in SPINBOXES:
        if item["command"] == command_id:
            return CommandFrameSpinBox(parent, item)

    # 4. Search in Labels (Read-only)
    for item in LABELS:
        if item["command"] == command_id:
            return CommandFrameLabels(parent, item)

    # 5. Search in List Controls (Flexible)
    for item in LIST_CONTROLS:
        if item["command"] == command_id:
            return CommandFrameControls(parent, item)

    print(f"Widget Factory Error: Command ID '{command_id}' not found in registry.")
    return None