import sys
from PyQt6.QtWidgets import (
    QApplication,
    QWidget,
    QVBoxLayout,
    QPushButton,
    QLabel,
    QFontDialog,
)
from PyQt6.QtCore import Qt
from PyQt6.QtGui import QFont


class FontPickerApp(QWidget):
    def __init__(self):
        super().__init__()

        # Window setup
        self.setWindowTitle("PyQt6 Font Picker Demo")
        self.setMinimumSize(400, 300)

        # Create layout
        self.layout = QVBoxLayout()
        self.setLayout(self.layout)

        # 1. The Target Label: This is what we want to change
        self.label = QLabel(
            "This is a sample text.\nClick the button below to change my font style, size, and weight."
        )
        self.label.setAlignment(Qt.AlignmentFlag.AlignCenter)

        # Add some basic styling to make it look nice
        self.label.setStyleSheet("""
            QLabel {
                font-size: 16px;
                padding: 30px;
                border: 2px solid #dcdcdc;
                border-radius: 15px;
                background-color: #000000;
            }
        """)
        self.layout.addWidget(self.label)

        # 2. The Button: This triggers the dialog
        self.btn = QPushButton("Change Font Style")
        self.btn.setStyleSheet("""
            QPushButton {
                padding: 10px;
                background-color: #4CAF50;
                color: white;
                font-size: 14px;
                border-radius: 5px;
            }
            QPushButton:hover {
                background-color: #45a049;
            }
        """)
        self.btn.clicked.connect(self.open_font_dialog)
        self.layout.addWidget(self.btn)

    def open_font_dialog(self):
        """
        This function opens the dialog and applies the selected font
        to the label.
        """
        # QFontDialog.getFont(parent, title, current_font)
        # 'ok' is a boolean that is True if the user clicks 'OK'
        # and False if they click 'Cancel'.
        options = QFontDialog.FontDialogOption.DontUseNativeDialog
        font, ok = QFontDialog.getFont(self.label.font(), self, options=options)
        print(f"{font.family()=} {font.pointSize()=} {font.weight()=}")

        if ok:
            # Apply the selected font to the label
            style = f"{{font-family: '{font.family()}';font-size: {font.pointSize()}pt;font-weight: {font.weight()};}}"
            print(style)
            self.label.setStyle(style)


if __name__ == "__main__":
    app = QApplication(sys.argv)

    # Set a global style for the app (Optional)
    app.setStyleSheet("QWidget { font-family: Arial; }")

    window = FontPickerApp()
    window.show()

    sys.exit(app.exec())
