# Table.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
import traceback

from datetime import date, time, datetime, timedelta

from tools import (
    trace, timer,
    timestamp_to_old_pamhyr_date,
    old_pamhyr_date_to_timestamp
)

from View.Tools.PamhyrTable import PamhyrTableModel

from PyQt5.QtCore import (
    Qt, QVariant, QAbstractTableModel,
    QCoreApplication, QModelIndex, pyqtSlot,
    QRect, QTime, QDateTime,
)

from PyQt5.QtWidgets import (
    QTableView, QAbstractItemView, QSpinBox,
    QTimeEdit, QDateTimeEdit, QItemDelegate,
)

from Model.BoundaryCondition.BoundaryConditionTypes import (
    NotDefined, PonctualContribution,
    TimeOverZ, TimeOverDischarge, ZOverDischarge
)

from View.BoundaryCondition.Edit.UndoCommand import (
    SetDataCommand, AddCommand, DelCommand,
    SortCommand, MoveCommand, PasteCommand,
    ReplaceDataCommand,
)

_translate = QCoreApplication.translate

logger = logging.getLogger()


class TableModel(PamhyrTableModel):
    def get_true_data_row(self, row):

        if len(self._data.data) > 0:
            bc = self._data.data[row]
        else:
            return 0

        return next(
            map(
                lambda e: e[0],
                filter(
                    lambda e: e[1] == bc,
                    enumerate(self._data._data)
                )
            ), 0
        )

    def data(self, index, role):
        if role == Qt.TextAlignmentRole:
            return Qt.AlignHCenter | Qt.AlignVCenter

        if role != Qt.ItemDataRole.DisplayRole:
            return QVariant()

        row = index.row()
        column = index.column()

        value = QVariant()

        if 0 <= column < 2:
            row = self.get_true_data_row(row)
            v = self._data.get_i(row)[column]
            if self._data.get_type_column(column) == float:
                if type(v) is str:
                    v = v.replace(',', '.')
                value = f"{float(v):.4f}"
            elif self._data.header[column] == "time":
                if self._opt_data == "time":
                    value = timestamp_to_old_pamhyr_date(int(v))
                else:
                    value = str(datetime.fromtimestamp(v))
            else:
                value = f"{v}"

        return value

    def setData(self, index, value, role=Qt.EditRole):
        if not index.isValid() or role != Qt.EditRole:
            return False

        if self.is_same_data(index, value):
            return False

        row = index.row()
        column = index.column()

        try:
            row = self.get_true_data_row(row)

            self._undo.push(
                SetDataCommand(
                    self._data, row,
                    column, value
                )
            )
        except Exception as e:
            logger.warning(e)
            logger.debug(traceback.format_exc())

        self.update()
        return True

    def add(self, row, parent=QModelIndex()):
        self.beginInsertRows(parent, row, row - 1)

        row = self.get_true_data_row(row)
        self._undo.push(
            AddCommand(
                self._data, row
            )
        )

        self.endInsertRows()
        self.update()

    def delete(self, rows, parent=QModelIndex()):
        self.beginRemoveRows(parent, rows[0], rows[-1])

        rows = list(map(
            lambda r: self.get_true_data_row(r),
            rows))

        self._undo.push(
            DelCommand(
                self._data, rows
            )
        )

        self.endRemoveRows()
        self.update()

    def sort(self, _reverse, parent=QModelIndex()):
        self.layoutAboutToBeChanged.emit()

        self._undo.push(
            SortCommand(
                self._data, _reverse
            )
        )

        self.layoutAboutToBeChanged.emit()
        self.update()

    def move_up(self, row, parent=QModelIndex()):
        if row <= 0:
            return

        target = row + 1

        self.beginMoveRows(parent, row - 1, row - 1, parent, target)

        row = self.get_true_data_row(row)
        self._undo_stack.push(
            MoveCommand(
                self._data, "up", row
            )
        )

        self.endMoveRows()
        self.update()

    def move_down(self, index, parent=QModelIndex()):
        if row > len(self._data):
            return

        target = row

        self.beginMoveRows(parent, row + 1, row + 1, parent, target)

        row = self.get_true_data_row(row)
        self._undo_stack.push(
            MoveCommand(
                self._data, "down", row
            )
        )

        self.endMoveRows()
        self.update()

    def paste(self, row, header, data):
        if len(data) == 0:
            return

        self.layoutAboutToBeChanged.emit()

        self._undo.push(
            PasteCommand(
                self._data, row,
                list(
                    map(
                        lambda d: self._data.new_from_data(header, d),
                        data
                    )
                )
            )
        )

        self.layoutAboutToBeChanged.emit()
        self.update()

    def auto_sort(self):
        self.layoutAboutToBeChanged.emit()
        self._data.sort(key=lambda x: x[0])
        self.layoutAboutToBeChanged.emit()

    def update(self):
        # self.auto_sort()
        self.layoutChanged.emit()

    def replace_data(self, data1, data2):
        self.layoutAboutToBeChanged.emit()
        self._undo.push(
            ReplaceDataCommand(
                self._data, data1, data2
            )
        )
        self.layoutAboutToBeChanged.emit()
        self.update()

    def read_from_file(self, file_name, bctype):

        logger.debug(f"Import boundary conditions from {file_name}")
        data0 = []
        data1 = []
        if bctype == "ZD":
            mult = 1
        else:
            mult = 60
        with open(file_name, encoding="utf-8") as bc_file:
            for line in bc_file:
                if not (line.startswith("#") or
                        line.startswith("*") or
                        line.startswith("$")):
                    line = line.split()
                    if bctype == "ZD":
                        data0.append(float(line[0]))
                    else:
                        data0.append(old_pamhyr_date_to_timestamp(line[0]))
                    data1.append(line[1])

        self.replace_data(data0, data1)
