import os

from PyQt5 import QtCore
from PyQt5.QtCore import QRect, QPoint
from PyQt5.QtGui import QMovie, QCloseEvent, QShowEvent
from PyQt5.QtWidgets import QDialog, QLabel, QVBoxLayout, QApplication, QWidget


class QLoadingDialog(QDialog):
    def __init__(self, parent=None):
        super(QLoadingDialog, self).__init__()
        self.setFixedSize(100, 100)
        # self.setWindowOpacity(0.8)
        self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
        self.setAttribute(QtCore.Qt.WA_TranslucentBackground)
        app = QApplication.instance()
        curr_theme = "light"
        if app:
            curr_theme = app.property("theme")
        gif_file = os.path.abspath("./assets/icons/{}/loading.gif".format(curr_theme))
        self.movie = QMovie(gif_file)
        self.label = QLabel()
        self.label.setMovie(self.movie)
        self.layout = QVBoxLayout(self)
        self.layout.addWidget(self.label)

    def center(self, host: QWidget = None):
        if host:
            hostGeometry: QRect = host.geometry()
            # dialogGeometry : QRect = self.geometry()
            centerPoint: QPoint = hostGeometry.center()
            centerPoint = host.mapToGlobal(centerPoint)
            offset = 30
            targetPoint = QPoint(centerPoint.x() - offset, centerPoint.y() - offset)
            self.move(targetPoint)
        else:
            screen = QApplication.desktop().screenNumber(QApplication.desktop().cursor().pos())
            centerPoint = QApplication.desktop().screenGeometry(screen).center()
            self.move(centerPoint)
        return self

    def showEvent(self, e: QShowEvent):
        if self.movie.state() == QMovie.NotRunning:
            self.movie.start()

    def closeEvent(self, e: QCloseEvent):
        if self.movie.state() == QMovie.Running:
            self.movie.stop()

    def exec_(self):
        self.center()
        return QDialog.exec_(self)

[((16, 14, 16, 37), 'PyQt5.QtWidgets.QApplication.instance', 'QApplication.instance', ({}, {}), '()', False, 'from PyQt5.QtWidgets import QDialog, QLabel, QVBoxLayout, QApplication, QWidget\n'), ((21, 21, 21, 37), 'PyQt5.QtGui.QMovie', 'QMovie', ({(21, 28, 21, 36): 'gif_file'}, {}), '(gif_file)', False, 'from PyQt5.QtGui import QMovie, QCloseEvent, QShowEvent\n'), ((22, 21, 22, 29), 'PyQt5.QtWidgets.QLabel', 'QLabel', ({}, {}), '()', False, 'from PyQt5.QtWidgets import QDialog, QLabel, QVBoxLayout, QApplication, QWidget\n'), ((24, 22, 24, 39), 'PyQt5.QtWidgets.QVBoxLayout', 'QVBoxLayout', ({(24, 34, 24, 38): 'self'}, {}), '(self)', False, 'from PyQt5.QtWidgets import QDialog, QLabel, QVBoxLayout, QApplication, QWidget\n'), ((52, 15, 52, 34), 'PyQt5.QtWidgets.QDialog.exec_', 'QDialog.exec_', ({(52, 29, 52, 33): 'self'}, {}), '(self)', False, 'from PyQt5.QtWidgets import QDialog, QLabel, QVBoxLayout, QApplication, QWidget\n'), ((37, 21, 37, 43), 'PyQt5.QtWidgets.QApplication.desktop', 'QApplication.desktop', ({}, {}), '()', False, 'from PyQt5.QtWidgets import QDialog, QLabel, QVBoxLayout, QApplication, QWidget\n'), ((38, 26, 38, 48), 'PyQt5.QtWidgets.QApplication.desktop', 'QApplication.desktop', ({}, {}), '()', False, 'from PyQt5.QtWidgets import QDialog, QLabel, QVBoxLayout, QApplication, QWidget\n'), ((37, 57, 37, 79), 'PyQt5.QtWidgets.QApplication.desktop', 'QApplication.desktop', ({}, {}), '()', False, 'from PyQt5.QtWidgets import QDialog, QLabel, QVBoxLayout, QApplication, QWidget\n')]