# UndoCommand.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 -*-

import logging

from copy import deepcopy
from tools import trace, timer, logger_exception

from PyQt5.QtWidgets import (
    QMessageBox, QUndoCommand, QUndoStack, QTableView,
)
from PyQt5.QtCore import (QItemSelection, QItemSelectionRange,
                          QItemSelectionModel)

from Model.Geometry import Reach
from Model.Except import exception_message_box

logger = logging.getLogger()


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 SetRKCommand(SetDataCommand):
    def __init__(self, reach, index, old_value, new_value):
        self.type = float
        super(SetRKCommand, self).__init__(reach, index, old_value, new_value)

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

    def redo(self):
        self._reach.profile(self._index).rk = 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(self._reach.profile(row))

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

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


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: p.copy(),
                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: p.copy(),
                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)


class ImportCommand(QUndoCommand):
    def __init__(self, reach, row, filename):
        QUndoCommand.__init__(self)

        self._reach = reach
        self._row = row
        self._filename = filename
        self._profiles = None
        self._old_profiles = []
        for row in range(len(self._reach)):
            self._old_profiles.append((self._reach.profile(row)))
        self._old_profiles.reverse()

    def undo(self):
        self._reach.delete_profiles(self._profiles)
        for profile in self._old_profiles:
            self._reach.insert_profile(self._row, profile)

    def redo(self):
        if self._profiles is None:
            self._reach.delete_profiles(self._old_profiles)
            try:
                self._profiles = self._reach.import_geometry(self._filename)
                self._profiles.reverse()
            except Exception as e:
                for profile in self._old_profiles:
                    self._reach.insert_profile(self._row, profile)
                logger_exception(e)
                exception_message_box(e)
        else:
            self._reach.delete_profiles(self._old_profiles)
            for profile in self._profiles:
                self._reach.insert_profile(self._row, profile)


class UpdateRKCommand(QUndoCommand):
    def __init__(self, reach, mesher, data):
        QUndoCommand.__init__(self)

        self._reach = reach
        self._data = data
        self._mesher = mesher
        self._rks = reach.get_rk()
        self._frictions = [f for f in reach._parent.frictions.frictions
                           if f.is_full_defined()]
        self._begin_rk = [f.begin_rk for f in self._frictions]
        self._end_rk = [f.end_rk for f in self._frictions]

        self._new_rks = None
        self._new_begin_rk = None
        self._new_end_rk = None

    def undo(self):

        self._reach.update_rks(self._rks)

        for begin_rk, end_rk, friction in zip(self._begin_rk,
                                              self._end_rk,
                                              self._frictions):
            friction.update_rks(begin_rk, end_rk)

    def redo(self):
        if self._new_rks is None:
            self._new_rks, self._new_begin_rk, self._new_end_rk = \
                self._mesher.update_rk(
                    self._reach, self._begin_rk, self._end_rk,
                    **self._data
                )

        self._reach.update_rks(self._new_rks)

        for begin_rk, end_rk, friction in zip(self._new_begin_rk,
                                              self._new_end_rk,
                                              self._frictions):
            friction.update_rks(begin_rk, end_rk)


class MeshingCommand(QUndoCommand):
    def __init__(self, reach, new_profiles, data, tableView):
        QUndoCommand.__init__(self)

        self._reach = reach
        self._data = data
        self._limites = data["limites"]

        self._new_profiles = new_profiles
        self._new_profiles_indexes = None
        self._tableView = tableView
        self._indexes = tableView.selectionModel().selection()

        rows = list(
            set(
                (i.row() for i in tableView.selectedIndexes())
            )
        )
        self._selected_rk = [self._reach.profile(r).rk for r in rows]
        self._new_indexes = None

    def undo(self):
        self._reach.hard_delete(self._new_profiles_indexes)

        # Update selection
        selection = self._tableView.selectionModel()
        selection.select(
            self._indexes,
            QItemSelectionModel.Rows |
            QItemSelectionModel.ClearAndSelect |
            QItemSelectionModel.Select
        )

    def redo(self):
        if self._new_profiles_indexes is None:
            k = self._limites[0]
            self._new_profiles_indexes = []
            for i in range(self._limites[1] - self._limites[0]):
                k += 1
                for p in self._new_profiles[i]:
                    self._new_profiles_indexes.append(k)
                    k += 1

        k = self._limites[0]
        for i in range(self._limites[1] - self._limites[0]):
            k += 1
            for p in self._new_profiles[i]:
                self._reach.insert_profile(k, p)
                k += 1

        # Update selection
        if self._new_indexes is None:
            ind = []
            for i in range(self._reach.number_profiles):
                if self._reach.profile(i).rk in self._selected_rk:
                    ind.append(i)
            self._tableView.setFocus()
            self._new_indexes = QItemSelection()
            if len(ind) > 0:
                for i in ind:
                    self._new_indexes.append(QItemSelectionRange(
                        self._tableView.model().index(i, 0)
                    ))

        selection = self._tableView.selectionModel()
        selection.select(
            self._new_indexes,
            QItemSelectionModel.Rows |
            QItemSelectionModel.ClearAndSelect |
            QItemSelectionModel.Select
        )


class PurgeCommand(QUndoCommand):
    def __init__(self, reach, np_purge):
        QUndoCommand.__init__(self)

        self._reach = reach
        self._np_purge = np_purge
        self._deleted = {}

    def undo(self):
        for profile in self._deleted:
            for point in self._deleted[profile]:
                point.set_as_not_deleted()
            profile.modified()

    def redo(self):
        for profile in self._reach._profiles:
            self._deleted[profile] = profile.purge(self._np_purge)


class ChangeReachCommand(QUndoCommand):
    def __init__(self, new_reach, parent):
        QUndoCommand.__init__(self)

        self._old_reach = parent._study.river.current_reach()
        self._new_reach = new_reach
        self._parent = parent

    def undo(self):
        p = self._parent
        p._reach = self._old_reach.reach
        p._study.river.set_current_reach(self._old_reach)
        p.setup_table()
        p.update_redraw()
        p.find(QTableView, "tableView").selectionModel()\
                                       .selectionChanged\
                                       .connect(p.select_current_profile)

    def redo(self):
        p = self._parent
        p._reach = self._new_reach.reach
        p._study.river.set_current_reach(self._new_reach)
        p.setup_table()
        p.update_redraw()
        p.find(QTableView, "tableView").selectionModel()\
                                       .selectionChanged\
                                       .connect(p.select_current_profile)


class ShiftCommand(QUndoCommand):
    def __init__(self, reach, rows, dx, dy, dz):
        QUndoCommand.__init__(self)

        self._reach = reach
        self._rows = rows
        self._dx = dx
        self._dy = dy
        self._dz = dz

        self._old = []
        for profile in self._reach.profiles:
            self._old.append(profile.points.copy())

    def undo(self):
        for i in self._rows:
            profile = self._reach.profiles[i]
            profile.shift(
                -self._dx, -self._dy, -self._dz
            )

    def redo(self):
        for i in self._rows:
            profile = self._reach.profiles[i]
            profile.shift(
                self._dx, self._dy, self._dz
            )
