# -*- coding: utf-8 -*-
# hub/ticket/ui/details/task_stage_action_row.py

"""Clickable task stage row for Ticket details."""

from __future__ import annotations

from PySide6.QtCore import Qt, Signal, Slot
from PySide6.QtGui import QMouseEvent

from gui.components import Label
from gui.containers import HContainer
from gui.theme_bus import theme_bus

from ui.cards.task_card_pixmap_factory import load_tinted_icon_pixmap


class TaskStageActionRow(HContainer):
    """Row with icon, text and optional click action."""

    clicked = Signal(str)

    def __init__(self, stage_key: str, parent=None):
        super().__init__(
            margin=[4, 2, 4, 2],
            spacing=12,
            content_fit=True,
            parent=parent,
            style="TICKET_DETAILS_STAGE_ROW",
            active_style="TICKET_DETAILS_STAGE_ROW_ACTIVE",
            is_active=False,
        )
        self._stage_key = stage_key
        self._icon_path = ""
        self._theme = "dark" if self.palette().window().color().lightness() < 128 else "light"
        self._is_clickable = False
        self._is_highlighted = False
        self._icon_label: Label | None = None
        self._text_label: Label | None = None
        self._setup_ui()
        theme_bus.theme_changed.connect(self.set_theme)

    def _setup_ui(self) -> None:
        # Root-row этапа: держит иконку, текст этапа и свободное место для выравнивания строки.
        self._icon_label = Label("", style="TICKET_TASK_CARD_AVATAR_IMAGE")
        self._icon_label.set_fixed_size(22, 22)
        self._text_label = Label(
            "",
            alignment="left",
            style="TICKET_DETAILS_STAGE_TEXT_INACTIVE",
            active_style="TICKET_DETAILS_STAGE_TEXT_ACTIVE",
            is_active=False,
        )
        self.add_widget(self._icon_label)
        self.add_widget(self._text_label)
        self.add_stretch()

    def configure(
        self,
        text: str,
        icon_path: str,
        emphasized: bool,
        clickable: bool,
    ) -> None:
        self._icon_path = icon_path
        self._is_clickable = bool(clickable)
        self._is_highlighted = bool(emphasized) or self._is_clickable

        if self._text_label is not None:
            self._text_label.set_text(text)
            self._text_label.style(is_active=self._is_highlighted)

        self.style(is_active=self._is_clickable)
        self.setCursor(
            Qt.CursorShape.PointingHandCursor
            if self._is_clickable
            else Qt.CursorShape.ArrowCursor
        )
        self._refresh_icon()

    @Slot(str)
    def set_theme(self, theme: str) -> None:
        normalized_theme = (theme or "").strip().lower()
        if normalized_theme in {"dark", "light"}:
            self._theme = normalized_theme
        super().set_theme(theme)
        self._refresh_icon()

    def mousePressEvent(self, event: QMouseEvent) -> None:
        if self._is_clickable and event.button() == Qt.MouseButton.LeftButton:
            self.clicked.emit(self._stage_key)
        super().mousePressEvent(event)

    def _refresh_icon(self) -> None:
        if self._icon_label is None or not self._icon_path:
            return
        icon_width = self._icon_label.width() or 22
        icon_height = self._icon_label.height() or 22
        pixmap = load_tinted_icon_pixmap(
            self._icon_path,
            icon_width,
            icon_height,
            self._resolve_icon_color(),
            padding=2,
        )
        if pixmap is not None:
            self._icon_label.set_pixmap(pixmap)

    def _resolve_icon_color(self) -> str:
        if self._is_highlighted:
            return "#F5F7FA" if self._theme == "dark" else "#172B4D"
        return "#8C9BAB" if self._theme == "dark" else "#6B778C"
