from pathlib import Path

from pyqt_compat import QtWidgets


class HelpMixin:

    def _init_help(self, layout: QtWidgets.QVBoxLayout) -> QtWidgets.QWidget:
        self.help_widget = QtWidgets.QWidget()
        self.help_layout = QtWidgets.QVBoxLayout()
        self.help_widget.setLayout(self.help_layout)
        layout.addWidget(self.help_widget)

        self.help_browser = QtWidgets.QTextBrowser()
        self.help_browser.setHtml(self.load_help_text())
        self.help_browser.document().setDocumentMargin(4) # type: ignore
        self.help_layout.addWidget(self.help_browser)

        self.help_widget.hide()
        return self.help_widget

    def load_help_text(self) -> str:
        f = Path(__file__).parent / 'help_text' / 'version_manage_help_text.html'
        if not f.exists():
            return ''
        with f.open() as fp:
            return fp.read().replace('\n', '<br>')
