# SPDX-FileCopyrightText: (c) TagStudio Contributors
# SPDX-License-Identifier: GPL-3.0-only


import typing

import structlog
from PySide6.QtCore import Qt, Signal
from PySide6.QtGui import QColor
from PySide6.QtWidgets import QPushButton, QVBoxLayout, QWidget

from tagstudio.core.library.alchemy.enums import TagColorEnum
from tagstudio.core.library.alchemy.models import TagColorGroup
from tagstudio.qt.models.palette import ColorType, get_tag_color
from tagstudio.qt.translations import Translations
from tagstudio.qt.views.stylesheets.stylesheets import (
    get_tag_border_color,
    get_tag_highlight_color,
    get_tag_text_color,
    tag_style,
)

if typing.TYPE_CHECKING:
    from tagstudio.core.library.alchemy.library import Library

logger = structlog.get_logger(__name__)


class TagColorPreview(QWidget):
    on_click = Signal()

    def __init__(
        self,
        library: "Library",
        tag_color_group: TagColorGroup | None,
    ) -> None:
        super().__init__()
        self.lib: Library = library
        self.tag_color_group = tag_color_group

        self.setCursor(Qt.CursorShape.PointingHandCursor)
        self.base_layout = QVBoxLayout(self)
        self.base_layout.setObjectName("baseLayout")
        self.base_layout.setContentsMargins(0, 0, 0, 0)

        self.button = QPushButton(self)
        self.button.setFlat(True)
        self.button.setMinimumSize(56, 28)
        self.button.setMaximumHeight(28)
        self.button.clicked.connect(self.on_click.emit)

        self.base_layout.addWidget(self.button)

        self.set_tag_color_group(tag_color_group)

    def set_tag_color_group(self, color_group: TagColorGroup | None):
        logger.info(
            "[TagColorPreview] Setting tag color",
            primary=color_group.primary if color_group else None,
            secondary=color_group.secondary if color_group else None,
        )
        self.tag_color_group = color_group

        if color_group:
            self.button.setText(color_group.name)
            self.button.setText(
                f"{color_group.name} ({self.lib.get_namespace_name(color_group.namespace)})"
            )
        else:
            self.button.setText(Translations["color.title.no_color"])

        primary_color = self._get_primary_color(color_group)
        border_color = (
            get_tag_border_color(primary_color)
            if not (color_group and color_group.secondary and color_group.color_border)
            else (QColor(color_group.secondary))
        )
        highlight_color = get_tag_highlight_color(
            primary_color
            if not (color_group and color_group.secondary)
            else QColor(color_group.secondary)
        )
        text_color: QColor
        if color_group and color_group.secondary:
            text_color = QColor(color_group.secondary)
        else:
            text_color = get_tag_text_color(primary_color, highlight_color)

        self.button.setStyleSheet(
            tag_style(primary_color, text_color, border_color, highlight_color)
        )
        # Add back the padding if the hint is generated while the button has focus (no padding)
        self.button.setMinimumWidth(
            self.button.sizeHint().width() + (16 if self.button.hasFocus() else 0)
        )

    def _get_primary_color(self, tag_color_group: TagColorGroup | None) -> QColor:
        primary_color = QColor(
            get_tag_color(ColorType.PRIMARY, TagColorEnum.DEFAULT)
            if not tag_color_group
            else tag_color_group.primary
        )

        return primary_color
