# -*- coding: utf-8 -*-
# gui/components/kanban_card.py

"""Переиспользуемая карточка для канбан-доски."""

from typing import Optional

from PySide6.QtCore import Qt, Signal
from PySide6.QtWidgets import QWidget, QSizePolicy

from gui.containers.h_container import HContainer
from gui.containers.s_container import SContainer
from gui.containers.v_container import VContainer
from gui.components.label import Label


class KanbanCard(SContainer):
    """Одна карточка на канбан-доске."""

    card_clicked = Signal(object)

    def __init__(
        self,
        card_id: object,
        title: str = "",
        subtitle: str = "",
        status_color: str = "#DFE1E6",
        style: str = "TASK_CARD",
        parent: Optional[QWidget] = None,
    ):
        super().__init__(
            width_percent=100,
            margin=[6, 4, 6, 4],
            style=style,
            content_fit=True,
            parent=parent,
        )
        self._card_id = card_id
        self._status_color = status_color
        self._build_ui(title, subtitle, status_color)

    @property
    def card_id(self) -> object:
        """Идентификатор карточки."""
        return self._card_id

    def set_title(self, text: str) -> None:
        """Установить текст заголовка."""
        self._title_label.set_text(text)

    def set_subtitle(self, text: str) -> None:
        """Установить текст подзаголовка."""
        self._subtitle_label.set_text(text)

    def set_status_color(self, color: str) -> None:
        """Установить цвет полосы состояния."""
        self._status_color = color
        self._status_strip.setStyleSheet(
            f"background-color: {color}; border: none; border-radius: 2px;"
        )

    def set_badge_text(self, text: str) -> None:
        """Установить текст бейджа состояния."""
        self._badge_label.set_text(text)
        self._badge_label.setVisible(bool(text))

    def set_badge_style(self, style_key: str) -> None:
        """Установить стиль бейджа (ключ APP_STYLES)."""
        self._badge_label.style(style_key=style_key)

    def _build_ui(self, title: str, subtitle: str, status_color: str) -> None:
        row = HContainer(parent=self)

        self._status_strip = QWidget()
        self._status_strip.setFixedWidth(4)
        self._status_strip.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Expanding)
        self._status_strip.setStyleSheet(
            f"background-color: {status_color}; border: none; border-radius: 2px;"
        )
        row.add_widget(self._status_strip)

        content = VContainer(margin=[6, 4, 4, 4], spacing=2, parent=row)

        self._badge_label = Label("", style="TASK_BADGE")
        self._badge_label.setVisible(False)
        self._badge_label.set_max_height(20)
        content.add_widget(self._badge_label)

        self._title_label = Label(title, style="TASK_CARD_TITLE")
        content.add_widget(self._title_label)

        self._subtitle_label = Label(subtitle, style="TASK_CARD_SUBTITLE")
        content.add_widget(self._subtitle_label)

    def mousePressEvent(self, event) -> None:
        """Обработка клика по карточке."""
        if event.button() == Qt.LeftButton:
            self.card_clicked.emit(self._card_id)
        super().mousePressEvent(event)
