import pathlib

from PyQt6.QtGui import QPixmap
from PyQt6.QtWidgets import QDialog, QDialogButtonBox, QLabel, QVBoxLayout, QWidget
from PyQt6.QtCore import Qt


class AboutDialog(QDialog):

    def __init__(self, parent=None) -> None:
        super().__init__(parent=parent)
        self.setModal(True)
        about = About(self)
        self.setLayout(QVBoxLayout())
        self.layout().addWidget(about)
        bbox = QDialogButtonBox(QDialogButtonBox.StandardButton.Ok)
        bbox.accepted.connect(self.accept)
        self.layout().addWidget(bbox)


class About(QWidget):

    def __init__(self, parent=None) -> None:
        super().__init__(parent=parent)
        self.setLayout(QVBoxLayout())

        heading = QLabel("pyRelacs")
        font = heading.font()
        font.setPointSize(18)
        font.setBold(True)
        heading.setFont(font)
        heading.setAlignment(Qt.AlignmentFlag.AlignCenter)
        subheading = QLabel("relacsed electrophysiological recordings")
        subheading.setAlignment(Qt.AlignmentFlag.AlignCenter)
        nix_link = QLabel("https://github.com/relacs")
        nix_link.setOpenExternalLinks(True)
        nix_link.setAlignment(Qt.AlignmentFlag.AlignCenter)
        rtd_link = QLabel("https://relacs.net")
        rtd_link.setOpenExternalLinks(True)
        rtd_link.setAlignment(Qt.AlignmentFlag.AlignCenter)

        iconlabel = QLabel()
        _root = pathlib.Path(__file__).parent.parent
        pixmap = QPixmap(str(pathlib.Path.joinpath(_root, "icons/relacstuxheader.png")))
        s = pixmap.size()
        new_height = int(s.height() * 300/s.width())
        pixmap = pixmap.scaled(300, new_height, Qt.AspectRatioMode.KeepAspectRatio, Qt.TransformationMode.FastTransformation)
        iconlabel.setPixmap(pixmap)
        iconlabel.setMaximumWidth(300)
        iconlabel.setAlignment(Qt.AlignmentFlag.AlignCenter)
        iconlabel.setScaledContents(True)

        self.layout().addWidget(heading)
        self.layout().addWidget(subheading)
        self.layout().addWidget(iconlabel)
        self.layout().addWidget(nix_link)
        self.layout().addWidget(rtd_link)
