# -*- coding: utf-8 -*-
from qgis.PyQt.QtCore import Qt
from qgis.PyQt.QtGui import (QBrush, QColor, QImage, QMovie, QPalette, QPixmap)
from qgis.PyQt.QtWidgets import (QApplication, QDialog, QFrame, QHBoxLayout, QLabel, QTextEdit)

from autoLoaderRaster.utils import (funnyPushButton, getThemeIcon, translateMsg, windowIcon)


class ui_About(QDialog):
    def __init__(self, parent):
        QDialog.__init__(self)
        self.parent = parent
        self.setupUi()
    
    def setupUi(self):
        self.setObjectName("ui_About")
        self.maxWidth, self.maxheight = 530, 400 
        self.setMinimumSize(self.maxWidth,self.maxheight)
        self.setMaximumSize(self.maxWidth,self.maxheight)        
        self.resize(self.maxWidth, self.maxheight) 
        windowIcon(self, "about.png")
        
        self.textEdit = QTextEdit(self)

        palette = QPalette()
        brush = QBrush(QColor(0,0,0,0))
        brush.setStyle(Qt.BrushStyle.SolidPattern)
        palette.setBrush(QPalette.ColorGroup.Active, QPalette.ColorRole.Base,brush)
        brush = QBrush(QColor(0,0,0,0))
        brush.setStyle(Qt.BrushStyle.SolidPattern)
        palette.setBrush(QPalette.ColorGroup.Inactive, QPalette.ColorRole.Base,brush)

        brush = QBrush(QColor(255,255,255))
        brush.setStyle(Qt.BrushStyle.SolidPattern)
        palette.setBrush(QPalette.ColorGroup.Disabled, QPalette.ColorRole.Base,brush)
        self.textEdit.setPalette(palette)
        self.textEdit.setAutoFillBackground(True)
        self.textEdit.width = 320
        self.textEdit.height = 380
        self.textEdit.setFrameShape(QFrame.Shape.NoFrame)
        self.textEdit.setFrameShadow(QFrame.Shadow.Plain)
        self.textEdit.setReadOnly(True)
        self.textEdit.setObjectName("textEdit")
        self.textEdit.setTextInteractionFlags(Qt.TextInteractionFlag.TextBrowserInteraction)
        self.textEdit.setGeometry(10, 320, 200, 60)  

        self.labelImage = QLabel(self)
        myImgPath = getThemeIcon("autoLoaderRasterPluginAbout.png")
        carIcon = QImage(myImgPath)
        self.labelImage.setPixmap(QPixmap.fromImage(carIcon))
        self.labelImage.setGeometry(60, 10, 440, 300)

        self.lblImage = QLabel(self)
        self.movie = QMovie(getThemeIcon("animation.gif")) 
        self.lblImage.setMovie(self.movie)
        self.lblImage.setLayout(QHBoxLayout())
        self.lblImage.layout().addWidget(QLabel(''))
        self.lblImage.setVisible(False)
        self.lblImage.setGeometry(220, 10, 286, 308) 
        self.lblImage.setVisible(True)
        self.isMovieStart = False
        self.movie.start()
        self.lblImage.repaint()

        icon = getThemeIcon("closeabout.png")
        toolTip = translateMsg("Close")
        self.closeButton = funnyPushButton(self)
        self.closeButton.initPushButton(128, 128, 180, 250, "closeButton", "", toolTip, True, icon, 128, 128, True)
        
        self.translateUi()
        self.closeButton.clicked.connect(self.reject)

    def startMovie(self):
        self.isMovieStart = True
        self.movie.start()
        self.labelImage.repaint()

    def stopMovie(self):
        self.isMovieStart = False
        self.movie.stop()
        self.labelImage.repaint()
        
    def unload(self) : self.stopMovie()

    def translateUi(self):
        title = translateMsg("Dynamic grid raster loader")
        self.setWindowTitle("%s %s" % (title, self.parent.version))
        self.textEdit.setHtml("<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
        "p, li { white-space: pre-wrap; }\n"
        "</style></head><body style=\" font-family:\'Sans Serif\'; font-size:8pt; font-weight:400; font-style:normal;\">\n"
        "</td></tr></table>"
        "<p style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"></p>\n"
        "<p style=\"margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">"
        "<b>M. MASSE Christophe</b>."
        "<br><i>%s</i></p></body></html>" % self.parent.dateVersion)

