import json
import os
from PyQt5.QtWidgets import QFrame, QVBoxLayout, QHBoxLayout, QLabel, QPushButton, QWidget
from PyQt5.QtSvg import QSvgWidget
from PyQt5.QtCore import Qt

from src.config import SECONDARY_COLOR, UXUI_ASSETS, BUTTON_STYLE, SCRIPT_CONFIG

def create_ai_toolbox(parent, config_utils, inference_controller):
    """Create the AI toolbox layout"""
    try:
        # Read JSON configuration
        print("config_path:", SCRIPT_CONFIG)
        if os.path.exists(SCRIPT_CONFIG):
            with open(SCRIPT_CONFIG, "r", encoding="utf-8") as f:
                config = json.load(f)
            plugins = config.get("plugins", [])
        else:
            # If no configuration file, try to generate it
            plugins = config_utils.generate_global_config().get("plugins", [])
            if not plugins:
                print("Unable to generate configuration, using empty tool list")
        
        # Create toolbox UI
        toolbox_frame = QFrame(parent)
        toolbox_frame.setStyleSheet(f"border: none; background: {SECONDARY_COLOR}; border-radius: 15px;")
        toolbox_frame.setFixedHeight(450)
        toolbox_frame.setFixedWidth(240)
        toolbox_layout = QVBoxLayout(toolbox_frame)
        
        # Title row
        title_layout = QHBoxLayout()
        title_container = QWidget()
        container_layout = QHBoxLayout(title_container)
        container_layout.setSpacing(10)
        
        toolbox_icon = QSvgWidget(os.path.join(UXUI_ASSETS, "Assets_svg/ic_window_toolbox.svg"))
        toolbox_icon.setFixedSize(40, 40)
        container_layout.addWidget(toolbox_icon)
        
        title_label = QLabel("AI Toolbox")
        title_label.setStyleSheet("color: white; font-size: 20px; font-weight: bold;")
        container_layout.addWidget(title_label)
        
        title_layout.addWidget(title_container)
        toolbox_layout.addLayout(title_layout)
        
        # Create tool buttons (categorized)
        for plugin in plugins:
            mode = plugin.get("mode", "")
            display_name = plugin.get("display_name", "")
            
            # Add category title
            category_label = QLabel(display_name)
            category_label.setStyleSheet("color: white; font-size: 16px; font-weight: bold; margin-top: 10px;")
            toolbox_layout.addWidget(category_label)
            
            # Add all model buttons in this category
            for model in plugin.get("models", []):
                model_name = model.get("name", "")
                display_name = model.get("display_name", "")
                
                # Create tool configuration
                tool_config = {
                    "mode": mode,
                    "model_name": model_name,
                    "display_name": display_name,
                    "description": model.get("description", ""),
                    "compatible_devices": model.get("compatible_devices", [])
                }
                
                # Create button
                button = QPushButton(display_name)
                button.clicked.connect(lambda checked, t=tool_config: inference_controller.select_tool(t))
                button.setStyleSheet(BUTTON_STYLE)
                button.setFixedHeight(40)
                toolbox_layout.addWidget(button)
        
        return toolbox_frame
    except Exception as e:
        print(f"Error in create_ai_toolbox: {e}")
        return QFrame(parent)