import 'package:azlistview/azlistview.dart';
import 'package:chat/configs/app_colors.dart';
import 'package:chat/controllers/private_controller.dart';
import 'package:chat/models/im/contact_info_model.dart';
import 'package:chat/routes/contact_routes.dart';
import 'package:chat/services/tim/friend_service.dart';
import 'package:chat/utils/im_tools.dart';
import 'package:chat/widgets/custom_avatar.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class ContactPage extends StatelessWidget {
  const ContactPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('通讯录'),
        actions: [
          IconButton(
            onPressed: () {
              // Get.toNamed(ImRoutes.friendSearch);
            },
            icon: const Icon(Icons.search_outlined),
          ),
        ],
      ),
      body: GetX<TimFriendService>(builder: (_) {
        return AzListView(
          physics: const BouncingScrollPhysics(),
          data: _.contacts,
          itemCount: _.contacts.length,
          itemBuilder: (__, index) {
            ContactInfoModel info = _.contacts[index];
            if (info.tagIndex == '@') {
              return Column(
                children: [
                  ListTile(
                    onTap: () async {
                      if (info.route != null) {
                        Get.toNamed(info.route!);
                      }
                    },
                    leading: Container(
                      width: 40,
                      height: 40,
                      decoration: BoxDecoration(
                        color: info.color!,
                        borderRadius: BorderRadius.circular(4),
                      ),
                      child: Icon(
                        info.icon,
                        size: 26,
                        color: AppColors.white,
                      ),
                    ),
                    tileColor: AppColors.white,
                    title: Text(info.name),
                  ),
                  const Divider(
                    height: 0,
                    indent: 72,
                  ),
                ],
              );
            } else {
              return Column(
                children: [
                  ListTile(
                    onTap: () async {
                      await PrivateController.to.setCurrentFriend(
                        info.userID,
                      );
                      Get.toNamed(
                        ContactRoutes.friendProfile,
                      );
                    },
                    tileColor: AppColors.white,
                    leading: CustomAvatar(
                      info.friendInfo!.userProfile!.faceUrl,
                      size: 40,
                    ),
                    title: Text(info.name),
                  ),
                  const Divider(
                    height: 0,
                    indent: 72,
                  ),
                ],
              );
            }
          },
          susItemBuilder: (__, index) {
            ContactInfoModel model = _.contacts[index];
            if ('@' == model.getSuspensionTag()) {
              return Container();
            }
            return ImTools.susItem(
              context,
              model.getSuspensionTag(),
            );
          },
          indexBarData: SuspensionUtil.getTagIndexList(_.contacts).toList(),
          indexBarOptions: ImTools.indexBarOptions,
        );
      }),
    );
  }
}
