# 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, QPushButton


class CoordinatesDialog(PamhyrDialog):
    _pamhyr_ui = "GeotiffCoordinates"
    _pamhyr_name = "ImageCoordinates"

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

        self.spinbox = {}
        self._init_default_values(x, y)
        self.setup_connections()

    def _init_default_values(self, x, y):
        self._x = x
        self._y = y
        self._values = {"bottom": x[0],
                        "top": x[1],
                        "left": y[0],
                        "right": y[1]}
        self.spinbox = {}
        for t in self._values:
            self.spinbox[t] = self.find(QDoubleSpinBox, f"doubleSpinBox_{t}")
            self.reset(t)

    def setup_connections(self):
        for t in self._values:
            self.find(QPushButton,
                      f"pushButton_{t}").clicked.connect(
                          lambda state, x=t: self.reset(x))

    def reset(self, t):
        self.spinbox[t].setValue(self._values[t])

    @property
    def x(self):
        return [self._values["bottom"], self._values["top"]]

    @property
    def y(self):
        return [self._values["left"], self._values["right"]]

    @property
    def values(self):
        return [self._values[t] for t in self._values]

    def accept(self):
        for t in self._values:
            self._values[t] = self.get_double_spin_box(f"doubleSpinBox_{t}")
        super().accept()

    def reject(self):
        self.close()
