from __future__ import annotations

from silx.gui import qt

from ewoks3dxrd.models import GaussianPeakSearchConfig
from ewoks3dxrd.models import SegmenterConfig

from ..common.utils import NoWheelDoubleSpinBox
from ..common.utils import NoWheelSpinBox
from .base_segmenter_algo_view import _BaseSegmenterAlgoView as BaseSegmenterAlgoView
from .constants import GAUSSIAN_SEGMENTER_DEFAULTS
from .constants import GAUSSIAN_SEGMENTER_TOOLTIPS


class GaussianPeakSearchParamView(BaseSegmenterAlgoView):
    def __init__(self, parent: qt.QWidget | None = None, **kwargs) -> None:
        super().__init__(parent=parent, **kwargs)
        seg_layout = qt.QFormLayout()
        self._threshold = NoWheelSpinBox(
            self,
        )
        self._threshold.setValue(GAUSSIAN_SEGMENTER_DEFAULTS["threshold"])

        self._smooth_sigma = NoWheelDoubleSpinBox(
            self,
        )
        self._smooth_sigma.setMinimum(0)
        self._smooth_sigma.setValue(GAUSSIAN_SEGMENTER_DEFAULTS["smooth_sigma"])

        self._bgc = NoWheelDoubleSpinBox()
        self._bgc.setMinimum(0)
        self._bgc.setValue(GAUSSIAN_SEGMENTER_DEFAULTS["bgc"])

        self._min_px = NoWheelSpinBox(
            self,
        )
        self._min_px.setValue(GAUSSIAN_SEGMENTER_DEFAULTS["min_px"])

        self._offset_threshold = NoWheelSpinBox(
            self,
        )
        self._offset_threshold.setValue(GAUSSIAN_SEGMENTER_DEFAULTS["offset_threshold"])

        self._ratio_threshold = NoWheelSpinBox(
            self,
        )
        self._ratio_threshold.setValue(GAUSSIAN_SEGMENTER_DEFAULTS["ratio_threshold"])

        self._threshold.setToolTip(GAUSSIAN_SEGMENTER_TOOLTIPS["threshold"])
        self._smooth_sigma.setToolTip(GAUSSIAN_SEGMENTER_TOOLTIPS["smooth_sigma"])
        self._bgc.setToolTip(GAUSSIAN_SEGMENTER_TOOLTIPS["bgc"])
        self._min_px.setToolTip(GAUSSIAN_SEGMENTER_TOOLTIPS["min_px"])
        self._offset_threshold.setToolTip(
            GAUSSIAN_SEGMENTER_TOOLTIPS["offset_threshold"]
        )
        self._ratio_threshold.setToolTip(GAUSSIAN_SEGMENTER_TOOLTIPS["ratio_threshold"])

        seg_layout.addRow("Threshold:", self._threshold)
        seg_layout.addRow("Smooth Sigma:", self._smooth_sigma)
        seg_layout.addRow("Background Constant:", self._bgc)
        seg_layout.addRow("Min Pixels:", self._min_px)
        seg_layout.addRow("Offset Threshold:", self._offset_threshold)
        seg_layout.addRow("Ratio Threshold:", self._ratio_threshold)
        self.setLayout(seg_layout)

        for widget in [
            self._threshold,
            self._smooth_sigma,
            self._bgc,
            self._min_px,
            self._offset_threshold,
            self._ratio_threshold,
        ]:
            widget.valueChanged.connect(self._debounceTimer.start)

    def getConfig(self) -> SegmenterConfig:
        return GaussianPeakSearchConfig(
            algorithm="gaussian_peak_search",
            threshold=self._threshold.value(),
            smooth_sigma=self._smooth_sigma.value(),
            bgc=self._bgc.value(),
            min_px=self._min_px.value(),
            offset_threshold=self._offset_threshold.value(),
            ratio_threshold=self._ratio_threshold.value(),
        )

    def setConfig(self, config: SegmenterConfig):
        self._threshold.setValue(config.threshold)
        self._smooth_sigma.setValue(config.smooth_sigma)
        self._bgc.setValue(config.bgc)
        self._offset_threshold.setValue(config.offset_threshold)
        self._ratio_threshold.setValue(config.ratio_threshold)
