from PyQt6.QtCore import Qt
from PyQt6.QtWidgets import (
    QCheckBox,
    QHBoxLayout,
    QLabel,
    QLineEdit,
    QSlider,
    QPushButton,
    QWidget,
    QVBoxLayout,
)


def create_slider_setting(
    name: str, setting_key: str, default_value: int, parent_widget=None
):
    """Helper function to create a slider setting with value label and reset button"""
    layout = QHBoxLayout()
    layout.setSpacing(8)

    label = QLabel(f"{name}:")
    label.setAlignment(Qt.AlignmentFlag.AlignVCenter)
    label.setMinimumWidth(130)

    slider = QSlider(Qt.Orientation.Horizontal)
    slider.setRange(0, 100)
    slider.setTickPosition(QSlider.TickPosition.TicksBothSides)

    value_label = QLabel(f"{default_value}%")
    value_label.setMinimumWidth(45)
    value_label.setAlignment(Qt.AlignmentFlag.AlignCenter)

    reset_button = QPushButton("Reset")
    reset_button.setFixedHeight(25)
    reset_button.setFixedWidth(60)
    reset_button.clicked.connect(lambda: slider.setValue(default_value))

    if parent_widget:
        current_value = parent_widget.settings.value(
            setting_key, default_value, type=int
        )
        slider.setValue(current_value)
        value_label.setText(f"{current_value}%")

        def update_label(value):
            value_label.setText(f"{value}%")

        slider.valueChanged.connect(update_label)

    layout.addWidget(label)
    layout.addWidget(slider, 1)
    layout.addWidget(value_label)
    layout.addWidget(reset_button)

    return layout, slider, value_label, reset_button


class CheckboxSetting(QWidget):
    def __init__(
        self,
        text: str,
        setting_key: str,
        default_value: bool,
        parent_widget=None,
        tooltip: str | None = None,
    ):
        super().__init__()
        self._layout = QVBoxLayout(self)
        self._layout.setContentsMargins(0, 0, 0, 0)
        self.checkbox = QCheckBox(text)

        if parent_widget:
            current_value = parent_widget.settings.value(
                setting_key, default_value, type=bool
            )
            self.checkbox.setChecked(current_value)

        if tooltip:
            self.checkbox.setToolTip(tooltip)
            self.explanation_label = QLabel(tooltip)
            self.explanation_label.setStyleSheet("color: #888888; font-size: 11px;")
            self.explanation_label.setWordWrap(True)
            self._layout.addWidget(self.checkbox)
            ex_layout = QHBoxLayout()
            ex_layout.setContentsMargins(0, 0, 0, 0)
            ex_layout.addSpacing(14)
            ex_layout.addWidget(self.explanation_label)
            self._layout.addLayout(ex_layout)
        else:
            self.explanation_label = None
            self._layout.addWidget(self.checkbox)

    def isChecked(self):
        return self.checkbox.isChecked()

    def setChecked(self, value: bool):
        return self.checkbox.setChecked(value)

    @property
    def stateChanged(self):
        return self.checkbox.stateChanged


def create_checkbox_setting(
    text: str, setting_key: str, default_value: bool, parent_widget=None, tooltip=None
):
    """Helper function to create a checkbox setting (returns a CheckboxSetting widget)"""
    return CheckboxSetting(text, setting_key, default_value, parent_widget, tooltip)
