import 'package:chat/configs/app_colors.dart';
import 'package:chat/services/tim/group_service.dart';
import 'package:chat/widgets/custom_avatar.dart';
import 'package:flutter/material.dart';
import 'package:tencent_im_sdk_plugin/models/v2_tim_group_member_full_info.dart';

class GroupAvatar extends StatefulWidget {
  final String groupID;
  final double size;
  const GroupAvatar(
    this.groupID, {
    this.size = 44,
    Key? key,
  }) : super(key: key);

  @override
  State<GroupAvatar> createState() => _GroupAvatarState();
}

class _GroupAvatarState extends State<GroupAvatar> {
  List<V2TimGroupMemberFullInfo?>? members;

  @override
  void initState() {
    super.initState();

    TimGroupService.to.members(widget.groupID, count: 9).then((value) {
      setState(() {
        members = value;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: widget.size,
      height: widget.size,
      decoration: BoxDecoration(
        border: Border.all(
          color: AppColors.border,
          width: 0.4,
        ),
        borderRadius: BorderRadius.circular(4),
      ),
      child: members == null
          ? CustomAvatar('')
          : GridView.builder(
              padding: const EdgeInsets.all(1),
              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount:
                    (members != null && members!.length > 4) ? 3 : 2,
                childAspectRatio: 1,
                crossAxisSpacing: 1,
                mainAxisSpacing: 1,
              ),
              itemCount: members!.length,
              itemBuilder: (context, index) {
                return CustomAvatar(
                  members![index]?.faceUrl,
                  size: widget.size / 3,
                  radius: 2,
                );
              },
            ),
    );
  }
}
