from pathlib import Path
from typing import List

from PyQt6.QtCore import Qt
from PyQt6.QtWidgets import (
    QCheckBox,
    QComboBox,
    QHBoxLayout,
    QLabel,
    QPushButton,
    QWidget,
)

from app.data_structures.processing import Configurations

from .base import DialogWindow


class ConfigurationWindow(DialogWindow):
    """
    Window for choosing configurations for processing (cutting margin, object detection).
    """

    def __init__(self, file_paths: List[Path]) -> None:
        super().__init__((500, 200), title="Konfigurace")
        self.file_paths = file_paths
        self._add_export_configurations()
        self._add_button_next()

    def _add_export_configurations(self) -> None:
        configurations = QWidget()
        layout = QHBoxLayout(configurations)
        self.cut_margin_select = QComboBox()
        self.cut_margin_select.addItems(("0", "5", "10", "15"))
        self.object_detect_cb = QCheckBox()
        layout.addWidget(
            self._get_labeled_widget(
                "Přidat čas před/po události (s)", self.cut_margin_select
            )
        )
        layout.addWidget(
            self._get_labeled_widget("Detekce objektů", self.object_detect_cb)
        )
        self.general_layout.addWidget(configurations)

    def _add_button_next(self) -> None:
        control_widget = QWidget()
        layout = QHBoxLayout(control_widget)
        self.next_btn = QPushButton("Analyzovat")
        self.next_btn.clicked.connect(self._get_configurations)
        layout.setAlignment(Qt.AlignmentFlag.AlignCenter)
        layout.addWidget(self.next_btn)
        self.general_layout.addWidget(control_widget)

    def _get_configurations(self) -> None:
        self.configurations = Configurations(
            cut_margin=int(self.cut_margin_select.currentText()),
            object_detection=self.object_detect_cb.isChecked(),
        )
        self.next_window()

    @staticmethod
    def _get_labeled_widget(label: str, widget: QWidget) -> QWidget:
        new_widget = QWidget()
        layout = QHBoxLayout(new_widget)
        layout.setAlignment(Qt.AlignmentFlag.AlignLeft)
        layout.addWidget(QLabel(label))
        layout.addWidget(widget)
        return new_widget
