import re
import sqlite3

from qgis.PyQt import QtCore, QtGui, QtWidgets

from ..compat import DLGBTN_CANCEL, DLGBTN_OK, HORIZONTAL, MSGBOX_OK, SPSZ_EXPANDING, SPSZ_MINIMUM
from ..QRiS.settings import CONSTANTS


def validate_name(parent: QtWidgets.QWidget, txtName: QtWidgets.QLineEdit) -> bool:

    txtName.setText(txtName.text().strip())
    if len(txtName.text()) < 1:
        QtWidgets.QMessageBox.warning(parent, "Missing Name", "You must provide a name to continue.")
        txtName.setFocus()
        return False

    return True


def validate_name_unique(db_path: str, table: str, column: str, value: str) -> bool:

    with sqlite3.connect(db_path) as conn:
        curs = conn.cursor()
        curs.execute(f"SELECT * FROM {table} where {column} like ?", [value])  # nosec B608 - table and column are always hardcoded string literals passed by internal callers
        return curs.fetchone() is None


def add_standard_form_buttons(form: QtWidgets.QDialog, help_slug: str) -> QtWidgets.QHBoxLayout:

    form.horiz = QtWidgets.QHBoxLayout()

    form.cmdHelp = QtWidgets.QPushButton()
    form.cmdHelp.setText("Help")
    form.cmdHelp.setToolTip("Open online help documentation in browser")
    form.cmdHelp.setToolTipDuration(2000)
    help_url = CONSTANTS["webUrl"].rstrip("/") + "/software-help/" + help_slug.strip("/") if help_slug is not None and len(help_slug) > 0 else CONSTANTS
    form.cmdHelp.clicked.connect(lambda: QtGui.QDesktopServices.openUrl(QtCore.QUrl(help_url)))

    form.horiz.addWidget(form.cmdHelp)

    form.spacerItem = QtWidgets.QSpacerItem(40, 20, SPSZ_EXPANDING, SPSZ_MINIMUM)
    form.horiz.addItem(form.spacerItem)

    form.buttonBox = QtWidgets.QDialogButtonBox()
    form.horiz.addWidget(form.buttonBox)
    form.buttonBox.setOrientation(HORIZONTAL)
    form.buttonBox.setStandardButtons(DLGBTN_CANCEL | DLGBTN_OK)
    form.buttonBox.accepted.connect(form.accept)
    form.buttonBox.rejected.connect(form.reject)

    return form.horiz


def format_superscript(text: str) -> str:
    """Replaces numeric characters following a caret '^' in a string with their superscript equivalents."""
    if not text:
        return text

    def repl(match):
        digits = match.group(1)
        superscript_map = str.maketrans("0123456789", "⁰¹²³⁴⁵⁶⁷⁸⁹")
        return digits.translate(superscript_map)

    # Match a caret followed by one or more digits
    return re.sub(r"\^(\d+)", repl, text)


def add_help_button(form: QtWidgets.QDialog, help_slug: str, label: str = "Help") -> QtWidgets.QWidget:

    form.cmdHelp = QtWidgets.QPushButton()
    form.cmdHelp.setText(label)
    form.cmdHelp.setToolTip("Open online help documentation in browser")
    form.cmdHelp.setToolTipDuration(2000)
    help_url = CONSTANTS["webUrl"].rstrip("/") + "/software-help/" + help_slug.strip("/") if help_slug is not None and len(help_slug) > 0 else CONSTANTS
    form.cmdHelp.clicked.connect(lambda: QtGui.QDesktopServices.openUrl(QtCore.QUrl(help_url)))

    return form.cmdHelp


def message_box(title: str, message: str):
    msg = QtWidgets.QMessageBox()
    msg.setIcon(QtWidgets.QMessageBox.Warning)
    msg.setText(message)
    msg.setWindowTitle(title)
    msg.setStandardButtons(MSGBOX_OK)
    msg.setDefaultButton(MSGBOX_OK)
    msg.setEscapeButton(MSGBOX_OK)
    msg.exec()
