# 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

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, QItemDelegate,
    QComboBox,
)

from View.Pollutants.Edit.UndoCommand import (
    SetDataCommand, PasteCommand,
)

_translate = QCoreApplication.translate

logger = logging.getLogger()


class ComboBoxDelegate(QItemDelegate):
    def __init__(self, type_dict=None, trad=None, parent=None):
        super(ComboBoxDelegate, self).__init__(parent)

        # self._type_dict = type_dict if type_dict else POLLUTANT_TYPES
        self._trad = trad
        self.editor = None

        self._type = {}
        if self._trad is not None:
            self._type = self._trad.get_dict("type")

    def createEditor(self, parent, option, index):
        self.editor = QComboBox(parent)

        # Ajouter les items du dictionnaire
        for key, value in self._type.items():
            self.editor.addItem(value, key)

        self.editor.currentTextChanged.connect(self.currentItemChanged)
        return self.editor

    def setEditorData(self, editor, index):
        """Affiche la valeur courante"""
        current_value = index.data(Qt.DisplayRole)  # C'est la string

        # Trouve l'index du combobox qui correspond au texte affiché
        current_index = editor.findText(current_value)
        if current_index >= 0:
            editor.setCurrentIndex(current_index)

    def setModelData(self, editor, model, index):
        """Récupère l'index (la clé) et le stocke dans le modèle"""
        # Récupère la clé (userData) du combobox
        selected_index = editor.currentData()
        model.setData(index, selected_index)
        editor.close()
        editor.deleteLater()

    def updateEditorGeometry(self, editor, option, index):
        r = QRect(option.rect)
        if self.editor and self.editor.windowFlags() & Qt.Popup:
            if editor.parent() is not None:
                r.setTopLeft(self.editor.parent().mapToGlobal(r.topLeft()))
        editor.setGeometry(r)

    @pyqtSlot()
    def currentItemChanged(self):
        self.commitData.emit(self.sender())


class TableModel(PamhyrTableModel):
    def __init__(self, trad=None, **kwargs):
        self._trad = trad
        self._types = {}
        if self._trad is not None:
            self._types = self._trad.get_dict("types")

        super(TableModel, self).__init__(trad=trad, **kwargs)

    def is_same_data(self, index, value):
        row = index.row()
        column = index.column()

        if self._headers[column] == "type":
            return int(self._data.data[row][column]) == int(value)

        return super(TableModel, self).is_same_data(index, value)

    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()

        if len(self._data.data) == 0 or len(self._data.data[row]) == 0:
            return QVariant()

        value = self._data.data[row][column]

        # Si c'est la colonne "type", convertir l'indice en string
        if self._headers[column] == "type":
            value = self._trad.get_dict("type").get(value, "")

        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:
            if self._headers[column] == "type":
                self._undo.push(
                    SetDataCommand(
                        self._data, row, column, int(value)
                    )
                )
            else:
                self._undo.push(
                    SetDataCommand(
                        self._data, row, column, float(value)
                    )
                )
        except Exception as e:
            logger.info(e)
            logger.debug(traceback.format_exc())

        self.dataChanged.emit(index, index)
        return True

    def paste(self, row, 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(d),
                                data
                            )
                        )
            )
        )

        self.layoutChanged.emit()
