import 'package:collection/collection.dart';
import 'package:flutter/material.dart';

import '../../../../api/marianumcloud/talk/get_participants/get_participants_response.dart';
import '../../../../model/account_data.dart';
import '../../../../widget/user_avatar.dart';
import '../data/open_direct_chat.dart';

class ParticipantsListView extends StatelessWidget {
  final GetParticipantsResponse participantsResponse;

  /// Hide the per-participant direct-message shortcut: in a 1:1 chat the
  /// only other participant *is* the current chat — the shortcut would
  /// just navigate back to where the user already is.
  final bool showDirectMessageAction;

  const ParticipantsListView(
    this.participantsResponse, {
    this.showDirectMessageAction = true,
    super.key,
  });

  @override
  Widget build(BuildContext context) {
    String lastname(participant) =>
        participant.displayName.toString().split(' ').last;

    final participants = participantsResponse.data.sorted((a, b) {
      final typeComparison = a.participantType.index.compareTo(
        b.participantType.index,
      );
      if (typeComparison != 0) return typeComparison;
      return lastname(a).compareTo(lastname(b));
    });
    var groupedParticipants = participants.groupListsBy(
      (participant) => participant.participantType,
    );

    final selfId = AccountData().getUsername();
    return Scaffold(
      appBar: AppBar(title: const Text('Mitglieder')),
      body: ListView(
        children: [
          ...groupedParticipants.entries.map(
            (entry) => Column(
              children: [
                ListTile(
                  title: Text(entry.key.prettyName),
                  titleTextStyle: Theme.of(context).textTheme.titleMedium,
                ),
                ...entry.value.map((participant) {
                  final canDirectMessage =
                      showDirectMessageAction &&
                      participant.actorType == 'users' &&
                      participant.actorId != selfId;
                  return ListTile(
                    leading: UserAvatar(id: participant.actorId),
                    title: Text(participant.displayName),
                    subtitle: participant.statusMessage != null
                        ? Text(participant.statusMessage!)
                        : null,
                    trailing: canDirectMessage
                        ? IconButton(
                            tooltip: 'Private Nachricht',
                            onPressed: () => openOrCreateDirectChat(
                              context,
                              actorId: participant.actorId,
                              actorDisplayName: participant.displayName,
                            ),
                            icon: const Icon(Icons.textsms_outlined),
                          )
                        : null,
                  );
                }),
                Divider(),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
