import 'package:adaptive_dialog/adaptive_dialog.dart';
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/models/im/name_card_model.dart';
import 'package:chat/models/im/private_conversation_model.dart';
import 'package:chat/routes/contact_routes.dart';
import 'package:chat/services/tim/conversation_service.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_conversation.dart';

class ContactFriendRecommendFriendPage extends StatelessWidget {
  const ContactFriendRecommendFriendPage({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,
                ),
              ),
            ),
          ),
          InkWell(
            onTap: () {
              FocusScope.of(Get.context!).requestFocus(FocusNode());
              Get.offNamed(ContactRoutes.friendRecommendGroup);
            },
            child: Container(
              decoration: const BoxDecoration(
                color: AppColors.white,
              ),
              width: double.infinity,
              padding: const EdgeInsets.only(
                left: 16,
                top: 12,
                bottom: 12,
              ),
              child: const Text(
                '选择一个群',
                style: TextStyle(
                  fontSize: 16,
                ),
              ),
            ),
          ),
          Expanded(
            child: GetX<TimFriendService>(builder: (_) {
              return AzListView(
                physics: const ClampingScrollPhysics(),
                data: _.contacts,
                itemCount: _.contacts.length,
                itemBuilder: (__, index) {
                  ContactInfoModel info = _.contacts[index];
                  return Column(
                    children: [
                      ListTile(
                        onTap: () async {
                          FocusScope.of(Get.context!).requestFocus(FocusNode());

                          V2TimConversation conversation =
                              await TimConversationService.to.getById(
                            'c2c_' + info.userID,
                          );

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