import 'dart:math';

import 'package:adaptive_dialog/adaptive_dialog.dart';
import 'package:chat/configs/app_colors.dart';
import 'package:chat/controllers/group_controller.dart';
import 'package:chat/controllers/private_controller.dart';
import 'package:chat/routes/contact_routes.dart';
import 'package:chat/widgets/custom_avatar.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tencent_im_sdk_plugin/enum/group_member_role.dart';
import 'package:tencent_im_sdk_plugin/models/v2_tim_group_member_full_info.dart';

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

  @override
  Widget build(BuildContext context) {
    return GetX<GroupController>(builder: (_) {
      List<Widget> items = List<Widget>.empty(growable: true);

      if (_.currentGroup.value.memberList != null) {
        var members = _.currentGroup.value.memberList!;

        if (members.length > 13) {
          members = members.sublist(0, 13);
        }

        for (var item in members) {
          items.add(_memberItem(item!));
        }
      }

      /// 因为 Public 类型的群，不支持邀请功能，用户只能主动申请加群
      items.add(
        InkWell(
          onTap: () async {
            OkCancelResult result = await showOkCancelAlertDialog(
              style: AdaptiveStyle.iOS,
              context: Get.context!,
              title: '系统提示',
              message: '当前群聊不支持邀请用户，请分享群二维码至您要邀请的好友。',
              okLabel: '去分享',
              cancelLabel: '取消',
              defaultType: OkCancelAlertDefaultType.ok,
            );

            if (result == OkCancelResult.ok) {
              Get.toNamed(ContactRoutes.groupQrCode);
            }
          },
          child: Column(
            children: [
              Container(
                width: 44,
                height: 44,
                decoration: BoxDecoration(
                  color: AppColors.unactive.withOpacity(0.1),
                  border: Border.all(
                    color: AppColors.unactive.withOpacity(0.3),
                    width: 0.4,
                    style: BorderStyle.solid,
                  ),
                  borderRadius: BorderRadius.circular(4),
                ),
                child: const Center(
                  child: Icon(
                    Icons.add,
                    color: AppColors.unactive,
                  ),
                ),
              ),
              const Text(
                '邀请',
                style: TextStyle(
                  fontSize: 10,
                  color: AppColors.unactive,
                ),
              ),
            ],
          ),
        ),
      );

      if (_.currentGroup.value.isAdmin || _.currentGroup.value.isOwner) {
        items.add(
          InkWell(
            onTap: () {
              Get.toNamed(
                ContactRoutes.groupKick,
              );
            },
            child: Column(
              children: [
                Container(
                  width: 44,
                  height: 44,
                  decoration: BoxDecoration(
                    color: AppColors.unactive.withOpacity(0.1),
                    border: Border.all(
                      color: AppColors.unactive.withOpacity(0.3),
                      width: 0.4,
                      style: BorderStyle.solid,
                    ),
                    borderRadius: BorderRadius.circular(4),
                  ),
                  child: const Center(
                    child: Icon(
                      Icons.remove,
                      color: AppColors.unactive,
                    ),
                  ),
                ),
                const Text(
                  '移除',
                  style: TextStyle(
                    fontSize: 10,
                    color: AppColors.unactive,
                  ),
                ),
              ],
            ),
          ),
        );
      }

      return Container(
        color: AppColors.white,
        padding: const EdgeInsets.all(16),
        child: GridView.count(
          shrinkWrap: true,
          physics: const NeverScrollableScrollPhysics(),
          crossAxisCount: 5,
          childAspectRatio: 1 / 1.1,
          crossAxisSpacing: 12,
          mainAxisSpacing: 12,
          children: items,
        ),
      );
    });
  }

  Widget _memberItem(V2TimGroupMemberFullInfo member) {
    double w = 40;
    double h = 12;
    return InkWell(
      onTap: () async {
        await PrivateController.to.setCurrentFriend(member.userID);
        Get.toNamed(
          ContactRoutes.friendProfile,
        );
      },
      child: Column(
        children: [
          ClipRRect(
            child: Stack(
              children: [
                CustomAvatar(member.faceUrl),
                if (member.role ==
                        GroupMemberRoleType.V2TIM_GROUP_MEMBER_ROLE_ADMIN ||
                    member.role ==
                        GroupMemberRoleType.V2TIM_GROUP_MEMBER_ROLE_OWNER)
                  Positioned(
                    left: 0,
                    top: sqrt(w * w / 2 - sqrt2 * w * h + h * h),
                    child: Transform.rotate(
                      angle: -0.25 * pi,
                      alignment: Alignment.bottomLeft,
                      child: Container(
                        color: member.role ==
                                GroupMemberRoleType
                                    .V2TIM_GROUP_MEMBER_ROLE_OWNER
                            ? AppColors.red
                            : AppColors.golden,
                        width: w,
                        height: h,
                        alignment: Alignment.center,
                        child: Text(
                          member.role ==
                                  GroupMemberRoleType
                                      .V2TIM_GROUP_MEMBER_ROLE_OWNER
                              ? '群主'
                              : '管理员',
                          style: const TextStyle(
                            color: AppColors.white,
                            fontSize: 8,
                          ),
                        ),
                      ),
                    ),
                  ),
              ],
            ),
          ),
          const SizedBox(height: 2),
          Text(
            member.nameCard!.isNotEmpty ? member.nameCard! : member.nickName!,
            style: const TextStyle(
              fontSize: 10,
              color: AppColors.unactive,
            ),
          )
        ],
      ),
    );
  }
}
