import 'package:adaptive_dialog/adaptive_dialog.dart';
import 'package:chat/configs/app_colors.dart';
import 'package:chat/controllers/group_controller.dart';
import 'package:chat/routes/contact_routes.dart';
import 'package:chat/services/tim/conversation_service.dart';
import 'package:chat/services/tim/group_service.dart';
import 'package:chat/views/conversation/info/widgets/group_member_preview.dart';
import 'package:chat/views/home/widgets/action_button.dart';
import 'package:chat/views/home/widgets/action_item.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

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

  @override
  Widget build(BuildContext context) {
    GroupController.to.fetchGroupMemberList();

    return GetX<GroupController>(builder: (_) {
      var currentGroup = _.currentGroup.value;
      var group = _.currentGroup.value.group;

      return Scaffold(
        appBar: AppBar(
          title: Text('聊天信息(${group?.memberCount})'),
          centerTitle: true,
        ),
        body: SingleChildScrollView(
          child: Column(
            children: [
              const GroupMemberPreview(),
              const SizedBox(height: 8),
              Container(
                color: AppColors.white,
                child: Column(
                  children: [
                    ActionItem(
                      '群聊名称',
                      isFirst: true,
                      extend: group?.groupName,
                      onTap: () {
                        if (currentGroup.isAdmin || currentGroup.isOwner) {
                          // Get.toNamed(
                          //   ImRoutes.groupName,
                          // );
                        }
                      },
                    ),
                    ActionItem(
                      '群二维码',
                      onTap: () {
                        Get.toNamed(
                          ContactRoutes.groupQrCode,
                        );
                      },
                    ),
                    ActionItem(
                      '群公告',
                      bottom: group?.notification,
                      onTap: () {
                        Get.toNamed(
                          ContactRoutes.groupNotification,
                        );
                      },
                    ),
                    Visibility(
                      visible: currentGroup.isAdmin || currentGroup.isOwner,
                      child: ActionItem(
                        '群管理',
                        onTap: () {
                          Get.toNamed(
                            ContactRoutes.groupManage,
                          );
                        },
                      ),
                    ),
                    Visibility(
                      visible: currentGroup.isAdmin || currentGroup.isOwner,
                      child: ActionItem(
                        '加群申请',
                        isLast: true,
                        onTap: () {
                          Get.toNamed(
                            ContactRoutes.groupApprove,
                          );
                        },
                      ),
                    ),
                  ],
                ),
              ),
              const SizedBox(height: 8),
              Container(
                color: AppColors.white,
                child: Column(
                  children: [
                    ActionItem(
                      '查找聊天记录',
                      onTap: () {
                        // Get.toNamed(
                        //   ImRoutes.conversationSearch,
                        // );
                      },
                    ),
                  ],
                ),
              ),
              const SizedBox(height: 8),
              Container(
                color: AppColors.white,
                child: Column(
                  children: [
                    ActionItem(
                      '消息免打扰',
                      rightWidget: SizedBox(
                        height: 24,
                        child: Switch(
                          value:
                              _.currentGroup.value.conversation!.recvOpt == 1,
                          onChanged: (e) async {
                            _.toggleReceiveOpt();
                          },
                          materialTapTargetSize:
                              MaterialTapTargetSize.shrinkWrap,
                        ),
                      ),
                    ),
                    ActionItem(
                      '置顶聊天',
                      rightWidget: SizedBox(
                        height: 24,
                        child: Switch(
                          value: _.currentGroup.value.conversation!.isPinned!,
                          onChanged: (e) async {
                            _.togglePinned();
                          },
                          materialTapTargetSize:
                              MaterialTapTargetSize.shrinkWrap,
                        ),
                      ),
                    ),
                  ],
                ),
              ),
              const SizedBox(height: 8),
              Container(
                color: AppColors.white,
                child: Column(
                  children: [
                    ActionItem(
                      '我在群里的昵称',
                      extend: _.currentGroup.value.selfInfo?.nameCard ?? '',
                      onTap: () {
                        Get.toNamed(
                          ContactRoutes.groupNickname,
                        );
                      },
                    ),
                  ],
                ),
              ),
              const SizedBox(height: 8),
              Column(
                children: [
                  ActionButton(
                    '清空聊天记录',
                    onTap: () async {
                      OkCancelResult result = await showOkCancelAlertDialog(
                        style: AdaptiveStyle.iOS,
                        context: Get.context!,
                        title: '系统提示',
                        message: '将删除该聊天记录，是否继续？',
                        okLabel: '确定',
                        cancelLabel: '取消',
                        defaultType: OkCancelAlertDefaultType.ok,
                      );

                      if (result == OkCancelResult.ok) {
                        TimConversationService.to.clearHistoryMessage(
                          _.currentGroup.value.conversation!,
                        );
                      }
                    },
                  ),
                  if (!currentGroup.isOwner) const Divider(height: 0),
                  ActionButton(
                    '删除并退出',
                    onTap: () async {
                      OkCancelResult result = await showOkCancelAlertDialog(
                        style: AdaptiveStyle.iOS,
                        context: Get.context!,
                        title: '系统提示',
                        message: '将删除并退出该群组，是否继续？',
                        okLabel: '确定',
                        cancelLabel: '取消',
                        defaultType: OkCancelAlertDefaultType.ok,
                      );

                      if (result == OkCancelResult.ok) {
                        TimGroupService.to.quit(
                          group!,
                        );
                        Navigator.popUntil(context, (route) => route.isFirst);
                      }
                    },
                  ),
                ],
              ),
              const SizedBox(height: 16),
            ],
          ),
        ),
      );
    });
  }
}
