# -*- coding: utf-8 -*-
"""
LVT Shared Infrastructure — Help Dialog.

Reusable help dialog component that displays module-specific
bilingual help content (English / Vietnamese).

Author: Lộc Vũ Trung (LVT) / Slow Forest
License: GPL-3.0
"""

from qgis.PyQt.QtCore import Qt
from qgis.PyQt.QtWidgets import (
    QDialog, QVBoxLayout, QHBoxLayout, QPushButton, QTextEdit,
    QApplication,
)

from .i18n import current_language, tr


class HelpDialog(QDialog):
    """Reusable help dialog for any LVT module.

    Displays HTML help content with bilingual support.

    Args:
        module_key: str, key into HELP_CONTENT dict.
        parent: Parent widget.
    """

    def __init__(self, module_key, parent=None):
        super().__init__(parent)
        self.module_key = module_key
        self.setWindowFlags(
            self.windowFlags() & ~Qt.WindowContextHelpButtonHint
        )

        screen = QApplication.primaryScreen()
        if screen:
            sh = int(screen.availableGeometry().height() * 0.75)
        else:
            sh = 550
        self.setMinimumSize(620, 450)
        self.resize(700, sh)

        self._setup_ui()
        self._refresh()

    def _setup_ui(self):
        """Build the help dialog UI."""
        layout = QVBoxLayout(self)
        layout.setSpacing(8)

        # Help content area
        self.txt_help = QTextEdit()
        self.txt_help.setReadOnly(True)
        self.txt_help.setStyleSheet(
            "QTextEdit { background: #FAFAFA; border: 1px solid #ddd; "
            "border-radius: 4px; padding: 8px; }"
        )
        layout.addWidget(self.txt_help)

        # Bottom buttons
        btn_layout = QHBoxLayout()
        btn_layout.addStretch()
        self.btn_close = QPushButton(tr("Close"))
        self.btn_close.setStyleSheet(
            "padding: 6px 20px; border-radius: 4px;"
        )
        self.btn_close.clicked.connect(self.close)
        btn_layout.addWidget(self.btn_close)
        layout.addLayout(btn_layout)

    def _refresh(self):
        """Load help content for current language."""
        from .help_content import get_help_html
        lang = current_language()
        self.setWindowTitle(
            "LVT4U — " + (
                "Hướng dẫn" if lang == "vi" else "Help"
            )
        )
        self.btn_close.setText(tr("Close"))
        self.txt_help.setHtml(get_help_html(self.module_key, lang))
