# standard
from dataclasses import dataclass

# PyQGIS
from qgis.PyQt.QtWidgets import QLabel, QWidget


@dataclass
class Tag:
    """Dataclass to define tag with name color, and text color, border color"""

    name: str
    color: str | None = None
    text_color: str | None = None
    border_color: str | None = None
    tooltip: str | None = None


class TagLabel(QLabel):
    """
    QLabel to display tag with text and background color
    """

    def __init__(
        self,
        tag: Tag,
        parent: QWidget | None = None,
    ) -> None:
        super().__init__(tag.name, parent)

        border = (
            f"border: 1px solid {tag.border_color};"
            if tag.border_color is not None
            else ""
        )

        background_color = (
            f"background-color: {tag.color};" if tag.color is not None else ""
        )

        text_color = f"color: {tag.text_color};" if tag.text_color is not None else ""

        style: str = f"""
        QLabel {{
            {background_color};
            {text_color};
            {border}
            border-radius: 10px;
            padding: 1px 2px 1px 2px; margin-right:1px;
        }}
        """

        self.setStyleSheet(style)
        self.setContentsMargins(10, 2, 10, 2)

        if tag.tooltip:
            self.setToolTip(tag.tooltip)
