# Table.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 tools import trace, timer

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

from PyQt5.QtGui import (
    QColor, QBrush,
)

from PyQt5.QtWidgets import (
    QDialogButtonBox, QPushButton, QLineEdit,
    QFileDialog, QTableView, QAbstractItemView,
    QUndoStack, QShortcut, QAction, QItemDelegate,
    QComboBox,
)

from View.Tools.PamhyrTable import PamhyrTableModel

_translate = QCoreApplication.translate


class TableModel(PamhyrTableModel):
    def data(self, index, role):
        row = index.row()
        column = index.column()

        if role == Qt.ForegroundRole:
            if self._headers[column] == "status":
                color = Qt.gray

                if self._data[row].is_ok():
                    color = Qt.green
                elif self._data[row].is_warning():
                    color = Qt.yellow
                elif self._data[row].is_error():
                    color = Qt.red

                return QBrush(color)
            return QVariant()

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

        if self._headers[column] == "name":
            return self._data[row].name
        if self._headers[column] == "status":
            return self._data[row].summary

        return QVariant()
