import 'package:azlistview/azlistview.dart';
import 'package:chat/configs/app_colors.dart';
import 'package:chat/models/im/contact_info_model.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';
import 'package:tencent_im_sdk_plugin/models/v2_tim_friend_info.dart';
import 'package:tencent_im_sdk_plugin/models/v2_tim_group_member_full_info.dart';

class FriendSelector extends StatefulWidget {
  final Function(List<V2TimFriendInfo>) onChanged;
  final List<V2TimGroupMemberFullInfo?>? lockedUsers;
  const FriendSelector({
    required this.onChanged,
    this.lockedUsers,
    Key? key,
  }) : super(key: key);

  @override
  State<FriendSelector> createState() => _FriendSelectorState();
}

class _FriendSelectorState extends State<FriendSelector> {
  /// 选中的好友列表
  List<V2TimFriendInfo> selectList =
      List<V2TimFriendInfo>.empty(growable: true);

  /// 选择列表改变的事件，更新选中列表
  _selectListChange(id) {
    setState(() {
      if (selectList.contains(id)) {
        selectList.remove(id);
      } else {
        selectList.add(id);
      }
    });
    widget.onChanged(selectList);
  }

  @override
  Widget build(BuildContext context) {
    return GetX<TimFriendService>(
      builder: (_) {
        return AzListView(
          data: _.contacts,
          itemCount: _.contacts.length,
          itemBuilder: (BuildContext context, int index) {
            ContactInfoModel info = _.contacts[index];
            return _contactItem(info);
          },
          susItemBuilder: (BuildContext context, int index) {
            ContactInfoModel model = _.contacts[index];
            return ImTools.susItem(
              context,
              model.getSuspensionTag(),
              susHeight: 32,
            );
          },
          indexBarData: SuspensionUtil.getTagIndexList(_.contacts).toList(),
          indexBarOptions: ImTools.indexBarOptions,
        );
      },
    );
  }

  Widget _contactItem(
    ContactInfoModel info,
  ) {
    bool isDisable = widget.lockedUsers == null
        ? false
        : widget.lockedUsers!
            .where(
              (e) => e!.userID == info.friendInfo!.userID,
            )
            .isNotEmpty;

    return Column(
      children: [
        GestureDetector(
          onTap: () {},
          child: Container(
            color: AppColors.white,
            padding: const EdgeInsets.symmetric(vertical: 4),
            child: GestureDetector(
              behavior: HitTestBehavior.opaque,
              onTap: isDisable
                  ? null
                  : () {
                      setState(() {
                        _selectListChange(info.friendInfo);
                      });
                    },
              child: Row(
                children: [
                  Radio<V2TimFriendInfo>(
                    groupValue:
                        selectList.contains(info.friendInfo) || isDisable
                            ? info.friendInfo
                            : null,
                    onChanged: isDisable
                        ? null
                        : (value) {
                            setState(() {
                              _selectListChange(info.friendInfo);
                            });
                          },
                    value: info.friendInfo!,
                    toggleable: true,
                  ),
                  CustomAvatar(
                    info.friendInfo!.userProfile!.faceUrl,
                    size: 32,
                    radius: 2,
                  ),
                  const SizedBox(
                    width: 16,
                  ),
                  Expanded(
                    child: Text(
                      info.name,
                      style: const TextStyle(
                        fontSize: 16,
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),
        ),
        const Divider(
          height: 0,
          indent: 92,
        ),
      ],
    );
  }
}
