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

"""Просмотрщик документа Ticket."""

from __future__ import annotations

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

from domain import TicketDocumentSnapshot


class ReportViewer(Dialog):
    """Простой viewer сохранённого документа Ticket."""

    def __init__(
        self,
        document: TicketDocumentSnapshot,
        parent=None,
    ):
        self._document = document
        self._close_button: Button | None = None
        super().__init__(
            title=document.title,
            width=720,
            height=760,
            modal=True,
            parent=parent,
        )
        self._setup_ui()
        self._connect_signals()

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

        title_label = Label(
            self._document.title,
            alignment="left",
            style="TICKET_LIST_HEADER",
        )
        metadata_label = Label(
            (
                f"Задача #{self._document.payload.get('task_id', self._document.task_id)}\n"
                f"{self._document.created_at.strftime('%d.%m.%Y %H:%M')}\n"
                f"{self._document.location or 'Локация не указана'}"
            ),
            alignment="left",
            style="TICKET_LIST_SUBTITLE",
        )
        viewer = TextInput(
            text=self._document.content or self._document.summary,
            style="TICKET_PREVIEW_AREA",
            multiline=True,
        )
        viewer.set_read_only(True)
        self._close_button = Button("Закрыть", style="FILTER_BUTTON", content_fit=True)

        main_container.add_widget(title_label)
        main_container.add_widget(metadata_label)
        main_container.add_widget(viewer)
        main_container.add_widget(self._close_button)

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