# ViCodePy - A video coder for Experimental Psychology
#
# Copyright (C) 2024 Esteban Milleret
# Copyright (C) 2024 Rafael Laboissière
#
# 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/>.

from PySide6.QtWidgets import QMessageBox

FORMAT = 3


def format_ok(fmt, widget=None):
    msg = QMessageBox(widget)
    msg.setWindowTitle("ViCodePy: Project file format issue")

    if not isinstance(fmt, int):
        msg.setIcon(QMessageBox.Critical)
        msg.setText("The format of the project file must be a integer number")
        msg.exec()
        return False

    if fmt < FORMAT:
        msg.setIcon(QMessageBox.Warning)
        msg.setText("The project file format is outdated.")
        msg.setInformativeText(
            f"The format version indicated in the project file ({fmt}) "
            "is outdated in respect to the format version supported by "
            f"this version of ViCodePy ({FORMAT}).\n\n"
            "The project file will be upgraded accordingly."
        )
        msg.setStandardButtons(QMessageBox.Ok)
        msg.exec()
        return True

    if fmt > FORMAT:
        msg.setIcon(QMessageBox.Critical)
        msg.setText("The current installed version of ViCodePy is outdated.")
        msg.setInformativeText(
            "The format version supported by this version of ViCodePy "
            f"({FORMAT}) is outdated in respect to the format version "
            f"indicated in the project file ({fmt}).\n\n"
            "Please, upgrade ViCodePy to the last avaiable version."
        )
        msg.setStandardButtons(QMessageBox.Ok)
        msg.exec()
        return False

    return True
