# ============================================================================== #
    # about_window.py - AcouSed                                   #
    # Copyright (C) 2024  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/>. #

    #                                                      by Brahim MOUDJED #
# ============================================================================== #

import sys
from PyQt5.QtGui import QIcon, QPixmap, QFont
from PyQt5.QtWidgets import (QWidget, QLabel, QHBoxLayout, QVBoxLayout, QApplication, QMainWindow, QGridLayout,
                             QDialog, QDialogButtonBox, QPushButton, QTextEdit, QFrame, QLineEdit)
from PyQt5.QtCore import Qt
import os


class Logos():
    def __init__(self):
        def path_logo(icon):
            return os.path.join(
                os.path.dirname(__file__), "..", "logos", icon
            )

        self.logo_AcouSed = path_logo("AcouSed.png")

        self.logo_INRAE = path_logo("BlocMarque-INRAE-Inter.jpg")

        self.logo_OSR = path_logo("OSR.png")
        self.logo_europe = path_logo("Europe.png")
        self.logo_saone_rhone = path_logo("plan_Rhone_Saone.png")
        self.logo_carnot = path_logo("Carnot_EE.png")

        self.logo_CNR = path_logo("CNR.png")
        self.logo_EDF = path_logo("EDF.png")
        self.logo_Ubertone = path_logo("Ubertone.jpg")

logos_inst = Logos()


class AboutWindow(QDialog):

    def __init__(self):
        super().__init__()

        self.setGeometry(400, 200, 350, 200)

        self.setWindowTitle("About AcouSed")

        self.verticalLayout_Main = QVBoxLayout()
        self.setLayout(self.verticalLayout_Main)

        self.gridLayout = QGridLayout()
        self.verticalLayout_Main.addLayout(self.gridLayout)

        # ----------------------------------------------------------

        self.label_logo_AcouSed = QLabel()
        self.label_logo_AcouSed.setPixmap(QPixmap(logos_inst.logo_AcouSed).
                                          scaledToHeight(128, Qt.SmoothTransformation))
        self.gridLayout.addWidget(self.label_logo_AcouSed, 0, 0, 5, 1, Qt.AlignCenter)

        self.label_acoused = QLabel()
        self.label_acoused.setText("Acoused 2.0")
        self.label_acoused.setFont(QFont("DejaVu Sans", 14))
        self.gridLayout.addWidget(self.label_acoused, 0, 1, 1, 1, Qt.AlignCenter)

        self.label_date = QLabel()
        self.label_date.setText("2025.01.01")
        self.label_date.setFont(QFont("DejaVu Sans", 12))
        self.gridLayout.addWidget(self.label_date, 1, 1, 1, 1, Qt.AlignCenter)

        self.label_logo_INRAE = QLabel()
        self.label_logo_INRAE.setPixmap(QPixmap(logos_inst.logo_INRAE).scaledToHeight(42, Qt.SmoothTransformation))
        self.gridLayout.addWidget(self.label_logo_INRAE, 2, 1, 1, 1, Qt.AlignCenter)

        self.label_contact = QLabel()
        self.label_contact.setText("Contact : celine.berni@inrae.fr \n"
                                   "                   jerome.lecoz@inrae.fr")
        self.gridLayout.addWidget(self.label_contact, 3, 1, 1, 1, Qt.AlignCenter)

        self.label_link = QLabel()
        self.label_link.setText("< a href = https://forgemia.inra.fr/theophile.terraz/acoused > "
                                "https://forgemia.inra.fr/theophile.terraz/acoused </a>")
        self.label_link.setOpenExternalLinks(True)
        self.gridLayout.addWidget(self.label_link, 4, 1, 1, 1, Qt.AlignCenter)

        # ----------------------------------------------------------

        self.horizontalLine = QFrame()
        self.horizontalLine.setFrameShape(QFrame.HLine)
        self.horizontalLine.setFrameShadow(QFrame.Sunken)

        self.verticalLayout_Main.addWidget(self.horizontalLine)

        # ----------------------------------------------------------

        self.gridLayout_button = QGridLayout()
        self.verticalLayout_Main.addLayout(self.gridLayout_button)

        self.button_Licence = QPushButton()
        self.button_Licence.setText("Licence")
        self.gridLayout_button.addWidget(self.button_Licence, 0, 0, 1, 1)

        self.button_Licence.clicked.connect(self.licence_window)

        self.button_Copyright = QPushButton()
        self.button_Copyright.setText("Copyright")
        self.gridLayout_button.addWidget(self.button_Copyright, 0, 1, 1, 1)

        self.button_Copyright.clicked.connect(self.copyright_window)

        self.button_FundsPartners = QPushButton()
        self.button_FundsPartners.setText("Funds/Patners")
        self.gridLayout_button.addWidget(self.button_FundsPartners, 0, 2, 1, 1)

        self.button_FundsPartners.clicked.connect(self.funds_partners_window)

        self.button_Authors = QPushButton()
        self.button_Authors.setText("Authors")
        self.gridLayout_button.addWidget(self.button_Authors, 0, 3, 1, 1)

        self.button_Authors.clicked.connect(self.authors_window)

    def licence_window(self):
        lw = Licence()
        lw.exec()

    def copyright_window(self):
        cw = Copyright()
        cw.exec()

    def funds_partners_window(self):
        sw = FundsPartners()
        sw.exec()

    def authors_window(self):
        sw = Authors()
        sw.exec()


class Licence(QDialog):

    def __init__(self):

        super().__init__()

        self.setWindowTitle("Licence")

        self.setGeometry(500, 300, 450, 300)

        self.verticalLayout = QVBoxLayout()
        self.setLayout(self.verticalLayout)

        self.textEdit = QTextEdit()
        self.verticalLayout.addWidget(self.textEdit)

        self.textEdit.setText("AcouSed \n"
                              "Copyright (C) 2024 - INRAE \n\n"
                              "This program is free software: you can redistribute it and/or modify \n"
                              "it under the terms of the GNU General Public License as published by \n"
                              "the Free Software Foundation, either version 3 of the License, or \n "
                              "(at your option) any later version. \n\n"
                              "This program is distributed in the hope that it will be useful, \n"
                              "but WITHOUT ANY WARRANTY; without even the implied warranty of \n"
                              "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the \n"
                              "GNU General Public License for more details. \n\n"
                              "You should have received a copy of the GNU General Public License \n"
                              "along with this program.  If not, see https://www.gnu.org/licenses/.")


class Copyright(QDialog):

    def __init__(self):

        super().__init__()

        self.setWindowTitle("Copyright")

        self.setGeometry(500, 300, 200, 200)

        self.gridLayout = QGridLayout()
        self.setLayout(self.gridLayout)

        self.textEdit = QTextEdit()
        self.gridLayout.addWidget(self.textEdit, 0, 0, 1, 1)

        self.textEdit.setText("Copyright (c) 2024. INRAE \n"
                              "All Rights Reserved.")

        self.label_INRAE = QLabel()
        self.label_INRAE.setPixmap(QPixmap(logos_inst.logo_INRAE).scaledToHeight(64, Qt.SmoothTransformation))
        self.gridLayout.addWidget(self.label_INRAE, 1, 0, 1, 1, Qt.AlignCenter)


class FundsPartners(QDialog):

    def __init__(self):

        super().__init__()

        self.setWindowTitle("Funds/Partners")

        self.setGeometry(500, 300, 300, 300)

        self.gridLayout = QGridLayout()
        self.setLayout(self.gridLayout)

        self.label_support = QLabel()
        self.label_support.setText("The development of AcouSed software was funded by OSR, Carnot Eau & Environnement, INRAE and CNR Company. \n "
                                   "It was made in collaboration with Ubertone and EDF Companies.")
        self.gridLayout.addWidget(self.label_support, 0, 0, 1, 4, Qt.AlignLeft)

        self.label_OSR = QLabel()
        self.label_OSR.setPixmap(QPixmap(logos_inst.logo_OSR).scaledToHeight(78, Qt.SmoothTransformation))
        self.gridLayout.addWidget(self.label_OSR, 1, 0, 1, 1, Qt.AlignCenter)

        self.label_EUROPE = QLabel()
        self.label_EUROPE.setPixmap(QPixmap(logos_inst.logo_europe).scaledToHeight(32, Qt.SmoothTransformation))
        self.gridLayout.addWidget(self.label_EUROPE, 1, 1, 1, 1, Qt.AlignCenter)

        self.label_SAONE_RHONE = QLabel()
        self.label_SAONE_RHONE.setPixmap(QPixmap(logos_inst.logo_saone_rhone).scaledToHeight(64, Qt.SmoothTransformation))
        self.gridLayout.addWidget(self.label_SAONE_RHONE, 1, 2, 1, 1, Qt.AlignCenter)

        self.label_CARNOT = QLabel()
        self.label_CARNOT.setPixmap(QPixmap(logos_inst.logo_carnot).scaledToHeight(78, Qt.SmoothTransformation))
        self.gridLayout.addWidget(self.label_CARNOT, 1, 3, 1, 1, Qt.AlignCenter)

        self.label_INRAE = QLabel()
        self.label_INRAE.setPixmap(QPixmap(logos_inst.logo_INRAE).scaledToHeight(32, Qt.SmoothTransformation))
        self.gridLayout.addWidget(self.label_INRAE, 2, 0, 1, 1, Qt.AlignCenter)

        self.label_CNR = QLabel()
        self.label_CNR.setPixmap(QPixmap(logos_inst.logo_CNR).scaledToHeight(32, Qt.SmoothTransformation))
        self.gridLayout.addWidget(self.label_CNR, 2, 1, 1, 1, Qt.AlignCenter)

        self.label_Ubertone = QLabel()
        self.label_Ubertone.setPixmap(QPixmap(logos_inst.logo_Ubertone).scaledToHeight(24, Qt.SmoothTransformation))
        self.gridLayout.addWidget(self.label_Ubertone, 2, 2, 1, 1, Qt.AlignCenter)

        self.label_EDF = QLabel()
        self.label_EDF.setPixmap(QPixmap(logos_inst.logo_EDF).scaledToHeight(48, Qt.SmoothTransformation))
        self.gridLayout.addWidget(self.label_EDF, 2, 3, 1, 1, Qt.AlignCenter)


class Authors(QDialog):

    def __init__(self):

        super().__init__()

        self.setWindowTitle("Authors")

        self.setGeometry(500, 300, 300, 200)

        self.gridLayout = QGridLayout()
        self.setLayout(self.gridLayout)

        self.label_adrien = QLabel()
        self.label_adrien.setText("The development of AcouSed was based on Adrien VERGNE's PhD thesis work :")
        self.gridLayout.addWidget(self.label_adrien, 0, 0, 1, 1, Qt.AlignLeft)

        self.label_adrien_thesis = QLabel()
        self.label_adrien_thesis.setText(
            "   - Adrien Vergne thesis (2018): "
            "< a href = https://theses.fr/2018GREAU046 > https://theses.fr/2018GREAU046 </a>")
        self.gridLayout.addWidget(self.label_adrien_thesis, 1, 0, 1, 1)

        self.label_adrien_2020 = QLabel()
        self.label_adrien_2020.setText(
            "   -  Vergne A., Le Coz J., Berni C., & Pierrefeu G. (2020), Water Resources Research, 56(2): "
            "< a href = https://doi.org/10.1029/2019WR024877 > https://doi.org/10.1029/2019WR024877 </a>")
        self.gridLayout.addWidget(self.label_adrien_2020, 2, 0, 1, 1)

        self.label_adrien_2021 = QLabel()
        self.label_adrien_2021.setText(
            "   - Vergne A., Berni C., Le Coz J., & Tencé F., (2021), Water Resources Research, 57(9): "
            "< a href = https://doi.org/10.1029/2021WR029589 > https://doi.org/10.1029/2021WR029589 </a>")
        self.gridLayout.addWidget(self.label_adrien_2021, 3, 0, 1, 1)

        self.label_brahim = QLabel()
        self.label_brahim.setText("\n Acoused was designed and developped by Brahim MOUDJED from 2022 to 2025. \n")
        self.gridLayout.addWidget(self.label_brahim, 4, 0, 1, 1)

        self.label_PA = QLabel()
        self.label_PA.setText("TECC company (< a href = https://parouby.fr/ > https://parouby.fr/ </a>) was involved in 2025 to debugging the program.")
        self.gridLayout.addWidget(self.label_PA, 5, 0, 1, 1)


# if __name__ == "__main__":
#     app = QApplication(sys.argv)
#     w = AboutWindow()
#     w.show()
#     sys.exit(app.exec_())
