# **************************************************************************
# *                                                                        *
# *  Copyright (C) Golaem S.A. - All Rights Reserved.                      *
# *                                                                        *
# **************************************************************************

import sys
import os
from glm.Qtpy.Qt import QtGui, QtCore, QtWidgets, Qt


if sys.version_info[0] == 2:
    # FileNotFoundError is only available since Python 3.3
    FileNotFoundError = IOError
    from io import open

######################################################################
# GolaemAboutWindow Singelton
######################################################################
def getTheGolaemAboutWindowInstance(parentWindow=None, productName="Golaem", golaemVersion=None, baseDir=None):
    """
    Returns the instance singleton
    """
    global golaemAboutWindowInstance
    try:
        golaemAboutWindowInstance
    except NameError:
        golaemAboutWindowInstance = GolaemAboutWindow(parentWindow, productName, golaemVersion, baseDir)

    return golaemAboutWindowInstance


######################################################################
# GolaemAboutWindow
######################################################################
class GolaemAboutWindow(QtWidgets.QMainWindow):
    """
    The Golaem About widget shows information about Golaem
    """

    def __init__(self, parent=None, productName="Golaem", golaemVersion=None, baseDir=None):
        super(GolaemAboutWindow, self).__init__(parent=parent)
        self.productName = productName
        self.golaemVersion = golaemVersion
        self.baseDir = baseDir

        self.initEditor()

    def initEditor(self):
        # get the correct path for ressources
        selfDirectory = os.path.dirname(__file__)
        if self.baseDir is None:
            self.baseDir = selfDirectory
        if not os.path.isfile(os.path.join(self.baseDir, "icons", "glmAbout.png")):
            self.baseDir = os.path.join(selfDirectory, os.pardir, os.pardir, os.pardir)
        if not os.path.isfile(os.path.join(self.baseDir, "icons", "glmAbout.png")):
            self.baseDir = os.path.join(selfDirectory, os.pardir, os.pardir, os.pardir, os.pardir, "Resources")
        golaemIconPath = os.path.join(self.baseDir, "icons", "glmAbout.png")
        golaemImagePath = os.path.join(self.baseDir, "icons", "W09_GOLM_about_528x90.png")

        # init editorMainWindow
        self.setWindowTitle("About Autodesk {}".format(self.productName))
        self.setObjectName("GolaemAboutWindow")
        self.setWindowIcon(QtGui.QIcon(golaemIconPath))

        # Main widget and layout
        self.mainLayout = QtWidgets.QVBoxLayout()
        self.mainLayout.setContentsMargins(0, 0, 0, 0)
        self.mainLayout.setSpacing(0)
        self.centralWidget = QtWidgets.QWidget(self)
        self.centralWidget.setLayout(self.mainLayout)
        self.centralWidget.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
        self.setCentralWidget(self.centralWidget)

        # image UI
        golaemImage = QtWidgets.QLabel(self)
        golaemImage.setPixmap(QtGui.QPixmap(golaemImagePath))
        # layout
        golaemImageLayout = QtWidgets.QHBoxLayout()
        golaemImageWidget = QtWidgets.QWidget(self)
        golaemImageWidget.setLayout(golaemImageLayout)
        golaemImageLayout.addStretch()
        golaemImageLayout.addWidget(golaemImage)
        golaemImageLayout.addStretch()

        self.mainLayout.addWidget(golaemImageWidget)

        # info label
        if self.productName is not None:
            golaemInfoLabel = QtWidgets.QLabel(self)
            golaemInfoLabel.setMargin(5)
            if self.golaemVersion is not None:
                golaemInfoLabel.setText("Version: {} {}".format(self.productName, self.golaemVersion))
            else:
                golaemInfoLabel.setText("{}".format(self.productName))

            self.mainLayout.addWidget(golaemInfoLabel)

        # license info text
        licenseText = "Unable to get license info. Please check your installation."
        licenseTextPath = os.path.join(selfDirectory, "LICENSE")
        try:
            with open(licenseTextPath, "r", encoding="utf-8") as file:
                licenseText = file.read()
        except FileNotFoundError:
            licenseText = "Unable to open license info file. Please check your installation."
        except UnicodeDecodeError:
            licenseText = "There was an error decoding the file. Please ensure the file is encoded in UTF-8."
        # license info widget
        licenseInfoTextBrowser = QtWidgets.QTextBrowser(self)
        licenseInfoTextBrowser.setText(licenseText)
        self.mainLayout.addWidget(licenseInfoTextBrowser)

        # OK button
        OKButton = QtWidgets.QPushButton("OK", self)
        OKButton.setFixedSize(75, 24)
        OKButton.clicked.connect(self.close)
        # layout
        OKLayout = QtWidgets.QHBoxLayout()
        OKWidget = QtWidgets.QWidget(self)
        OKWidget.setLayout(OKLayout)
        OKLayout.addStretch()
        OKLayout.addWidget(OKButton)
        OKLayout.addStretch()
        self.mainLayout.addWidget(OKWidget)
