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

"""Простые диалоги Ticket на локальной GUI-библиотеке."""

from __future__ import annotations

from gui.components import Button, Dialog, Label, TextInput
from gui.containers import HContainer, VContainer


class TicketMessageDialog(Dialog):
    """Унифицированный диалог предупреждения или подтверждения."""

    def __init__(
        self,
        title: str,
        message: str,
        accept_text: str = "ОК",
        reject_text: str | None = None,
        parent=None,
    ):
        self._title = title
        self._message = message
        self._accept_text = accept_text
        self._reject_text = reject_text
        self._accept_button: Button | None = None
        self._reject_button: Button | None = None
        super().__init__(
            title=title,
            width=420,
            height=220,
            modal=True,
            parent=parent,
        )
        self._setup_ui()
        self._connect_signals()

    @classmethod
    def ask_confirmation(
        cls,
        parent,
        title: str,
        message: str,
        accept_text: str = "Подтвердить",
        reject_text: str = "Отмена",
    ) -> bool:
        dialog = cls(
            title=title,
            message=message,
            accept_text=accept_text,
            reject_text=reject_text,
            parent=parent,
        )
        return dialog.exec() == cls.DialogCode.Accepted

    @classmethod
    def show_warning(
        cls,
        parent,
        title: str,
        message: str,
    ) -> None:
        cls(
            title=title,
            message=message,
            accept_text="Закрыть",
            reject_text=None,
            parent=parent,
        ).exec()

    def _setup_ui(self) -> None:
        # Root-контейнер диалога сообщения: заголовок, текстовое поле и строка кнопок.
        main_container = VContainer(margin=16, spacing=12)
        self.add_widget(main_container)

        title_label = Label(
            self._title,
            alignment="left",
            style="TICKET_LIST_HEADER",
        )
        message_view = TextInput(
            text=self._message,
            style="TICKET_PREVIEW_AREA",
            multiline=True,
        )
        message_view.set_read_only(True)
        message_view.set_min_height(96)

        # Actions-row диалога: выравнивает кнопки подтверждения и, при необходимости, отмены.
        actions = HContainer(spacing=8, content_fit=True)
        actions.add_stretch()
        if self._reject_text is not None:
            self._reject_button = Button(
                self._reject_text,
                style="FILTER_BUTTON",
                content_fit=True,
            )
            actions.add_widget(self._reject_button)
        self._accept_button = Button(
            self._accept_text,
            style="FILTER_BUTTON_ACTIVE",
            content_fit=True,
        )
        actions.add_widget(self._accept_button)

        main_container.add_widget(title_label)
        main_container.add_widget(message_view)
        main_container.add_widget(actions)

    def _connect_signals(self) -> None:
        if self._accept_button is not None:
            self._accept_button.clicked.connect(self.accept)
        if self._reject_button is not None:
            self._reject_button.clicked.connect(self.reject)
