from rich.console import Group
from rich.text import Text

from openhands.sdk.conversation.visualizer.default import (
    DefaultConversationVisualizer,
    DEFAULT_HIGHLIGHT_REGEX,
    EVENT_VISUALIZATION_CONFIG,
    indent_content,
    section_header,
)
from openhands.sdk.event import MessageEvent
from openhands.sdk.event.base import Event


class DarkTerminalVisualizer(DefaultConversationVisualizer):
    """Wizualizator z jasnymi kolorami dla czarnego tła."""

    def __init__(self):
        highlight = {
            k: v.replace("bright_black", "white")
            for k, v in DEFAULT_HIGHLIGHT_REGEX.items()
        }
        super().__init__(highlight_regex=highlight)

    def _create_event_block(self, event: Event) -> Group | None:
        config = EVENT_VISUALIZATION_CONFIG.get(type(event))
        if not config or config.skip:
            return None

        if (
            self._skip_user_messages
            and isinstance(event, MessageEvent)
            and event.llm_message
            and event.llm_message.role == "user"
        ):
            return None

        content = event.visualize
        if not content.plain.strip():
            return None

        if self._highlight_patterns:
            content = self._apply_highlighting(content)

        title = config.title(event) if callable(config.title) else config.title
        color = config.color(event) if callable(config.color) else config.color
        subtitle = self._format_metrics_subtitle() if config.show_metrics else None

        parts = [section_header(title, color), Text()]
        if config.indent_content:
            parts.append(indent_content(content))
        else:
            parts.append(content)
        if subtitle:
            parts.extend([Text(), Text.from_markup(subtitle)])  # bez .stylize("dim")
        parts.append(Text())
        return Group(*parts)
