import 'package:adaptive_dialog/adaptive_dialog.dart';
import 'package:chat/configs/app_colors.dart';
import 'package:chat/configs/app_size.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/routes/contact_routes.dart';
import 'package:chat/services/tim/conversation_service.dart';
import 'package:chat/views/home/widgets/group_avatar.dart';
import 'package:chat/widgets/custom_avatar.dart';
import 'package:chat/widgets/custom_easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tencent_im_sdk_plugin/enum/conversation_type.dart';

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

  @override
  Widget build(BuildContext context) {
    PrivateConversationModel info = PrivateController.to.currentFriend.value;
    return GestureDetector(
      onTap: () {
        FocusScope.of(Get.context!).requestFocus(FocusNode());
      },
      child: Scaffold(
        appBar: AppBar(
          title: const Text(
            '选择一个聊天',
          ),
        ),
        body: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Padding(
              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.friendRecommendFriend);
              },
              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,
                  ),
                ),
              ),
            ),
            Container(
              padding: const EdgeInsets.only(
                left: 16,
                top: 6,
                bottom: 6,
              ),
              child: const Text(
                '最近联系人',
                style: TextStyle(
                  color: AppColors.unactive,
                  fontSize: AppSize.smallFontSize,
                ),
              ),
            ),
            Expanded(
              child: GetX<TimConversationService>(
                builder: (_) {
                  return _.conversationList.isEmpty
                      ? CustomEasyRefresh.empty()
                      : ListView.separated(
                          shrinkWrap: true,
                          physics: const ClampingScrollPhysics(),
                          itemCount: _.conversationList.length,
                          itemBuilder: (context, index) {
                            var conversation = _.conversationList[index]!;
                            return ListTile(
                              tileColor: AppColors.white,
                              onTap: () async {
                                FocusScope.of(Get.context!)
                                    .requestFocus(FocusNode());

                                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: info.userProfile?.faceUrl ?? '',
                                    userID: info.userID,
                                    userName: info.userProfile?.nickName ?? '',
                                  );

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

                                  Get.back();
                                }
                              },
                              leading: conversation.type ==
                                      ConversationType.V2TIM_C2C
                                  ? CustomAvatar(
                                      conversation.faceUrl,
                                    )
                                  : GroupAvatar(conversation.groupID!),
                              title: Text(
                                conversation.showName!,
                                style: const TextStyle(
                                  fontSize: 16,
                                ),
                              ),
                            );
                          },
                          separatorBuilder: (context, index) {
                            return const Divider(
                              height: 0,
                              indent: 72,
                            );
                          },
                        );
                },
              ),
            ),
          ],
        ),
      ),
    );
  }
}
