# ShiftDialog.py -- Pamhyr
# Copyright (C) 2023-2026  INRAE
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

# -*- coding: utf-8 -*-

from View.Tools.PamhyrWindow import PamhyrDialog
from PyQt5.QtWidgets import QDoubleSpinBox
from View.Tools.FlexibleDoubleSpinBox import FlexibleDoubleSpinBox
from PyQt5.QtWidgets import QDoubleSpinBox
from View.Tools.FlexibleDoubleSpinBox import FlexibleDoubleSpinBox


class ShiftDialog(PamhyrDialog):
    _pamhyr_ui = "GeometryReachShift"
    _pamhyr_name = "Shift"

    def __init__(self, trad=None, parent=None):
        super(ShiftDialog, self).__init__(
            title=trad[self._pamhyr_name],
            trad=trad,
            options=[],
            parent=parent
        )

        self._replace_spinboxes()
        self._init_default_values()

    def _replace_spinboxes(self):
        for name in ["doubleSpinBox_X", "doubleSpinBox_Y", "doubleSpinBox_Z"]:
            old = self.find(QDoubleSpinBox, name)

            if old is None:
                continue

            new = FlexibleDoubleSpinBox(old.parent())
            new.setObjectName(old.objectName())

            # Copier propriétés utiles
            new.setDecimals(old.decimals())
            new.setMinimum(old.minimum())
            new.setMaximum(old.maximum())
            new.setSingleStep(old.singleStep())
            new.setValue(old.value())
            new.setGeometry(old.geometry())

            # Remplacement dans layout si présent
            layout = old.parent().layout()
            if layout is not None:
                layout.replaceWidget(old, new)

            old.hide()
            old.setParent(None)
            old.deleteLater()

    def _init_default_values(self):
        self._dx = 0.0
        self._dy = 0.0
        self._dz = 0.0

    @property
    def dx(self):
        return self._dx

    @property
    def dy(self):
        return self._dy

    @property
    def dz(self):
        return self._dz

    def accept(self):
        self._dx = self.get_double_spin_box("doubleSpinBox_X")
        self._dy = self.get_double_spin_box("doubleSpinBox_Y")
        self._dz = self.get_double_spin_box("doubleSpinBox_Z")
        super().accept()

    def reject(self):
        self.close()
