"""BubbleDelegate: how the transcript draws a message.

Incoming messages sit on the left under the peer's avatar, outgoing ones on
the right. Messages of one turn (same direction, same day, less than five
minutes apart) are grouped: the avatar and the sender's name only at the start
of the turn, and less space between them. Text is laid out with QTextDocument:
HTML a peer sent is sanitised, plain text is escaped and its links made
clickable. Texts a client writes on its own are centred notes; an encrypted
body says so; anything else is summarised in a bubble until it gets a bubble
of its own (docs/messaging/ui-plan.md, B4). The first message of a day has the
day above it (Today, Yesterday, the weekday, the date); an outgoing message
shows its delivery state after its time and a failed one is drawn in red.
A reply starts with a quote of what it answers (who, and the first line);
clicking the quote goes to that message. A message being pointed out is
flashed (flash()). Under the mouse a bubble shows its actions button (three dots, beside it on
the side facing the middle), which opens the same menu as a right click.
A picture is drawn inline once it is here (MediaCache decodes it off the GUI
thread): at most 320 pixels high, 640 for a large source, at least 120 wide,
with the time over its corner and the caption under it; until then a box of the
same kind says what it is and how far the download got. Any other file is its
icon, its name and "type · size" (with how far the download got, or in red why
it failed); a PDF that is here shows its first page, with "PDF · N pages ·
size" over it. A call is an arrow for its direction (red for one that wants
attention: missed, rejected, failed), what happened ("Missed video call") and
"duration — reason"; a click opens the call's details. Audio that is here (with
QtMultimedia) is a player: play/pause, 48 bars of waveform (the sender's peaks,
else measured from the file) that fill as it plays and seek on a click or a
drag, the position and the length, and a title (a recording's, else the name).
A movie that is here is its poster (blink.messagepane.video) with a play badge
and its length; a click plays it in the bubble (AudioPlayer, one clip at a time)
with a transport over its bottom (play/pause, a track to seek on, the clock);
one GStreamer cannot read is a plain file, Open gives it to the system player.
A location is a map (blink.messagepane.locations: OpenStreetMap tiles) with a
pin where it is, a live share's trail and a meet-up's meeting point, and under
it what it is (Location, Live location, Meet-up) and how it stands (until,
ended, how many points, last update); a click opens the map window.
Layouts are cached per message, width and font.
"""

import os

from datetime import date, timedelta

from PyQt6.QtCore import Qt, QLocale, QPointF, QRectF, QSize, QSizeF
from PyQt6.QtGui import QAbstractTextDocumentLayout, QColor, QFont, QFontMetricsF, QPainter, QPainterPath, QPalette, QTextCharFormat, QTextCursor, QTextDocument, QTextOption, QPen
from PyQt6.QtWidgets import QStyle, QStyledItemDelegate

from blink.contact_mangler import mangled_name
from blink.messagepane.format import bubble_kind, day_label, delivery_mark, linkify, own_location_request, plain_summary, sanitize_html
from blink.widgets.color import is_dark_theme, secondary_text_color


__all__ = ['BubbleDelegate']


class BubbleLayout(object):
    __slots__ = ('kind', 'run_start', 'day_text', 'document', 'text_size', 'bubble_size', 'size', 'time_text', 'mark', 'mark_kind', 'name_text', 'quote_name', 'quote_text', 'quote_height',
                 'image_path', 'image_size', 'file_name', 'file_meta', 'file_note', 'file_error', 'file_icon',
                 'call_arrow', 'call_title', 'call_detail', 'call_attention', 'audio_title')


class BubbleDelegate(QStyledItemDelegate):
    margin = 10             # left and right of the transcript
    avatar_size = 28
    avatar_gap = 6
    padding_h = 10
    padding_v = 6
    radius = 12
    run_gap = 10            # space above the first message of a turn
    inner_gap = 2           # space between messages of a turn
    turn_interval = timedelta(minutes=5)
    max_bubble_width = 560
    divider_height = 30

    def __init__(self, parent=None):
        super().__init__(parent)
        self.peer_avatar = None         # callable(painter, rect): draws the peer's avatar
        self.sender_avatar = None       # callable(painter, rect, item): draws the avatar of a message's sender, before peer_avatar
        self.sender_name = None         # callable(item): the name over a turn of a message's sender, instead of its display_name
        self.group_by_sender = False    # a conference: a turn is one participant's (blink.messagepane.chat.sender_key)
        self.progress_of = None         # callable(message id) -> download fraction or None
        self._cache = {}

    def clear_cache(self):
        self._cache.clear()

    # Layout

    @staticmethod
    def _item(index):
        return index.data(Qt.ItemDataRole.UserRole)

    def _neighbour(self, index, offset):
        row = index.row() + offset
        model = index.model()
        if 0 <= row < model.rowCount():
            return self._item(model.index(row, 0))
        return None

    def is_run_start(self, index):
        item, previous = self._item(index), self._neighbour(index, -1)
        if previous is None or previous.direction != item.direction:
            return True
        if bubble_kind(previous) == 'note':
            return True
        if self.group_by_sender:
            from blink.messagepane.chat import sender_key
            if sender_key(previous) != sender_key(item):
                return True     # another participant of the conference
        if previous.timestamp.astimezone().date() != item.timestamp.astimezone().date():
            return True
        return item.timestamp - previous.timestamp > self.turn_interval

    def day_text(self, index):
        """The divider label when this is the first message of its day, else ''."""
        item, previous = self._item(index), self._neighbour(index, -1)
        day = item.timestamp.astimezone().date()
        if previous is not None and previous.timestamp.astimezone().date() == day:
            return ''
        locale = QLocale()
        return day_label(day, date.today(), lambda number: locale.dayName(number, QLocale.FormatType.LongFormat),
                         lambda number: locale.monthName(number, QLocale.FormatType.LongFormat))

    def _small_font(self, font):
        small = QFont(font)
        if small.pointSizeF() > 0:
            small.setPointSizeF(max(small.pointSizeF() - 1.5, 6))
        return small

    def _bubble_width_limit(self, width):
        return max(120, min(self.max_bubble_width, int((width - 2 * self.margin - self.avatar_size - self.avatar_gap) * 0.78)))

    def layout(self, index, width, font):
        item = self._item(index)
        run_start = self.is_run_start(index)
        day_text = self.day_text(index)
        search_text = getattr(index.model(), 'search_text', '')
        reply = item.reply
        progress = self.progress_of(item.id) if self.progress_of is not None else None
        progress = None if progress is None else int(progress * 100)
        image_path = self.file_path(item) if item.category in ('image', 'other', 'audio', 'video') else None
        video_info = video_state = None
        if item.category == 'video' and image_path:
            from blink.messagepane.video import VideoProbe, video_available
            if video_available():
                video_info = VideoProbe.instance().info(image_path)
                video_state = 'pending' if video_info is None else 'ok' if video_info else 'bad'
        location_share = location_version = None
        if item.category == 'location':
            from blink.messagepane.locations import LocationStore
            store = LocationStore.instance()
            location_share = store.shown(item)
            location_version = (store.version(item.id), location_share is None)
        upload_key = (item.upload.get('stage'), None if item.upload.get('progress') is None else int(item.upload['progress'] * 100)) if item.upload is not None else None
        key = (image_path, video_state, location_version, item.caption, progress, upload_key, item.id, item.state, item.content_type, len(item.content or ''), width, font.key(), run_start, day_text, search_text,
               (reply['id'], reply['text']) if reply else None)
        layout = self._cache.get(key)
        if layout is not None:
            return layout
        if len(self._cache) > 4000:
            self._cache.clear()
        layout = BubbleLayout()
        layout.kind = kind = bubble_kind(item)
        layout.run_start = run_start
        layout.day_text = day_text
        layout.time_text = item.timestamp.astimezone().strftime('%H:%M')
        layout.mark, layout.mark_kind = delivery_mark(item)
        layout.quote_name = layout.quote_text = ''
        layout.quote_height = 0
        layout.image_path = layout.image_size = None
        if item.category == 'call':
            layout.kind = kind = 'call'
        elif item.category == 'image':
            layout.kind = kind = 'image'
        elif video_state in ('pending', 'ok'):
            layout.kind = kind = 'video'
        elif item.category == 'location' and location_share is not False:
            layout.kind = kind = 'location'
        elif item.category in ('other', 'audio', 'video'):
            from blink.messagepane.media import pdf_available
            from blink.messagepane.audio import audio_available
            # video is a file here until it has a player of its own
            if item.category == 'audio' and image_path and audio_available():
                layout.kind = kind = 'audio'
            else:
                layout.kind = kind = 'pdf' if image_path and image_path.lower().endswith('.pdf') and pdf_available() else 'file'
        if getattr(item, 'private', False) and kind != 'note':
            # every private bubble says so, and with whom, as Blink for macOS does
            if item.outgoing:
                layout.name_text = translate('message_pane', 'Private message to %s') % (getattr(item, 'recipient_name', '') or '?')
            else:
                sender = self.sender_name(item) if self.sender_name is not None else mangled_name(item.display_name or '', uri=item.uri or item.remote_uri or None)
                layout.name_text = translate('message_pane', 'Private message from %s') % sender
        elif run_start and not item.outgoing and kind != 'note':
            layout.name_text = self.sender_name(item) if self.sender_name is not None else mangled_name(item.display_name or '', uri=item.uri or item.remote_uri or None)
        else:
            layout.name_text = ''
        small = self._small_font(font)
        document = QTextDocument()
        document.setDocumentMargin(0)
        if kind == 'image':
            return self._image_layout(key, layout, item, image_path, width, font, small, progress)
        if kind == 'location':
            return self._location_layout(key, layout, item, location_share, width, font, small)
        if kind == 'video':
            # until it is probed, a 16:9 well (the poster replaces it a moment later)
            natural = video_info['size'] if video_info and video_info['size'].isValid() else QSize(16, 9)
            return self._image_layout(key, layout, item, image_path, width, font, small, progress, natural=natural)
        if kind == 'pdf':
            layout = self._image_layout(key, layout, item, image_path, width, font, small, progress, pdf=True)
            if layout.image_path:
                return layout
            layout.kind = kind = 'file'         # cannot be read: a plain file
        if kind == 'file':
            return self._file_layout(key, layout, item, image_path, width, font, small, progress)
        if kind == 'call':
            return self._call_layout(key, layout, item, width, font, small)
        if kind == 'audio':
            return self._audio_layout(key, layout, item, image_path, width, font, small)
        if kind == 'note':
            document.setDefaultFont(small)
            option = QTextOption(Qt.AlignmentFlag.AlignHCenter)
            option.setWrapMode(QTextOption.WrapMode.WrapAtWordBoundaryOrAnywhere)
            document.setDefaultTextOption(option)
            document.setPlainText(plain_summary(item))
            document.setTextWidth(width - 2 * self.margin - 40)
        else:
            document.setDefaultFont(font)
            option = QTextOption()
            option.setWrapMode(QTextOption.WrapMode.WrapAtWordBoundaryOrAnywhere)
            document.setDefaultTextOption(option)
            if kind == 'text':
                content = item.content if isinstance(item.content, str) else (item.content or b'').decode('utf-8', 'replace')
                document.setHtml(sanitize_html(content) if item.content_type == 'text/html' else linkify(content))
                from blink.messagepane.reactions import color_emoji
                color_emoji(document)
            elif kind == 'encrypted':
                document.setPlainText('🔒 ' + _('Encrypted message'))
            else:
                summary = plain_summary(item)
                if item.category in self.summary_icons:
                    summary = '\u2003 ' + summary      # room for the icon drawn in front
                if progress is not None:
                    summary += '  ' + (translate('message_pane', 'downloading %d%%') % progress if progress < 100 else translate('message_pane', 'downloaded'))
                document.setPlainText(summary)
            if search_text and kind == 'text':
                self._highlight(document, search_text)
            limit = self._bubble_width_limit(width) - 2 * self.padding_h
            document.setTextWidth(limit)
            ideal = document.idealWidth()
            if ideal < limit:
                document.setTextWidth(max(ideal, 1))
        layout.document = document
        text_size = document.size()
        layout.text_size = QSizeF(text_size.width(), text_size.height())
        if kind == 'note':
            layout.bubble_size = QSizeF(text_size.width(), text_size.height())
            height = text_size.height() + 2 * self.padding_v
        else:
            time_metrics = QFontMetricsF(small)
            time_width = time_metrics.horizontalAdvance(layout.time_text + ('  ' + layout.mark if layout.mark else ''))
            layout.quote_name = layout.quote_text = ''
            layout.quote_height = 0
            quote_width = 0
            if reply:
                limit = self._bubble_width_limit(width) - 2 * self.padding_h
                layout.quote_name = translate('message_pane', 'You') if reply['outgoing'] else (reply['name'] or translate('message_pane', 'Them'))
                layout.quote_text = time_metrics.elidedText(' '.join(str(reply['text']).split()), Qt.TextElideMode.ElideRight, limit - 14)
                quote_width = min(limit, max(time_metrics.horizontalAdvance(layout.quote_text), time_metrics.horizontalAdvance(layout.quote_name)) + 14)
                layout.quote_height = 2 * time_metrics.height() + 8 + 4      # two lines, padding, gap below
            bubble_width = max(text_size.width(), time_width, quote_width) + 2 * self.padding_h
            bubble_height = text_size.height() + time_metrics.height() + 2 * self.padding_v + layout.quote_height
            layout.bubble_size = QSizeF(bubble_width, bubble_height)
            height = bubble_height
            if layout.name_text:
                height += time_metrics.height() + 2
        height += self.run_gap if run_start else self.inner_gap
        if day_text:
            height += self.divider_height
        layout.size = QSize(width, int(height + 0.999))
        self._cache[key] = layout
        return layout

    @staticmethod
    def _highlight(document, text):
        """Mark every occurrence of a searched text (case-insensitive)."""
        highlight = QTextCharFormat()
        highlight.setBackground(QColor('#8a6d00') if is_dark_theme() else QColor('#ffe08a'))
        cursor = document.find(text, 0)
        while not cursor.isNull():
            cursor.mergeCharFormat(highlight)
            cursor = document.find(text, cursor)

    # Pictures

    image_padding = 4
    image_max_height = 320
    image_large_max_height = 640
    image_large_source = 1600       # pixels on the long side
    image_min_width = 120
    video_max_height = 360
    video_min_width = 240           # room for the transport
    image_placeholder = (220, 150)

    _paths = {}         # message id: local path or None

    def file_path(self, item):
        """Where the message's file is here, remembered until forget() (a download changed it)."""
        if item.upload is not None:
            from blink.messagepane.files import local_file
            return local_file(item)         # being sent: where it is sent from
        try:
            return self._paths[item.id]
        except KeyError:
            from blink.messagepane.files import local_file
            path = self._paths[item.id] = local_file(item)
            return path

    def forget(self, message_id):
        self._paths.pop(message_id, None)

    pdf_width = 260
    pdf_max_height = 360

    def _image_layout(self, key, layout, item, path, width, font, small, progress, pdf=False, natural=None):
        from blink.messagepane.media import MediaCache
        limit = self._bubble_width_limit(width) - 2 * self.image_padding
        if pdf:
            limit = min(limit, self.pdf_width)
        video = natural is not None
        if natural is None:
            natural = MediaCache.instance().natural_size(path) if path else None
        if pdf and not (natural is not None and natural.isValid() and natural.width() > 0):
            return layout
        if natural is not None and natural.isValid() and natural.width() > 0 and natural.height() > 0:
            if pdf or video:
                natural = natural.scaled(limit, 100000, Qt.AspectRatioMode.KeepAspectRatio)    # pages and movies are drawn to fit, never at their own size
            max_height = self.video_max_height if video else self.pdf_max_height if pdf else self.image_large_max_height if max(natural.width(), natural.height()) >= self.image_large_source else self.image_max_height
            scale = min(1.0, limit / natural.width(), max_height / natural.height())
            box = (max(1, round(natural.width() * scale)), max(1, round(natural.height() * scale)))
            layout.image_path = path
        else:
            box = (min(self.image_placeholder[0], limit), self.image_placeholder[1])
        layout.image_size = box
        bubble_width = max(box[0], self.video_min_width if video else self.image_min_width) + 2 * self.image_padding
        document = QTextDocument()
        document.setDocumentMargin(0)
        caption_height = 0
        text = item.caption
        note = upload_note(item.upload) if item.upload is not None and item.upload['state'] != 'uploaded' else ''
        if note:
            text = note
        if pdf:
            from blink.messagepane.files import file_info
            info = file_info(item) or {}
            text = (info.get('name') or os.path.basename(path)) + ('\n' + note if note else '')
        if not layout.image_path:
            # what the box stands for until the picture is here
            from blink.messagepane.format import file_name
            text = (file_name(item.content) or translate('message_pane', 'Picture')) + ('\n' + note if note else '\n' + (translate('message_pane', 'downloading %d%%') % progress) if progress is not None and progress < 100 else '')
        if text:
            document.setDefaultFont(font if layout.image_path else small)
            option = QTextOption(Qt.AlignmentFlag.AlignLeft if layout.image_path else Qt.AlignmentFlag.AlignHCenter)
            option.setWrapMode(QTextOption.WrapMode.WrapAtWordBoundaryOrAnywhere)
            document.setDefaultTextOption(option)
            document.setPlainText(text)
            document.setTextWidth(bubble_width - 2 * self.padding_h)
            if layout.image_path:
                caption_height = document.size().height() + self.padding_v + 2
        layout.document = document
        layout.text_size = QSizeF(document.size().width(), document.size().height())
        layout.bubble_size = QSizeF(bubble_width, box[1] + 2 * self.image_padding + caption_height)
        height = layout.bubble_size.height()
        if layout.name_text:
            height += QFontMetricsF(small).height() + 2
        height += self.run_gap if layout.run_start else self.inner_gap
        if layout.day_text:
            height += self.divider_height
        layout.size = QSize(width, int(height + 0.999))
        self._cache[key] = layout
        return layout

    # Files

    file_icon_size = 32
    file_min_width = 200

    _mime_icons = {}

    def _mime_icon(self, name):
        from PyQt6.QtCore import QMimeDatabase
        from PyQt6.QtGui import QIcon
        mime = QMimeDatabase().mimeTypeForFile(name, QMimeDatabase.MatchMode.MatchExtension)
        icon = self._mime_icons.get(mime.name())
        if icon is None:
            icon = QIcon.fromTheme(mime.iconName(), QIcon.fromTheme(mime.genericIconName()))
            if icon.isNull():
                from blink.resources import Resources, themed_icon
                icon = themed_icon(Resources.get('icons/paperclip.svg'), '#bdbdbd')
            self._mime_icons[mime.name()] = icon
        return icon, mime

    def _file_layout(self, key, layout, item, path, width, font, small, progress):
        from blink.messagepane.files import failure_reason, file_info
        from blink.messagepane.format import format_size
        info = file_info(item) or {'name': translate('message_pane', 'File'), 'size': None, 'type': ''}
        layout.file_name = info['name']
        layout.file_icon, mime = self._mime_icon(info['name'])
        kind_text = mime.comment() if not mime.isDefault() else (os.path.splitext(info['name'])[1].lstrip('.').upper() or translate('message_pane', 'File'))
        size = info['size'] or (os.path.getsize(path) if path and os.path.exists(path) else None)
        layout.file_meta = ' · '.join(part for part in (kind_text, format_size(size)) if part)
        layout.file_error = False
        layout.file_note = ''
        if item.upload is not None:
            if item.upload['state'] != 'uploaded':
                layout.file_note, layout.file_error = upload_note(item.upload), item.upload['state'] == 'failed'
        elif progress is not None and progress < 100:
            layout.file_note = translate('message_pane', 'downloading %d%%') % progress
        elif not path:
            reason = failure_reason(item)
            if reason:
                layout.file_note, layout.file_error = '⚠ ' + reason, True
        metrics, small_metrics = QFontMetricsF(font), QFontMetricsF(small)
        limit = self._bubble_width_limit(width) - 2 * self.padding_h - self.file_icon_size - 10
        text_width = min(limit, max(metrics.horizontalAdvance(layout.file_name), small_metrics.horizontalAdvance(layout.file_meta),
                                    small_metrics.horizontalAdvance(layout.file_note), small_metrics.horizontalAdvance(layout.time_text + '  ' + (layout.mark or ''))))
        layout.file_name = metrics.elidedText(layout.file_name, Qt.TextElideMode.ElideMiddle, limit)
        layout.text_size = QSizeF(text_width, metrics.height() + small_metrics.height() * (2 if layout.file_note else 1))
        bubble_width = max(self.file_min_width, text_width + self.file_icon_size + 10 + 2 * self.padding_h)
        bubble_height = 2 * self.padding_v + layout.text_size.height() + small_metrics.height()
        layout.bubble_size = QSizeF(bubble_width, bubble_height)
        height = bubble_height
        if layout.name_text:
            height += small_metrics.height() + 2
        height += self.run_gap if layout.run_start else self.inner_gap
        if layout.day_text:
            height += self.divider_height
        layout.size = QSize(width, int(height + 0.999))
        self._cache[key] = layout
        return layout

    # Calls

    def _call_layout(self, key, layout, item, width, font, small):
        from blink.message_envelopes import call_lines, call_needs_attention, call_record, this_device_id
        record = call_record(item.content, item.metadata)
        device_id = this_device_id()
        lines = call_lines(record, device_id) if record else None
        if lines is None:
            title, duration, phrase = plain_summary(item), '', ''
            layout.call_attention = False
        else:
            title, duration, phrase = lines
            layout.call_attention = call_needs_attention(record, device_id)
        layout.mark, layout.mark_kind = '', None        # a call record is not a message that was delivered
        direction = (record or {}).get('direction') or item.direction
        layout.call_arrow = '↗' if direction == 'outgoing' else '↙'
        layout.call_title = title
        layout.call_detail = ' — '.join(part for part in (duration, phrase) if part)
        metrics, small_metrics = QFontMetricsF(font), QFontMetricsF(small)
        arrow_width = metrics.horizontalAdvance(layout.call_arrow + ' ')
        limit = self._bubble_width_limit(width) - 2 * self.padding_h
        text_width = min(limit, max(arrow_width + metrics.horizontalAdvance(title), small_metrics.horizontalAdvance(layout.call_detail),
                                    small_metrics.horizontalAdvance(layout.time_text + '  ' + (layout.mark or ''))))
        lines_height = metrics.height() + (small_metrics.height() if layout.call_detail else 0)
        layout.text_size = QSizeF(text_width, lines_height)
        layout.bubble_size = QSizeF(max(160, text_width + 2 * self.padding_h), 2 * self.padding_v + lines_height + small_metrics.height())
        height = layout.bubble_size.height()
        if layout.name_text:
            height += small_metrics.height() + 2
        height += self.run_gap if layout.run_start else self.inner_gap
        if layout.day_text:
            height += self.divider_height
        layout.size = QSize(width, int(height + 0.999))
        self._cache[key] = layout
        return layout

    # Audio

    audio_width = 300
    audio_button = 34
    audio_wave_height = 30
    audio_bars = 48

    def _audio_layout(self, key, layout, item, path, width, font, small):
        from blink.message_envelopes import recording_title
        from blink.messagepane.files import file_info
        info = file_info(item) or {}
        name = info.get('name') or os.path.basename(path)
        layout.image_path = path
        layout.audio_title = recording_title(name) or (translate('message_pane', 'Voice message') if item.peaks else name)
        small_metrics = QFontMetricsF(small)
        bubble_width = min(self._bubble_width_limit(width), self.audio_width)
        layout.bubble_size = QSizeF(bubble_width, 2 * self.padding_v + small_metrics.height() + max(self.audio_button, self.audio_wave_height) + 4 + small_metrics.height())
        height = layout.bubble_size.height()
        if layout.name_text:
            height += small_metrics.height() + 2
        height += self.run_gap if layout.run_start else self.inner_gap
        if layout.day_text:
            height += self.divider_height
        layout.size = QSize(width, int(height + 0.999))
        self._cache[key] = layout
        return layout

    def _audio_geometry(self, layout, bubble, small):
        """(button rect, waveform rect) inside an audio bubble."""
        top = bubble.top() + self.padding_v + QFontMetricsF(small).height() + 2
        row = max(self.audio_button, self.audio_wave_height)
        button = QRectF(bubble.left() + self.padding_h, top + (row - self.audio_button) / 2, self.audio_button, self.audio_button)
        wave = QRectF(button.right() + 10, top + (row - self.audio_wave_height) / 2, bubble.right() - self.padding_h - button.right() - 10, self.audio_wave_height)
        return button, wave

    def audio_hit(self, index, rect, position):
        """('play', None) on the button, ('seek', fraction) on the waveform, else None.
        A movie: ('play', None) on it, ('seek', fraction) on its track while it is the one playing."""
        item = self._item(index)
        if item is None or item.category not in ('audio', 'video'):
            return None
        font = self.parent().font()
        layout = self.layout(index, rect.width(), font)
        if layout.kind == 'video':
            from blink.messagepane.audio import AudioPlayer
            if item.upload is not None and item.upload['state'] == 'failed':
                return None             # a click retries the upload
            box = self._image_box(layout, self.bubble_rect(layout, item, QRectF(rect)))
            point = QPointF(position)
            if not box.contains(point):
                return None
            if AudioPlayer.instance().is_current(item.id):
                band, key, track = self._video_geometry(box)
                if track.adjusted(-2, -10, 2, 10).contains(point):
                    return 'seek', max(0.0, min(1.0, (point.x() - track.left()) / max(1.0, track.width())))
            return 'play', None
        if layout.kind != 'audio':
            return None
        button, wave = self._audio_geometry(layout, self.bubble_rect(layout, item, QRectF(rect)), self._small_font(font))
        point = QPointF(position)
        if button.adjusted(-4, -4, 4, 4).contains(point):
            return 'play', None
        if wave.adjusted(0, -6, 0, 6).contains(point):
            return 'seek', max(0.0, min(1.0, (point.x() - wave.left()) / max(1.0, wave.width())))
        return None

    def _paint_audio(self, painter, layout, item, bubble, small, secondary, text_colour):
        from blink.messagepane.audio import AudioInfo, AudioPlayer
        from blink.messagepane.format import format_clock, waveform_bars
        player = AudioPlayer.instance()
        fraction = player.fraction(item.id)
        playing = fraction is not None and player.playing
        info = AudioInfo.instance().get(layout.image_path)
        bars = waveform_bars(item.peaks, self.audio_bars) if item.peaks else (info[1] if info else [0.15] * self.audio_bars)
        duration = info[0] if info else None
        accent = QColor('#58a6ff') if is_dark_theme() else QColor('#1a73e8')
        small_metrics = QFontMetricsF(small)
        painter.setFont(small)
        painter.setPen(secondary)
        title_rect = QRectF(bubble.left() + self.padding_h, bubble.top() + self.padding_v, bubble.width() - 2 * self.padding_h, small_metrics.height())
        painter.drawText(title_rect, Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter, small_metrics.elidedText(layout.audio_title, Qt.TextElideMode.ElideRight, title_rect.width()))
        button, wave = self._audio_geometry(layout, bubble, small)
        circle = QPainterPath()
        circle.addEllipse(button)
        painter.fillPath(circle, accent)
        # the glyph: a play triangle with rounded corners, nudged right to look centred, or two rounded bars
        white = QColor('#ffffff')
        centre, side = button.center(), button.width() * 0.36
        if playing:
            painter.setPen(Qt.PenStyle.NoPen)
            painter.setBrush(white)
            bar_width, bar_height = side * 0.32, side * 1.05
            for offset in (-side * 0.42, side * 0.10):
                painter.drawRoundedRect(QRectF(centre.x() + offset, centre.y() - bar_height / 2, bar_width, bar_height), bar_width * 0.35, bar_width * 0.35)
        else:
            from PyQt6.QtGui import QPolygonF
            pen = QPen(white, max(1.0, side * 0.16))
            pen.setJoinStyle(Qt.PenJoinStyle.RoundJoin)
            painter.setPen(pen)
            painter.setBrush(white)
            x = centre.x() + side * 0.08
            painter.drawPolygon(QPolygonF([QPointF(x - side * 0.42, centre.y() - side * 0.55), QPointF(x - side * 0.42, centre.y() + side * 0.55),
                                           QPointF(x + side * 0.55, centre.y())]))
            painter.setPen(Qt.PenStyle.NoPen)
        step = wave.width() / len(bars)
        played = fraction or 0.0
        for number, value in enumerate(bars):
            height = max(2.0, value * wave.height())
            bar = QRectF(wave.left() + number * step + step * 0.2, wave.center().y() - height / 2, max(1.0, step * 0.6), height)
            painter.setBrush(accent if (number + 0.5) / len(bars) <= played else secondary)
            painter.drawRoundedRect(bar, bar.width() / 2, bar.width() / 2)
        clock = format_clock(duration) if duration else ''
        if fraction is not None:
            clock = format_clock(player.position()) + (' / ' + format_clock(duration) if duration else '')
        if item.upload is not None and item.upload['state'] != 'uploaded':
            clock = upload_note(item.upload)      # being sent: how far, in place of the length
        if clock:
            painter.setPen(secondary)
            painter.drawText(QRectF(wave.left(), bubble.bottom() - self.padding_v - small_metrics.height(), wave.width(), small_metrics.height()),
                             Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter, small_metrics.elidedText(clock, Qt.TextElideMode.ElideRight, wave.width()))

    def _paint_call(self, painter, layout, bubble, font, small, secondary, text_colour):
        metrics, small_metrics = QFontMetricsF(font), QFontMetricsF(small)
        attention = QColor('#ff7b72') if is_dark_theme() else QColor('#c62828')
        ok = QColor('#3fb950') if is_dark_theme() else QColor('#2e7d32')
        left, top = bubble.left() + self.padding_h, bubble.top() + self.padding_v
        width = bubble.width() - 2 * self.padding_h
        painter.setFont(font)
        painter.setPen(attention if layout.call_attention else ok)
        painter.drawText(QRectF(left, top, width, metrics.height()), Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter, layout.call_arrow)
        arrow_width = metrics.horizontalAdvance(layout.call_arrow + ' ')
        painter.setPen(attention if layout.call_attention else text_colour)
        painter.drawText(QRectF(left + arrow_width, top, width - arrow_width, metrics.height()), Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter,
                         metrics.elidedText(layout.call_title, Qt.TextElideMode.ElideRight, width - arrow_width))
        if layout.call_detail:
            painter.setFont(small)
            painter.setPen(secondary)
            painter.drawText(QRectF(left, top + metrics.height(), width, small_metrics.height()), Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter,
                             small_metrics.elidedText(layout.call_detail, Qt.TextElideMode.ElideRight, width))

    def _paint_file(self, painter, layout, item, bubble, font, small, secondary, text_colour):
        metrics, small_metrics = QFontMetricsF(font), QFontMetricsF(small)
        icon_box = QRectF(bubble.left() + self.padding_h, bubble.top() + self.padding_v + (metrics.height() + small_metrics.height() - self.file_icon_size) / 2,
                          self.file_icon_size, self.file_icon_size)
        layout.file_icon.paint(painter, icon_box.toRect())
        left = icon_box.right() + 10
        width = bubble.right() - self.padding_h - left
        top = bubble.top() + self.padding_v
        painter.setFont(font)
        painter.setPen(text_colour)
        painter.drawText(QRectF(left, top, width, metrics.height()), Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter, layout.file_name)
        top += metrics.height()
        painter.setFont(small)
        painter.setPen(secondary)
        painter.drawText(QRectF(left, top, width, small_metrics.height()), Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter, layout.file_meta)
        if layout.file_note:
            top += small_metrics.height()
            painter.setPen((QColor('#ff7b72') if is_dark_theme() else QColor('#c62828')) if layout.file_error else secondary)
            painter.drawText(QRectF(left, top, width, small_metrics.height()), Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter, layout.file_note)

    def _image_box(self, layout, bubble):
        box = QRectF(bubble.left() + self.image_padding, bubble.top() + self.image_padding, layout.image_size[0], layout.image_size[1])
        if bubble.width() - 2 * self.image_padding > box.width():
            box.moveLeft(bubble.left() + (bubble.width() - box.width()) / 2)
        return box

    # Locations

    location_size = (300, 190)

    def _location_layout(self, key, layout, item, share, width, font, small):
        from blink.messagepane.locations import share_detail, share_title
        box_width = min(self.location_size[0], self._bubble_width_limit(width) - 2 * self.image_padding)
        layout.image_size = (box_width, self.location_size[1])
        document = QTextDocument()
        document.setDocumentMargin(0)
        document.setDefaultFont(font)
        option = QTextOption()
        option.setWrapMode(QTextOption.WrapMode.WrapAtWordBoundaryOrAnywhere)
        document.setDefaultTextOption(option)
        import html as _html
        if share:
            detail = share_detail(share)
            document.setHtml(f'<span style="font-weight:500">{_html.escape(share_title(share))}</span>' + (f'<br><span style="font-size:small">{_html.escape(detail)}</span>' if detail else ''))
        else:
            document.setHtml(f'<span style="font-weight:500">{_html.escape(translate("message_pane", "Location"))}</span>')
        bubble_width = box_width + 2 * self.image_padding
        document.setTextWidth(bubble_width - 2 * self.padding_h)
        layout.document = document
        layout.text_size = QSizeF(document.size().width(), document.size().height())
        layout.bubble_size = QSizeF(bubble_width, layout.image_size[1] + 2 * self.image_padding + layout.text_size.height() + self.padding_v + 2)
        height = layout.bubble_size.height()
        if layout.name_text:
            height += QFontMetricsF(small).height() + 2
        height += self.run_gap if layout.run_start else self.inner_gap
        if layout.day_text:
            height += self.divider_height
        layout.size = QSize(width, int(height + 0.999))
        self._cache[key] = layout
        return layout

    def _paint_location(self, painter, layout, item, bubble, palette, small):
        from blink.messagepane.locations import LocationStore, fit_view, paint_map
        box = self._image_box(layout, bubble)
        share = LocationStore.instance().shown(item)
        clip = QPainterPath()
        clip.addRoundedRect(box, self.radius - 3, self.radius - 3)
        painter.save()
        painter.setClipPath(clip, Qt.ClipOperation.IntersectClip)     # within what the view repaints
        if share:
            track = [(lat, lng) for lat, lng, _ in share['track']]
            points = track + ([share['destination']] if share['destination'] else [])
            zoom, centre = fit_view(points, box.size().toSize())
            paint_map(painter, box, zoom, centre, track=track, pin=track[-1] if track else None, destination=share['destination'],
                      start=track[0] if track else None, accent=QColor('#1a73e8'))
        else:
            painter.fillRect(box, QColor(0, 0, 0, 30) if not is_dark_theme() else QColor(255, 255, 255, 24))
        painter.restore()
        metrics = QFontMetricsF(small)
        label = layout.time_text + ('  ' + layout.mark if layout.mark else '')
        pill = QRectF(0, 0, metrics.horizontalAdvance(label) + 12, metrics.height() + 4)
        pill.moveBottomRight(box.bottomRight() - QPointF(6, 6))
        pill_path = QPainterPath()
        pill_path.addRoundedRect(pill, pill.height() / 2, pill.height() / 2)
        painter.fillPath(pill_path, QColor(0, 0, 0, 120))
        painter.setFont(small)
        painter.setPen(QColor('#ffffff'))
        painter.drawText(pill, Qt.AlignmentFlag.AlignCenter, label)
        context = QAbstractTextDocumentLayout.PaintContext()
        context.palette = self._text_palette(palette, self._colours(palette, item.outgoing)[1])
        painter.save()
        painter.translate(bubble.left() + self.padding_h, box.bottom() + self.padding_v)
        layout.document.documentLayout().draw(painter, context)
        painter.restore()

    # Movies

    transport_height = 34
    badge_size = 54
    badge_min = 26

    def _video_geometry(self, box):
        """(transport band, play key, track) over the bottom of a movie's box."""
        band = QRectF(box.left(), box.bottom() - self.transport_height, box.width(), self.transport_height)
        key = QRectF(band.left() + 8, band.top() + (band.height() - 22) / 2, 22, 22)
        clock_width = 84
        track = QRectF(key.right() + 10, band.center().y() - 3, max(10.0, band.right() - 10 - clock_width - key.right() - 10), 6)
        return band, key, track

    def _paint_play_glyph(self, painter, centre, side, playing):
        from PyQt6.QtGui import QPolygonF
        painter.setPen(Qt.PenStyle.NoPen)
        painter.setBrush(QColor('#ffffff'))
        if playing:
            for offset in (-side * 0.45, side * 0.15):
                painter.drawRect(QRectF(centre.x() + offset, centre.y() - side * 0.55, side * 0.3, side * 1.1))
        else:
            painter.drawPolygon(QPolygonF([QPointF(centre.x() - side * 0.4, centre.y() - side * 0.6), QPointF(centre.x() - side * 0.4, centre.y() + side * 0.6),
                                           QPointF(centre.x() + side * 0.65, centre.y())]))

    def _paint_video_overlay(self, painter, layout, item, box, small):
        """The play badge and the length over a poster, or the transport over the movie playing."""
        from blink.messagepane.audio import AudioPlayer
        from blink.messagepane.format import format_clock
        from blink.messagepane.video import VideoProbe
        player = AudioPlayer.instance()
        info = VideoProbe.instance().info(layout.image_path) or {}
        duration = info.get('duration')
        metrics = QFontMetricsF(small)
        painter.save()
        if player.is_current(item.id):
            band, key, track = self._video_geometry(box)
            painter.fillRect(band, QColor(0, 0, 0, 140))
            self._paint_play_glyph(painter, key.center(), key.width() * 0.5, player.playing)
            fraction = player.fraction(item.id) or 0.0
            painter.setBrush(QColor(255, 255, 255, 90))
            painter.drawRoundedRect(track, 3, 3)
            painter.setBrush(QColor('#ffffff'))
            painter.drawRoundedRect(QRectF(track.left(), track.top(), max(track.height(), track.width() * fraction), track.height()), 3, 3)
            length = player.player.duration() / 1000.0 or duration
            clock = format_clock(player.position()) + (' / ' + format_clock(length) if length else '')
            painter.setFont(small)
            painter.setPen(QColor('#ffffff'))
            painter.drawText(QRectF(track.right() + 8, band.top(), band.right() - track.right() - 14, band.height()), Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter, clock)
        else:
            size = min(self.badge_size, max(min(box.width(), box.height()) * 0.28, self.badge_min))
            disc = QRectF(box.center().x() - size / 2, box.center().y() - size / 2, size, size)
            circle = QPainterPath()
            circle.addEllipse(disc)
            painter.fillPath(circle, QColor(0, 0, 0, 130))
            painter.setPen(QColor(255, 255, 255, 160))
            painter.drawEllipse(disc)
            self._paint_play_glyph(painter, disc.center() + QPointF(size * 0.04, 0), size * 0.3, False)
            if duration:
                text = format_clock(duration)
                pill = QRectF(0, 0, metrics.horizontalAdvance(text) + 12, metrics.height() + 4)
                pill.moveBottomLeft(box.bottomLeft() + QPointF(6, -6))
                pill_path = QPainterPath()
                pill_path.addRoundedRect(pill, pill.height() / 2, pill.height() / 2)
                painter.fillPath(pill_path, QColor(0, 0, 0, 120))
                painter.setFont(small)
                painter.setPen(QColor('#ffffff'))
                painter.drawText(pill, Qt.AlignmentFlag.AlignCenter, text)
        painter.restore()

    def _paint_image(self, painter, layout, item, bubble, palette, small, secondary):
        from blink.messagepane.media import MediaCache
        box = self._image_box(layout, bubble)
        clip = QPainterPath()
        clip.addRoundedRect(box, self.radius - 3, self.radius - 3)
        pixmap = None
        if layout.image_path:
            ratio = painter.device().devicePixelRatioF() if painter.device() is not None else 1.0
            pixmap = MediaCache.instance().thumbnail(layout.image_path, (box.width() * ratio, box.height() * ratio))
        frame = None
        if layout.kind == 'video':
            from blink.messagepane.audio import AudioPlayer
            player = AudioPlayer.instance()
            frame = player.frame if player.is_current(item.id) else None
        painter.save()
        painter.setClipPath(clip)
        if frame is not None:
            painter.fillRect(box, QColor('#000000'))
            target = QSizeF(frame.size()).scaled(box.size(), Qt.AspectRatioMode.KeepAspectRatio)
            painter.setRenderHint(QPainter.RenderHint.SmoothPixmapTransform, True)
            painter.drawImage(QRectF(box.center().x() - target.width() / 2, box.center().y() - target.height() / 2, target.width(), target.height()), frame)
        elif pixmap is not None:
            if layout.kind == 'pdf':
                painter.fillRect(box, QColor('#ffffff'))       # pages are drawn without their paper
            painter.setRenderHint(QPainter.RenderHint.SmoothPixmapTransform, True)
            painter.drawPixmap(box, pixmap, QRectF(pixmap.rect()))
        else:
            painter.fillRect(box, QColor(0, 0, 0, 200) if layout.kind == 'video' else QColor(0, 0, 0, 30) if not is_dark_theme() else QColor(255, 255, 255, 24))
            if not layout.image_path:
                context = QAbstractTextDocumentLayout.PaintContext()
                context.palette = self._text_palette(palette, secondary)
                painter.translate(box.left() + (box.width() - layout.text_size.width()) / 2, box.top() + (box.height() - layout.text_size.height()) / 2)
                layout.document.documentLayout().draw(painter, context)
        painter.restore()
        if layout.kind == 'video':
            self._paint_video_overlay(painter, layout, item, box, small)
        # the time (and the delivery state) on a dark pill over the picture's corner (above the transport while it plays)
        metrics = QFontMetricsF(small)
        label = layout.time_text + ('  ' + layout.mark if layout.mark else '')
        pill = QRectF(0, 0, metrics.horizontalAdvance(label) + 12, metrics.height() + 4)
        pill.moveBottomRight(box.bottomRight() - QPointF(6, 6))
        if layout.kind == 'video':
            from blink.messagepane.audio import AudioPlayer
            if AudioPlayer.instance().is_current(item.id):
                pill.moveBottom(box.bottom() - self.transport_height - 6)
        pill_path = QPainterPath()
        pill_path.addRoundedRect(pill, pill.height() / 2, pill.height() / 2)
        painter.fillPath(pill_path, QColor(0, 0, 0, 120))
        painter.setFont(small)
        painter.setPen(QColor('#ffffff'))
        painter.drawText(pill, Qt.AlignmentFlag.AlignCenter, label)
        if layout.kind == 'pdf':
            from blink.messagepane.files import file_info
            from blink.messagepane.format import format_size
            from blink.messagepane.media import page_count
            pages = page_count(layout.image_path)
            info = file_info(item) or {}
            size = info.get('size') or (os.path.getsize(layout.image_path) if os.path.exists(layout.image_path) else None)
            parts = ['PDF', (translate('message_pane', '1 page') if pages == 1 else translate('message_pane', '%d pages') % pages) if pages else '', format_size(size)]
            text = ' · '.join(part for part in parts if part)
            badge = QRectF(0, 0, metrics.horizontalAdvance(text) + 12, metrics.height() + 4)
            badge.moveBottomLeft(box.bottomLeft() + QPointF(6, -6))
            badge_path = QPainterPath()
            badge_path.addRoundedRect(badge, badge.height() / 2, badge.height() / 2)
            painter.fillPath(badge_path, QColor(0, 0, 0, 120))
            painter.setPen(QColor('#ffffff'))
            painter.drawText(badge, Qt.AlignmentFlag.AlignCenter, text)
        if layout.image_path and (item.caption or layout.kind == 'pdf' or (item.upload is not None and item.upload['state'] != 'uploaded')):
            context = QAbstractTextDocumentLayout.PaintContext()
            context.palette = self._text_palette(palette, self._colours(palette, item.outgoing)[1])
            painter.save()
            painter.translate(bubble.left() + self.padding_h, box.bottom() + self.padding_v)
            layout.document.documentLayout().draw(painter, context)
            painter.restore()

    def sizeHint(self, option, index):
        width = option.rect.width() if option.rect.width() > 0 else self.parent().viewport().width()
        return self.layout(index, width, option.font).size

    # Geometry

    def bubble_rect(self, layout, item, rect):
        top = rect.top() + (self.run_gap if layout.run_start else self.inner_gap) + (self.divider_height if layout.day_text else 0)
        if layout.kind == 'note':
            width = layout.bubble_size.width()
            return QRectF(rect.left() + (rect.width() - width) / 2, top + self.padding_v, width, layout.bubble_size.height())
        if layout.name_text:
            top += QFontMetricsF(self._small_font(self.parent().font())).height() + 2
        width = layout.bubble_size.width()
        if item.outgoing:
            left = rect.right() - self.margin - width
        else:
            left = rect.left() + self.margin + self.avatar_size + self.avatar_gap
        return QRectF(left, top, width, layout.bubble_size.height())

    def text_origin(self, layout, bubble):
        if layout.kind == 'note':
            return bubble.topLeft()
        return QPointF(bubble.left() + self.padding_h, bubble.top() + self.padding_v + (layout.quote_height or 0))

    def quote_rect(self, layout, bubble):
        return QRectF(bubble.left() + self.padding_h, bubble.top() + self.padding_v, bubble.width() - 2 * self.padding_h, layout.quote_height - 4)

    def bubble_at(self, index, rect, position):
        """Whether a point of the view is on the bubble itself (not the space around it)."""
        item = self._item(index)
        if item is None:
            return False
        layout = self.layout(index, rect.width(), self.parent().font())
        return self.bubble_rect(layout, item, QRectF(rect)).contains(QPointF(position))

    def quote_at(self, index, rect, position):
        """The reply dict when a point of the view is on a reply's quote, else None."""
        item = self._item(index)
        if item is None or not item.reply:
            return None
        layout = self.layout(index, rect.width(), self.parent().font())
        if not layout.quote_height:
            return None
        bubble = self.bubble_rect(layout, item, QRectF(rect))
        return item.reply if self.quote_rect(layout, bubble).contains(QPointF(position)) else None

    flashed_id = None

    def flash(self, message_id):
        """Point a message out for a moment (a quote was clicked)."""
        from PyQt6.QtCore import QTimer
        self.flashed_id = message_id
        self.parent().viewport().update()
        QTimer.singleShot(1200, self._end_flash)

    def _end_flash(self):
        self.flashed_id = None
        self.parent().viewport().update()

    def anchor_at(self, index, rect, position):
        """The link under a point of the view (in viewport coordinates), or ''."""
        item = self._item(index)
        if item is None:
            return ''
        layout = self.layout(index, rect.width(), self.parent().font())
        if layout.kind != 'text':
            return ''
        origin = self.text_origin(layout, self.bubble_rect(layout, item, QRectF(rect)))
        return layout.document.documentLayout().anchorAt(QPointF(position) - origin) or ''

    # Selecting text in a bubble: (message id, anchor, position), character positions in its document

    text_selection = None

    def text_position(self, index, rect, position, exact=True):
        """The character of a text bubble under a point of the view, or None (not a text bubble, or,
        exact, not on its text). Not exact: the nearest character, for a drag going past the text."""
        item = self._item(index)
        if item is None:
            return None
        layout = self.layout(index, rect.width(), self.parent().font())
        if layout.kind != 'text':
            return None
        origin = self.text_origin(layout, self.bubble_rect(layout, item, QRectF(rect)))
        point = QPointF(position) - origin
        size = layout.text_size
        if exact:
            if not QRectF(0, 0, size.width(), size.height()).contains(point):
                return None
        else:
            if point.y() < 0:
                return 0
            if point.y() > size.height():
                return layout.document.characterCount() - 1
            point = QPointF(min(max(point.x(), 0.0), size.width()), point.y())
        found = layout.document.documentLayout().hitTest(point, Qt.HitTestAccuracy.FuzzyHit)
        return found if found >= 0 else None

    def text_range(self, index, rect, position, unit):
        """(start, end) of the word or block ('word', 'block') under a point of a text bubble, or None."""
        found = self.text_position(index, rect, position)
        if found is None:
            return None
        layout = self.layout(index, rect.width(), self.parent().font())
        cursor = QTextCursor(layout.document)
        cursor.setPosition(found)
        cursor.select(QTextCursor.SelectionType.WordUnderCursor if unit == 'word' else QTextCursor.SelectionType.BlockUnderCursor)
        start, end = cursor.selectionStart(), cursor.selectionEnd()
        if unit == 'block' and start > 0 and layout.document.characterAt(start) == ' ':
            start += 1      # BlockUnderCursor takes the break before the block along
        return (start, end) if end > start else None

    def selected_text(self, index, rect):
        """The selected text of a bubble, '' when none of it is selected."""
        item = self._item(index)
        selection = self.text_selection
        if item is None or selection is None or selection[0] != item.id or selection[1] == selection[2]:
            return ''
        layout = self.layout(index, rect.width(), self.parent().font())
        if layout.kind != 'text':
            return ''
        last = layout.document.characterCount() - 1
        cursor = QTextCursor(layout.document)
        cursor.setPosition(min(selection[1], last))
        cursor.setPosition(min(selection[2], last), QTextCursor.MoveMode.KeepAnchor)
        return cursor.selectedText().replace(' ', '\n').replace(' ', '\n').replace('\xa0', ' ')

    def _selection_for(self, layout, item, palette):
        selection = self.text_selection
        if selection is None or selection[0] != item.id or selection[1] == selection[2]:
            return None
        last = layout.document.characterCount() - 1
        cursor = QTextCursor(layout.document)
        cursor.setPosition(min(selection[1], last))
        cursor.setPosition(min(selection[2], last), QTextCursor.MoveMode.KeepAnchor)
        highlight = QAbstractTextDocumentLayout.Selection()
        highlight.cursor = cursor
        text_format = QTextCharFormat()
        text_format.setBackground(palette.color(QPalette.ColorRole.Highlight))
        text_format.setForeground(palette.color(QPalette.ColorRole.HighlightedText))
        highlight.format = text_format
        return highlight

    # Painting

    def _colours(self, palette, outgoing):
        dark = is_dark_theme()
        if outgoing:
            fill = QColor('#2f5b85') if dark else QColor('#d7ebff')
        else:
            fill = QColor('#3b3d40') if dark else QColor('#eef0f2')
        return fill, palette.color(QPalette.ColorRole.Text)

    @staticmethod
    def _text_palette(palette, colour):
        text_palette = QPalette(palette)
        text_palette.setColor(QPalette.ColorRole.Text, colour)
        text_palette.setColor(QPalette.ColorRole.Link, QColor('#9ccfff') if is_dark_theme() else palette.color(QPalette.ColorRole.Link))
        return text_palette

    def paint(self, painter, option, index):
        item = self._item(index)
        if item is None:
            return
        rect = QRectF(option.rect)
        layout = self.layout(index, option.rect.width(), option.font)
        palette = option.palette
        secondary = secondary_text_color(palette)
        small = self._small_font(option.font)
        painter.save()
        painter.setRenderHint(QPainter.RenderHint.Antialiasing, True)
        painter.setRenderHint(QPainter.RenderHint.TextAntialiasing, True)
        bubble = self.bubble_rect(layout, item, rect)
        if layout.day_text:
            self._paint_divider(painter, rect, layout.day_text, small, secondary)
        if self._cancellable(item):
            self._paint_cancel_button(painter, self.cancel_rect(layout, item, bubble))
        elif self.downloadable(item):
            self._paint_download_button(painter, self.cancel_rect(layout, item, bubble), secondary)
        context = QAbstractTextDocumentLayout.PaintContext()
        if layout.kind == 'note':
            if own_location_request(item):
                if option.state & QStyle.StateFlag.State_MouseOver:
                    self._paint_actions_button(painter, self.actions_rect(layout, item, bubble), secondary)
                if layout.mark:
                    self._paint_note_mark(painter, layout, bubble, small, secondary)
            context.palette = self._text_palette(palette, secondary)
            painter.translate(self.text_origin(layout, bubble))
            layout.document.documentLayout().draw(painter, context)
            painter.restore()
            return

        if layout.name_text:
            painter.setFont(small)
            painter.setPen(secondary)
            name_width = max(bubble.width(), 240)
            if item.outgoing:       # an outgoing private message: the label ends where the bubble does
                name_rect = QRectF(bubble.right() - self.padding_h - name_width, bubble.top() - QFontMetricsF(small).height() - 2, name_width, QFontMetricsF(small).height())
                painter.drawText(name_rect, Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter, layout.name_text)
            else:
                name_rect = QRectF(bubble.left() + self.padding_h, bubble.top() - QFontMetricsF(small).height() - 2, name_width, QFontMetricsF(small).height())
                painter.drawText(name_rect, Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter, layout.name_text)
        if layout.run_start and not item.outgoing and (self.sender_avatar or self.peer_avatar) is not None:
            avatar = QRectF(rect.left() + self.margin, bubble.top(), self.avatar_size, self.avatar_size)
            if self.sender_avatar is not None:
                self.sender_avatar(painter, avatar, item)
            else:
                self.peer_avatar(painter, avatar)

        fill, text_colour = self._colours(palette, item.outgoing)
        if getattr(item, 'private', False):
            fill = QColor('#2b4a32') if is_dark_theme() else QColor('#ccffcc')     # Blink for macOS: COLOR_PRIVATE
        if layout.mark_kind == 'failed':
            fill = QColor('#6e2b2b') if is_dark_theme() else QColor('#ffd9d6')
        path = QPainterPath()
        path.addRoundedRect(bubble, self.radius, self.radius)
        painter.fillPath(path, fill)
        if item.id == self.flashed_id:
            painter.fillPath(path, QColor(255, 200, 0, 110))
        if layout.kind == 'file':
            self._paint_file(painter, layout, item, bubble, option.font, small, secondary, text_colour)
        if layout.kind == 'call':
            self._paint_call(painter, layout, bubble, option.font, small, secondary, text_colour)
        if layout.kind == 'audio':
            self._paint_audio(painter, layout, item, bubble, small, secondary, text_colour)
        if layout.kind in ('image', 'pdf', 'video', 'location'):
            if layout.kind == 'location':
                self._paint_location(painter, layout, item, bubble, palette, small)
            else:
                self._paint_image(painter, layout, item, bubble, palette, small, secondary)
            if option.state & QStyle.StateFlag.State_MouseOver:
                self._paint_actions_button(painter, self.actions_rect(layout, item, bubble), secondary)
            painter.restore()
            return
        if layout.quote_height:
            quote = self.quote_rect(layout, bubble)
            quote_path = QPainterPath()
            quote_path.addRoundedRect(quote, 6, 6)
            painter.fillPath(quote_path, QColor(0, 0, 0, 22) if not is_dark_theme() else QColor(255, 255, 255, 26))
            accent = QColor('#58a6ff') if is_dark_theme() else QColor('#1a73e8')
            painter.fillRect(QRectF(quote.left(), quote.top() + 3, 3, quote.height() - 6), accent)
            metrics = QFontMetricsF(small)
            painter.setFont(small)
            painter.setPen(accent)
            painter.drawText(QRectF(quote.left() + 9, quote.top() + 3, quote.width() - 12, metrics.height()), Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter, layout.quote_name)
            painter.setPen(secondary)
            painter.drawText(QRectF(quote.left() + 9, quote.top() + 3 + metrics.height(), quote.width() - 12, metrics.height()), Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter, layout.quote_text)

        if layout.kind not in ('file', 'call', 'audio'):
            context.palette = self._text_palette(palette, text_colour if layout.kind == 'text' else secondary)
            if layout.kind == 'text':
                highlight = self._selection_for(layout, item, palette)
                if highlight is not None:
                    context.selections = [highlight]
            painter.save()
            origin = self.text_origin(layout, bubble)
            painter.translate(origin)
            painter.setClipRect(QRectF(0, 0, layout.text_size.width() + 1, layout.text_size.height() + 1))
            layout.document.documentLayout().draw(painter, context)
            painter.restore()
        if layout.kind == 'summary' and item.category in self.summary_icons:
            line = QFontMetricsF(option.font).height()
            box = QRectF(origin.x(), origin.y() + line * 0.025, line * 0.95, line * 0.95)
            self._summary_icon(item.category).paint(painter, box.toRect())

        painter.setFont(small)
        painter.setPen(secondary)
        time_rect = QRectF(bubble.left() + self.padding_h, bubble.bottom() - self.padding_v - QFontMetricsF(small).height(),
                           bubble.width() - 2 * self.padding_h, QFontMetricsF(small).height())
        if layout.mark:
            mark_colour = {'displayed': QColor('#3fb950') if is_dark_theme() else QColor('#2e7d32'),     # read: green
                           'failed': QColor('#ff7b72') if is_dark_theme() else QColor('#c62828')}.get(layout.mark_kind, secondary)
            painter.setPen(mark_colour)
            painter.drawText(time_rect, Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter, layout.mark)
            time_rect.setRight(time_rect.right() - QFontMetricsF(small).horizontalAdvance(layout.mark + '  '))
            painter.setPen(secondary)
        painter.drawText(time_rect, Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter, layout.time_text)
        if option.state & QStyle.StateFlag.State_MouseOver:
            self._paint_actions_button(painter, self.actions_rect(layout, item, bubble), secondary)
        painter.restore()

    # drawn in front of a summary line, for what fonts often lack a glyph for: (file, recoloured in a dark theme)
    summary_icons = {'other': ('icons/paperclip.svg', True), 'location': ('icons/location-pin.svg', False)}
    _icons = {}

    def _summary_icon(self, category):
        dark = is_dark_theme()
        icon = self._icons.get((category, dark))
        if icon is None:
            from PyQt6.QtGui import QIcon
            from blink.resources import Resources, themed_icon
            filename, themed = self.summary_icons[category]
            path = Resources.get(filename)
            icon = self._icons[(category, dark)] = themed_icon(path, '#bdbdbd') if themed else QIcon(path)
        return icon

    actions_size = 22

    def _paint_note_mark(self, painter, layout, bubble, small, secondary):
        """The delivery mark of an own location request: after its centred text, level with its middle."""
        metrics = QFontMetricsF(small)
        text_width = min(layout.document.idealWidth(), bubble.width())
        colour = {'displayed': QColor('#3fb950') if is_dark_theme() else QColor('#2e7d32'),
                  'failed': QColor('#ff7b72') if is_dark_theme() else QColor('#c62828')}.get(layout.mark_kind, secondary)
        painter.save()
        painter.setFont(small)
        painter.setPen(colour)
        painter.drawText(QRectF(bubble.center().x() + text_width / 2 + 6, bubble.center().y() - metrics.height() / 2,
                                metrics.horizontalAdvance(layout.mark) + 2, metrics.height()),
                         Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter, layout.mark)
        painter.restore()

    def actions_rect(self, layout, item, bubble):
        """Where the actions button of a bubble is: beside it, towards the middle, level with its top."""
        size = self.actions_size
        if layout.kind == 'note':
            # a centred note (an own location request): before its text, level with its middle
            text_width = min(layout.document.idealWidth(), bubble.width())
            return QRectF(bubble.center().x() - text_width / 2 - 6 - size, bubble.center().y() - size / 2, size, size)
        left = bubble.left() - 6 - size if item.outgoing else bubble.right() + 6
        return QRectF(left, bubble.top() + 2, size, size)

    def actions_at(self, index, rect, position):
        """Whether a point of the view (viewport coordinates) is on the actions button of this row."""
        item = self._item(index)
        if item is None:
            return False
        layout = self.layout(index, rect.width(), self.parent().font())
        if layout.kind == 'note' and not own_location_request(item):
            return False
        return self.actions_rect(layout, item, self.bubble_rect(layout, item, QRectF(rect))).contains(QPointF(position))

    @staticmethod
    def _cancellable(item):
        """A file being encrypted or uploaded: it has a Cancel button, as on mobile."""
        return item.upload is not None and item.upload['state'] == 'uploading'

    def cancel_rect(self, layout, item, bubble):
        """The Cancel button of an upload: beside the bubble, under where the actions button shows."""
        size = self.actions_size
        left = bubble.left() - 6 - size if item.outgoing else bubble.right() + 6
        return QRectF(left, bubble.top() + 2 + size + 6, size, size)

    def downloadable(self, item):
        """A file not here and not being downloaded: it has a Download button (and Download in its menu)."""
        if item is None or item.upload is not None or item.category not in ('image', 'audio', 'video', 'other'):
            return False
        if item.outgoing:
            return False        # our own file: we had it to start with
        from blink.messagepane.files import file_info
        if file_info(item) is None or self.file_path(item):
            return False
        from blink.message_envelopes import file_transfer_envelope
        meta = file_transfer_envelope(item.content) or {}
        if meta.get('msrp') and not (meta.get('room') and meta.get('hash')):
            return False        # came over MSRP one to one: nowhere to fetch it from
        return self.progress_of is None or self.progress_of(item.id) is None

    def download_at(self, index, rect, position):
        """Whether a point of the view is on the Download button of this row."""
        item = self._item(index)
        if not self.downloadable(item):
            return False
        layout = self.layout(index, rect.width(), self.parent().font())
        return self.cancel_rect(layout, item, self.bubble_rect(layout, item, QRectF(rect))).contains(QPointF(position))

    def _paint_download_button(self, painter, rect, colour):
        """A down arrow onto a line, in a circle."""
        painter.save()
        background = QColor(0, 0, 0, 40) if not is_dark_theme() else QColor(255, 255, 255, 40)
        path = QPainterPath()
        path.addEllipse(rect)
        painter.fillPath(path, background)
        pen = QPen(colour)
        pen.setWidthF(1.8)
        pen.setCapStyle(Qt.PenCapStyle.RoundCap)
        pen.setJoinStyle(Qt.PenJoinStyle.RoundJoin)
        painter.setPen(pen)
        x, w, top, h = rect.center().x(), rect.width(), rect.top(), rect.height()
        arrow_top, arrow_tip = top + h * 0.26, top + h * 0.62
        painter.drawLine(QPointF(x, arrow_top), QPointF(x, arrow_tip))
        painter.drawLine(QPointF(x - w * 0.16, arrow_tip - h * 0.16), QPointF(x, arrow_tip))
        painter.drawLine(QPointF(x + w * 0.16, arrow_tip - h * 0.16), QPointF(x, arrow_tip))
        painter.drawLine(QPointF(x - w * 0.2, top + h * 0.74), QPointF(x + w * 0.2, top + h * 0.74))
        painter.restore()

    def cancel_at(self, index, rect, position):
        """Whether a point of the view is on the Cancel button of an upload in this row."""
        item = self._item(index)
        if item is None or not self._cancellable(item):
            return False
        layout = self.layout(index, rect.width(), self.parent().font())
        return self.cancel_rect(layout, item, self.bubble_rect(layout, item, QRectF(rect))).contains(QPointF(position))

    def _paint_cancel_button(self, painter, rect):
        painter.save()
        red = QColor('#ff7b72') if is_dark_theme() else QColor('#c62828')
        background = QColor(red)
        background.setAlpha(40)
        path = QPainterPath()
        path.addEllipse(rect)
        painter.fillPath(path, background)
        pen = QPen(red)
        pen.setWidthF(1.8)
        pen.setCapStyle(Qt.PenCapStyle.RoundCap)
        painter.setPen(pen)
        inset = rect.width() * 0.32
        cross = rect.adjusted(inset, inset, -inset, -inset)
        painter.drawLine(cross.topLeft(), cross.bottomRight())
        painter.drawLine(cross.topRight(), cross.bottomLeft())
        painter.restore()

    def _paint_actions_button(self, painter, rect, colour):
        painter.save()
        background = QColor(0, 0, 0, 40) if not is_dark_theme() else QColor(255, 255, 255, 40)
        path = QPainterPath()
        path.addEllipse(rect)
        painter.fillPath(path, background)
        painter.setPen(Qt.PenStyle.NoPen)
        painter.setBrush(colour)
        radius = 1.8
        for offset in (-5, 0, 5):
            painter.drawEllipse(QPointF(rect.center().x() + offset, rect.center().y()), radius, radius)
        painter.restore()

    def _paint_divider(self, painter, rect, text, font, colour):
        """The day: its name in a rounded pill across a thin line, at the top of the row."""
        metrics = QFontMetricsF(font)
        middle = rect.top() + self.divider_height / 2 + 4
        width = metrics.horizontalAdvance(text) + 20
        height = metrics.height() + 6
        pill = QRectF(rect.left() + (rect.width() - width) / 2, middle - height / 2, width, height)
        line_colour = QColor(colour)
        line_colour.setAlpha(70)
        painter.setPen(line_colour)
        painter.drawLine(QPointF(rect.left() + self.margin, middle), QPointF(pill.left() - 6, middle))
        painter.drawLine(QPointF(pill.right() + 6, middle), QPointF(rect.right() - self.margin, middle))
        painter.setPen(colour)
        painter.setFont(font)
        painter.drawText(pill, Qt.AlignmentFlag.AlignCenter, text)


def upload_note(upload):
    """What a file being sent says under its name: encrypting, uploading and how far (as mobile
    shows it), or why it failed."""
    if upload['state'] == 'failed':
        return '⚠ ' + translate('message_pane', 'Not sent: %s (click to retry)') % (upload['reason'] or translate('message_pane', 'failed'))
    if upload.get('stage') == 'encrypting':
        return translate('message_pane', 'Encrypting…')
    progress = upload.get('progress')
    if progress is None:
        return translate('message_pane', 'Uploading…')
    return translate('message_pane', 'Uploading %d%%') % int(progress * 100)


def translate(context, text):
    from blink.util import translate as _translate
    return _translate(context, text)


def _(text):
    from blink.util import translate
    return translate('message_pane', text)
