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

from copy import deepcopy
from tools import trace, timer

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

from Model.LateralContribution.LateralContribution import LateralContribution
from Model.LateralContribution.LateralContributionList import (
    LateralContributionList
)

from Model.LateralContribution.LateralContributionTypes import (
    NotDefined, LateralContrib,
)


class SetNameCommand(QUndoCommand):
    def __init__(self, lcs, tab, index, new_value):
        QUndoCommand.__init__(self)

        self._lcs = lcs
        self._tab = tab
        self._index = index
        self._old = self._lcs.get(self._tab, self._index).name
        self._new = str(new_value)

    def undo(self):
        self._lcs.get(self._tab, self._index).name = self._old

    def redo(self):
        self._lcs.get(self._tab, self._index).name = self._new


class SetBeginCommand(QUndoCommand):
    def __init__(self, lcs, tab, index, new_value):
        QUndoCommand.__init__(self)

        self._lcs = lcs
        self._tab = tab
        self._index = index
        self._old = self._lcs.get(self._tab, self._index).begin_section
        self._new = new_value

    def undo(self):
        self._lcs.get(self._tab, self._index).begin_section = self._old

    def redo(self):
        self._lcs.get(self._tab, self._index).begin_section = self._new


class SetEndCommand(QUndoCommand):
    def __init__(self, lcs, tab, index, new_value):
        QUndoCommand.__init__(self)

        self._lcs = lcs
        self._tab = tab
        self._index = index
        self._old = self._lcs.get(self._tab, self._index).end_section
        self._new = new_value

    def undo(self):
        self._lcs.get(self._tab, self._index).end_section = self._old

    def redo(self):
        self._lcs.get(self._tab, self._index).end_section = self._new


class SetEdgeCommand(QUndoCommand):
    def __init__(self, lcs, tab, index, edge):
        QUndoCommand.__init__(self)

        self._lcs = lcs
        self._tab = tab
        self._index = index
        self._old = self._lcs.get(self._tab, self._index).reach
        self._new = edge

    def undo(self):
        self._lcs.get(self._tab, self._index).reach = self._old

    def redo(self):
        self._lcs.get(self._tab, self._index).reach = self._new


class SetTypeCommand(QUndoCommand):
    def __init__(self, lcs, tab, index, _type):
        QUndoCommand.__init__(self)

        self._lcs = lcs
        self._tab = tab
        self._index = index
        self._type = _type
        self._old = self._lcs.get(self._tab, self._index)
        self._new = self._lcs.get(self._tab, self._index)\
                             .convert(self._type)

    def undo(self):
        self._lcs.set(self._tab, self._index, self._old)

    def redo(self):
        self._lcs.set(self._tab, self._index, self._new)


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

        self._lcs = lcs
        self._tab = tab
        self._index = index
        self._new = None

    def undo(self):
        self._new.set_as_deleted()

    def redo(self):
        if self._new is None:
            self._new = self._lcs.new(self._tab, self._index)
        else:
            self._new.set_as_not_deleted()


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

        self._lcs = lcs
        self._tab = tab
        self._rows = rows

        self._lc = []
        for row in rows:
            self._lc.append(self._lcs.get_tab(self._tab)[row])

    def undo(self):
        for el in self._lc:
            el.set_as_not_deleted()

    def redo(self):
        for el in self._lc:
            el.set_as_deleted()


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

        self._lcs = lcs
        self._tab = tab
        self._reverse = _reverse

        self._old = self._lcs._tabs[self._tab].copy()
        self._indexes = None

    def undo(self):
        ll = self._lcs._tabs[self._tab].copy()
        self._lcs.sort(
            self._tab,
            key=lambda x: self._indexes[ll.index(x)]
        )

    def redo(self):
        self._lcs.sort(
            self._tab,
            reverse=self._reverse,
            key=lambda x: x.name
        )

        if self._indexes is None:
            self._indexes = list(
                map(
                    lambda p: self._old.index(p),
                    self._lcs._tabs[self._tab]
                )
            )
            self._old = None


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

        self._lcs = lcs
        self._tab = tab
        self._up = up == "up"
        self._i = i

    def undo(self):
        if self._up:
            self._lcs.move_up(self._tab, self._i)
        else:
            self._lcs.move_down(self._tab, self._i)

    def redo(self):
        if self._up:
            self._lcs.move_up(self._tab, self._i)
        else:
            self._lcs.move_down(self._tab, self._i)


class PasteCommand(QUndoCommand):
    def __init__(self, lcs, tab, row, lc):
        QUndoCommand.__init__(self)

        self._lcs = lcs
        self._tab = tab
        self._row = row
        self._lc = deepcopy(lc)
        self._lc.reverse()

    def undo(self):
        self._lcs.delete(self._tab, self._lc)

    def redo(self):
        for lc in self._lc:
            self._lcs.insert(self._tab, self._row, lc)


class ImportCommand(QUndoCommand):
    def __init__(self, lcs, tab, data):
        QUndoCommand.__init__(self)

        self._tab = tab
        self._lcs = lcs
        self._data = data
        self._old_rows = list(range(len(self._lcs.get_tab(self._tab))))
        self._new_rows = list(range(len(self._data)))

        self._new_lc = None
        self._old_lc = []
        for row in self._old_rows:
            self._old_lc.append((row, self._lcs.get(self._tab, row)))

    def undo(self):
        self._lcs.delete_i(self._tab, self._new_rows)
        for row, el in self._old_lc:
            self._lcs.insert(self._tab, row, el)

    def redo(self):
        self._lcs.delete_i(self._tab, self._old_rows)
        if self._new_lc is None:
            self._new_lc = []
            for row, data in enumerate(self._data):
                new = LateralContrib(status=self._lcs._status)
                new.edge = data[0]
                new.begin_rk = data[1]
                new.end_rk = data[2]
                for i, val in enumerate(data[3:]):
                    new.insert(i, val)
                self._new_lc.append(new)

        for row, el in enumerate(self._new_lc):
            self._lcs.insert(self._tab, row, el)


class DuplicateCommand(QUndoCommand):
    def __init__(self, lcs, tab, rows, lc):
        QUndoCommand.__init__(self)

        self._lcs = lcs
        self._tab = tab
        self._rows = rows
        self._lc = deepcopy(lc)
        self._lc.reverse()

    def undo(self):
        self._lcs.delete(self._tab, self._lc)

    def redo(self):
        for lc in self._lcs:
            self._lcs.insert(self._tab, self._rows[0], lc)
