import 'package:adaptive_dialog/adaptive_dialog.dart';
import 'package:chat/configs/app_colors.dart';
import 'package:chat/controllers/private_controller.dart';
import 'package:chat/models/im/name_card_model.dart';
import 'package:chat/models/im/private_conversation_model.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:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tencent_im_sdk_plugin/models/v2_tim_conversation.dart';
import 'package:tencent_im_sdk_plugin/models/v2_tim_group_info.dart';

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

  @override
  Widget build(BuildContext context) {
    PrivateConversationModel card = PrivateController.to.currentFriend.value;

    return Scaffold(
      appBar: AppBar(
        title: const Text('选择一个群'),
      ),
      body: Column(
        children: [
          Container(
            color: AppColors.page,
            padding: const EdgeInsets.only(
              left: 16,
              right: 16,
              bottom: 8,
            ),
            child: Container(
              constraints: const BoxConstraints(
                maxHeight: 32,
              ),
              child: ClipRRect(
                borderRadius: BorderRadius.circular(32),
                child: TextField(
                  onChanged: (e) async {},
                  decoration: const InputDecoration(
                    hintText: '搜索',
                    hintStyle: TextStyle(
                      fontSize: 14,
                      color: AppColors.unactive,
                    ),
                    border: InputBorder.none,
                    focusedBorder: InputBorder.none,
                    fillColor: AppColors.white,
                    filled: true,
                    contentPadding: EdgeInsets.only(
                      bottom: 14,
                      left: 16,
                    ),
                  ),
                  cursorColor: AppColors.primary,
                ),
              ),
            ),
          ),
          Expanded(
            child: GetX<TimGroupService>(
              builder: (_) {
                return ListView.separated(
                  shrinkWrap: true,
                  physics: const ClampingScrollPhysics(),
                  itemBuilder: (context, index) {
                    V2TimGroupInfo group = _.groups[index];
                    return ListTile(
                      onTap: () async {
                        FocusScope.of(Get.context!).requestFocus(FocusNode());

                        V2TimConversation conversation =
                            await TimConversationService.to.getById(
                          'group_' + group.groupID,
                        );

                        OkCancelResult result = await showOkCancelAlertDialog(
                          style: AdaptiveStyle.iOS,
                          context: context,
                          title: '发送名片',
                          message: '确定要发送【个人名片】至${conversation.showName}？',
                          okLabel: '确定',
                          cancelLabel: '取消',
                          defaultType: OkCancelAlertDefaultType.cancel,
                        );

                        if (result == OkCancelResult.ok) {
                          var model = NameCardModel(
                            avatar: card.userProfile?.faceUrl ?? '',
                            userID: card.userID,
                            userName: card.userProfile?.nickName ?? '',
                          );

                          TimConversationService.to.sendCustomMessage(
                            conversation,
                            model,
                            'NAME_CARD',
                          );

                          Get.back();
                        }
                      },
                      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,
                );
              },
            ),
          ),
        ],
      ),
    );
  }
}
