"""
Handles all chdman command execution and parameter processing
"""

from PySide6.QtWidgets import (
    QLineEdit,
    QComboBox,
    QSpinBox,
    QCheckBox,
    QFileDialog,
    QMessageBox,
)
from pathlib import Path
import os
from typing import Dict, Any


class CommandHandler:
    def __init__(self, parent):
        self.parent = parent
        self.param_widgets: Dict[str, Any] = {}

    def add_file_param(
        self, name: str, label: str, required: bool = False, is_save: bool = False
    ):
        """Add a file selection parameter to the form"""
        layout = QHBoxLayout()
        line_edit = QLineEdit()
        browse_button = QPushButton("Browse...")

        def browse():
            current_path = line_edit.text()
            start_dir = (
                os.path.dirname(current_path) if current_path else str(Path.home())
            )

            if is_save:
                file_path, _ = QFileDialog.getSaveFileName(
                    self.parent, f"Select {label}", start_dir
                )
            else:
                file_path, _ = QFileDialog.getOpenFileName(
                    self.parent, f"Select {label}", start_dir
                )

            if file_path:
                line_edit.setText(file_path)
                if name == "input" and not self.parent.bulk_checkbox.isChecked():
                    self.update_output_path(file_path)

        browse_button.clicked.connect(browse)
        layout.addWidget(line_edit)
        layout.addWidget(browse_button)
        self.parent.params_layout.addRow(f"{label}{'*' if required else ''}:", layout)
        self.param_widgets[name] = line_edit

    # Additional command-related methods will be moved here from app.py
