import 'package:chat/configs/app_colors.dart';
import 'package:chat/routes/contact_routes.dart';
import 'package:chat/routes/conversation_routes.dart';
import 'package:chat/services/tim/conversation_service.dart';
import 'package:chat/services/tim/group_service.dart';
import 'package:chat/views/home/widgets/group_avatar.dart';
import 'package:chat/widgets/custom_easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:get/get.dart';
import 'package:tencent_im_sdk_plugin/models/v2_tim_group_info.dart';

class ContactGroupPage extends StatefulWidget {
  const ContactGroupPage({Key? key}) : super(key: key);

  @override
  State<ContactGroupPage> createState() => _ContactGroupPageState();
}

class _ContactGroupPageState extends State<ContactGroupPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('群聊'),
        actions: [
          IconButton(
            icon: const Icon(Icons.search_outlined),
            onPressed: () {
              Get.toNamed(ContactRoutes.groupSearch);
            },
          ),
          IconButton(
            icon: const Icon(Icons.add_outlined),
            onPressed: () {
              Get.toNamed(ContactRoutes.groupCreate);
            },
          ),
        ],
      ),
      body: GetX<TimGroupService>(builder: (_) {
        return EasyRefresh(
          onRefresh: () async {
            _.fetchList();
          },
          header: CustomEasyRefresh.header,
          child: Column(
            children: [
              ListView.separated(
                shrinkWrap: true,
                physics: const ClampingScrollPhysics(),
                itemBuilder: (context, index) {
                  V2TimGroupInfo group = _.groups[index];
                  return ListTile(
                    onTap: () async {
                      var conversation = await TimConversationService.to
                          .getById('group_' + group.groupID);
                      Get.toNamed(
                        ConversationRoutes.index,
                        arguments: {
                          'conversation': conversation,
                        },
                      );
                    },
                    leading: GroupAvatar(group.groupID),
                    tileColor: AppColors.white,
                    title: Text(group.groupName!),
                    subtitle: Text('成员数: ${group.memberCount}'),
                  );
                },
                separatorBuilder: (context, index) {
                  return const Divider(
                    height: 0,
                    indent: 72,
                  );
                },
                itemCount: _.groups.length,
              ),
              const Divider(
                height: 0,
                indent: 72,
              ),
              Container(
                color: AppColors.white,
                height: 54,
                child: Center(
                  child: Text(
                    '${_.groups.length} 个群聊',
                    style: const TextStyle(
                      color: AppColors.unactive,
                    ),
                  ),
                ),
              ),
            ],
          ),
        );
      }),
    );
  }
}
