import inspect
import os

from PyQt6.QtCore import pyqtSignal, pyqtSlot, Qt, QTimer, QEvent, QPropertyAnimation, QEasingCurve, QVariant
from PyQt6.QtGui import QCursor
from PyQt6.QtWidgets import QListWidgetItem, QMainWindow
from PyQt6.uic import loadUiType

from IProgram import IProgram


path = os.path.dirname(os.path.abspath(__file__))
resimYolu = os.path.join(os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))),
                         "Resimler")

MainWindowUI, MainWindowBase = loadUiType(os.path.join(path, 'QFikatiaProgramAciklamaPopup1.ui'))


class QFikatiaProgramAciklamaPopup1(QMainWindow, MainWindowUI):
    sinyal = pyqtSignal(str, QVariant)

    def __init__(self, parent=None, Prg:IProgram=None, itm:QListWidgetItem=None):
        super(QFikatiaProgramAciklamaPopup1, self).__init__(parent)
        self.setupUi(self)
        self.resimYolu = os.path.join(os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))),
                                      "Resimler")

        self.__prg = Prg
        self.__itm = itm

        if hasattr(self.parent(), 'sinyal'):
            self.sinyal = self.parent().sinyal

        self.move(QCursor.pos())

        self.setAttribute(Qt.WidgetAttribute.WA_TranslucentBackground, False)
        from ctypes import windll, c_int, byref

        windll.dwmapi.DwmExtendFrameIntoClientArea(c_int(self.winId().__int__()), byref(c_int(-1)))
        self.setWindowFlags(Qt.WindowType.CustomizeWindowHint | Qt.WindowType.Drawer | Qt.WindowType.WindowStaysOnTopHint)

        self.textAciklama.setText(self.__prg.aciklama
                                     if self.__prg.aciklama != "No Description" or ""
                                     else "Açıklama Yok")

    def closeEvent(self, e):
        self.__prg.aciklama = (
            self.textAciklama.toPlainText() if self.textAciklama.toPlainText() != "Açıklama Yok" or ""
        else "No Description")

    def show(self):
        super(QFikatiaProgramAciklamaPopup1, self).show()
        # self.fadeIn()
        self.sinyal.emit("aciklamaPencereAcildi", self)

    def mousePressEvent(self, event):
        self.offset = event.pos()

    def mouseMoveEvent(self, event):
        x = event.globalX()
        y = event.globalY()
        x_w = self.offset.x()
        y_w = self.offset.y()
        self.move(x - x_w, y - y_w)

    @pyqtSlot()
    def fadeIn(self):
        self.setWindowOpacity(0.1)
        self.anim = QPropertyAnimation(self, "windowOpacity")
        self.anim.setDuration(500)
        self.anim.setStartValue(0.1)
        self.anim.setEndValue(1)
        self.anim.setEasingCurve(QEasingCurve.Linear)
        self.anim.start()

    @pyqtSlot()
    def fadeOut(self):
        self.setWindowOpacity(1.000)
        self.anim = QPropertyAnimation(self, "windowOpacity")
        self.anim.setDuration(500)
        self.anim.setStartValue(1)
        self.anim.setEndValue(0.001)
        self.anim.setEasingCurve(QEasingCurve.Linear)
        self.anim.start()

    def changeEvent(self, e):
        if (e.type() == QEvent.Type.ActivationChange and not self.isActiveWindow()):
            try:
                # self.fadeOut()
                pass
            finally:
                QTimer.singleShot(600, self.__beklemeliKapat)
            return
        QMainWindow.changeEvent(self, e)

    @pyqtSlot()
    def __beklemeliKapat(self):
        if not self.isActiveWindow():
            self.sinyal.emit("aciklamaPencereKapatildi", self)
            self.close()
            del self