import datetime

from PyQt6.QtCore import Qt
from PyQt6.QtWidgets import (
    QFontDialog,
    QLabel,
    QPushButton,
    QVBoxLayout,
    QWidget,
)


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)


def date_string_difference(date_str1, date_str2):
    """
    Compare two datetime strings and return the difference
    between the two in %H%M format
    """

    date_format = "%Y-%m-%d %H:%M:%S"

    dt1 = datetime.datetime.strptime(date_str1, date_format).astimezone(datetime.UTC)
    dt2 = datetime.datetime.strptime(date_str2, date_format).astimezone(datetime.UTC)
    diff = abs(dt1 - dt2)

    # Extract total seconds to calculate hours and minutes
    total_seconds = int(diff.total_seconds())
    hours = total_seconds // 3600
    minutes = (total_seconds % 3600) // 60
    return f"{hours:02d}{minutes:02d}"


if __name__ == "__main__":
    print(
        f"Result 1: {date_string_difference('2023-10-27 14:30:00', '2023-10-27 12:00:00')}"
    )
    print(
        f"Result 2: {date_string_difference('2023-10-27 23:59:00', '2023-10-28 01:01:00')}"
    )
    print(
        f"Result 3: {date_string_difference('2023-10-27 10:00:00', '2023-10-29 15:45:00')}"
    )

    # 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())
