import 'package:flutter/material.dart';

import '../../../../api/marianumcloud/talk/chat/get_chat_response.dart';
import '../../../../api/marianumcloud/talk/chat/rich_object_string_processor.dart';
import '../data/chat_bubble_styles.dart';
import '../data/talk_markdown.dart';

class AnswerReference extends StatelessWidget {
  final GetChatResponseObject referenceMessage;
  final String? selfId;
  const AnswerReference({
    required this.referenceMessage,
    required this.selfId,
    super.key,
  });

  @override
  Widget build(BuildContext context) {
    final style = ChatBubbleStyles(context);
    final isSelf = referenceMessage.actorId == selfId;
    final accent = isSelf
        ? style.getSelfStyle().color!.withGreen(200)
        : style.getRemoteStyle().color!.withWhite(200);
    return DecoratedBox(
      decoration: BoxDecoration(
        color: accent.withValues(alpha: 0.2),
        borderRadius: const BorderRadius.all(Radius.circular(5)),
        border: Border(
          left: BorderSide(color: accent, width: 5),
        ),
      ),
      child: Padding(
        padding: const EdgeInsets.all(5).add(const EdgeInsets.only(left: 5)),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              referenceMessage.actorDisplayName,
              maxLines: 1,
              style: TextStyle(
                overflow: TextOverflow.ellipsis,
                color: accent,
                fontSize: 12,
              ),
            ),
            Text(
              () {
                final text = RichObjectStringProcessor.parseToString(
                  referenceMessage.message,
                  referenceMessage.messageParameters,
                );
                return referenceMessage.markdown
                    ? markdownToPlainText(text)
                    : text;
              }(),
              maxLines: 2,
              style: TextStyle(
                overflow: TextOverflow.ellipsis,
                color: Theme.of(context).colorScheme.onSurface,
                fontSize: 12,
              ),
            ),
          ],
        ),
      ),
    );
  }
}
