import 'package:flutter/material.dart';

import '../../../../api/marianumcloud/talk/getParticipants/getParticipantsCache.dart';
import '../../../../api/marianumcloud/talk/getParticipants/getParticipantsResponse.dart';
import '../../../../api/marianumcloud/talk/room/getRoomResponse.dart';
import '../../../../widget/largeProfilePictureView.dart';
import '../../../../widget/loadingSpinner.dart';
import '../../../../widget/userAvatar.dart';
import '../talkNavigator.dart';
import 'participants/participantsListView.dart';

class ChatInfo extends StatefulWidget {
  final GetRoomResponseObject room;
  const ChatInfo(this.room, {super.key});

  @override
  State<ChatInfo> createState() => _ChatInfoState();
}

class _ChatInfoState extends State<ChatInfo> {
  GetParticipantsResponse? participants;

  @override
  void initState() {
    GetParticipantsCache(
      chatToken: widget.room.token,
      onUpdate: (GetParticipantsResponse data) {
        setState(() {
          participants = data;
        });
      }
    );
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    var isGroup = widget.room.type != GetRoomResponseObjectConversationType.oneToOne;
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.room.displayName),
      ),
      body: Center(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            const SizedBox(height: 30),
            GestureDetector(
              child: UserAvatar(
                id: isGroup ? widget.room.token : widget.room.name,
                isGroup: isGroup,
                size: 80,
              ),
              onTap: () {
                if(isGroup) return;
                TalkNavigator.pushSplitView(context, LargeProfilePictureView(widget.room.name));
              },
            ),
            const SizedBox(height: 30),
            Text(widget.room.displayName, textAlign: TextAlign.center, style: const TextStyle(fontSize: 30)),
            if(!isGroup) Text(widget.room.name),
            const SizedBox(height: 10),
            if(isGroup) Text(widget.room.description, textAlign: TextAlign.center),
            const SizedBox(height: 30),
            if(participants == null) const LoadingSpinner(),
            if(participants != null) ...[
              ListTile(
                leading: const Icon(Icons.supervised_user_circle),
                title: Text('${participants!.data.length} Teilnehmer'),
                trailing: const Icon(Icons.arrow_right),
                onTap: () => TalkNavigator.pushSplitView(context, ParticipantsListView(participants!)),
              ),
            ],
          ],
        ),
      ),
    );
  }
}
