# UndoCommand.py -- Pamhyr
# Copyright (C) 2023  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 copy import deepcopy
from tools import trace, timer

from PyQt5.QtWidgets import (
    QMessageBox, QUndoCommand, QUndoStack,
)

from Model.Geometry import Reach


class SetDataCommand(QUndoCommand):
    def __init__(self, reach, index, old_value, new_value):
        QUndoCommand.__init__(self)

        self._reach = reach
        self._index = index
        self._old = old_value
        self._new = self.type(new_value)


class SetNameCommand(SetDataCommand):
    def __init__(self, reach, index, old_value, new_value):
        self.type = str
        super(SetNameCommand, self).__init__(
            reach, index, old_value, new_value)

    def undo(self):
        self._reach.profile(self._index).name = self._old

    def redo(self):
        self._reach.profile(self._index).name = self._new


class SetKPCommand(SetDataCommand):
    def __init__(self, reach, index, old_value, new_value):
        self.type = float
        super(SetKPCommand, self).__init__(reach, index, old_value, new_value)

    def undo(self):
        self._reach.profile(self._index).kp = self._old

    def redo(self):
        self._reach.profile(self._index).kp = self._new


class AddCommand(QUndoCommand):
    def __init__(self, reach, index):
        QUndoCommand.__init__(self)

        self._reach = reach
        self._index = index
        self._profile = None

    def undo(self):
        self._reach.delete_profiles([self._profile])

    def redo(self):
        if self._profile is None:
            self._profile = self._reach.insert(self._index)
        else:
            self._reach.insert_profile(self._index, self._profile)


class DelCommand(QUndoCommand):
    def __init__(self, reach, rows):
        QUndoCommand.__init__(self)

        self._reach = reach
        self._rows = rows

        self._profiles = []
        for row in rows:
            self._profiles.append((row, self._reach.profile(row)))
        self._profiles.sort()

    def undo(self):
        for row, profile in self._profiles:
            self._reach.insert_profile(row, profile)

    def redo(self):
        self._reach.delete(self._rows)


class SortCommand(QUndoCommand):
    def __init__(self, reach, _reverse):
        QUndoCommand.__init__(self)

        self._reach = reach
        self._reverse = _reverse

        old = self._reach.profiles
        self._reach.sort(self._reverse)
        new = self._reach.profiles

        self._indexes = list(
            map(
                lambda p: old.index(p),
                new
            )
        )

    def undo(self):
        self._reach.sort_with_indexes(self._indexes)

    def redo(self):
        self._reach.sort(self._reverse)


class MoveCommand(QUndoCommand):
    def __init__(self, reach, up, i):
        QUndoCommand.__init__(self)

        self._reach = reach
        self._up = up == "up"
        self._i = i

    def undo(self):
        if self._up:
            self._reach.move_up_profile(self._i)
        else:
            self._reach.move_down_profile(self._i)

    def redo(self):
        if self._up:
            self._reach.move_up_profile(self._i)
        else:
            self._reach.move_down_profile(self._i)


class PasteCommand(QUndoCommand):
    def __init__(self, reach, row, profiles):
        QUndoCommand.__init__(self)

        self._reach = reach
        self._row = row
        self._profiles = list(
            map(
                lambda p: deepcopy(p),
                profiles
            )
        )
        self._profiles.reverse()

    def undo(self):
        self._reach.delete_profiles(self._profiles)

    def redo(self):
        for profile in self._profiles:
            self._reach.insert_profile(self._row, profile)


class DuplicateCommand(QUndoCommand):
    def __init__(self, reach, rows, profiles):
        QUndoCommand.__init__(self)

        self._reach = reach
        self._rows = rows
        self._profiles = list(
            map(
                lambda p: deepcopy(p),
                profiles
            )
        )
        self._profiles.reverse()

    def undo(self):
        self._reach.delete_profiles(self._profiles)

    def redo(self):
        for profile in self._profiles:
            self._reach.insert_profile(self._rows[0], profile)
