"""Welcome tutorial dialog shown once after first successful license activation.

Displays all hotkeys and a brief description of each silent-mode feature.
Hidden from screen capture (CSCA/SEB compatible).
"""

from __future__ import annotations

import ctypes
import sys

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

_STYLE = """
QDialog {
    background-color: #1e1e1e;
    border: 1px solid #555;
    border-radius: 10px;
}
QLabel#title {
    color: #ffffff;
    font-size: 16px;
    font-weight: bold;
    padding: 4px 0 8px 0;
}
QLabel#hotkey {
    color: #5baaff;
    font-size: 13px;
    font-weight: bold;
    padding: 6px 0 0 0;
}
QLabel#desc {
    color: #c0c0c0;
    font-size: 12px;
    padding: 0 0 2px 12px;
}
QLabel#silent_note {
    color: #77dd77;
    font-size: 12px;
    padding: 10px 0 0 0;
}
QPushButton#start_btn {
    background-color: #2d6a4f;
    color: #ffffff;
    border: none;
    border-radius: 6px;
    padding: 10px 32px;
    font-size: 13px;
    font-weight: bold;
    margin-top: 14px;
}
QPushButton#start_btn:hover {
    background-color: #3a8f6a;
}
"""

_WDA_EXCLUDEFROMCAPTURE = 0x00000011


class WelcomeWindow(QDialog):
    """First-launch tutorial dialog."""

    def __init__(
        self,
        hotkey: str = "ctrl+shift+space",
        clipboard_hotkey: str = "ctrl+shift",
        page_solver_hotkey: str = "shift+shift",
    ) -> None:
        super().__init__(
            None, Qt.WindowType.FramelessWindowHint | Qt.WindowType.WindowStaysOnTopHint
        )
        self.setAttribute(Qt.WidgetAttribute.WA_DeleteOnClose)
        self.setFixedWidth(480)
        self.setStyleSheet(_STYLE)

        self._hotkey = hotkey.upper().replace("+", " + ")
        self._cb_hotkey = clipboard_hotkey.upper().replace("+", " + ")
        self._ps_hotkey = page_solver_hotkey.upper().replace("+", " + ")

        self._build_ui()
        self._center_on_screen()
        self._apply_stealth()

    def _build_ui(self) -> None:
        layout = QVBoxLayout(self)
        layout.setContentsMargins(28, 24, 28, 24)
        layout.setSpacing(0)

        title = QLabel("MuraveyChina — License activated!")
        title.setObjectName("title")
        title.setFont(QFont("Segoe UI", 16, QFont.Weight.Bold))
        layout.addWidget(title)

        def add_hotkey(key: str, desc: str) -> None:
            lbl_key = QLabel(key)
            lbl_key.setObjectName("hotkey")
            layout.addWidget(lbl_key)
            lbl_desc = QLabel(desc)
            lbl_desc.setObjectName("desc")
            lbl_desc.setWordWrap(True)
            layout.addWidget(lbl_desc)

        add_hotkey(
            self._hotkey,
            "Select a question with the mouse → press hotkey → answer is typed automatically",
        )
        add_hotkey(
            self._cb_hotkey,
            "Copy a question (Ctrl+C) → press hotkey → answer is typed automatically",
        )
        add_hotkey(
            self._ps_hotkey,
            "Double-tap to read the entire page and answer all questions at once",
        )

        note = QLabel(
            "Everything works SILENTLY — no windows, no popups.\nJust the answer typed where you need it."
        )
        note.setObjectName("silent_note")
        note.setWordWrap(True)
        layout.addWidget(note)

        btn = QPushButton("Start working")
        btn.setObjectName("start_btn")
        btn.clicked.connect(self.accept)
        btn.setDefault(True)
        layout.addWidget(btn, alignment=Qt.AlignmentFlag.AlignHCenter)

    def _center_on_screen(self) -> None:
        self.adjustSize()
        screen = QApplication.primaryScreen()
        if screen is None:
            return
        geom = screen.availableGeometry()
        x = geom.left() + (geom.width() - self.width()) // 2
        y = geom.top() + (geom.height() - self.height()) // 2
        self.move(x, y)

    def _apply_stealth(self) -> None:
        """Hide window from CSCA/SEB screen capture."""
        if sys.platform != "win32":
            return
        try:
            hwnd = int(self.winId())
            ctypes.windll.user32.SetWindowDisplayAffinity(hwnd, _WDA_EXCLUDEFROMCAPTURE)  # type: ignore[attr-defined]
        except (OSError, TypeError, ValueError, AttributeError):
            pass
