# ProfileDialog.py -- Pamhyr
# Copyright (C) 2025-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 random

from tools import logger_exception

from View.Tools.PamhyrWindow import PamhyrDialog
from View.Network.translate import NetworkTranslate

from PyQt5.QtGui import (
    QKeySequence,
)

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

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

logger = logging.getLogger()


class SelectProfileDialog(PamhyrDialog):
    _pamhyr_ui = "SelectProfile"
    _pamhyr_name = "Select profile"

    def __init__(self, study=None, config=None,
                 reach=None, trad=None, parent=None):
        self._trad = trad
        if self._trad is not None:
            name = trad["dialog_select_profile_title"]

        super(SelectProfileDialog, self).__init__(
            title=name,
            study=study,
            config=config,
            options=[],
            parent=parent
        )

        self._reach = reach
        self._profile = None

        self.setup_combobox()

    def setup_combobox(self):
        self.combobox_add_items(
            "comboBox",
            list(
                map(
                    lambda p: p.display_name(),
                    self._reach.reach.profiles)))

    def accept(self):
        profile = self.get_combobox_text("comboBox")

        self._profile = next(
            filter(
                lambda p: p.display_name() == profile,
                self._reach.reach.profiles
            )
        )

        super(SelectProfileDialog, self).accept()

    def reject(self):
        self.close()
