import 'package:chat/configs/app_colors.dart';
import 'package:chat/routes/app_routes.dart';
import 'package:chat/routes/contact_routes.dart';
import 'package:chat/routes/user_routes.dart';
import 'package:chat/services/tim/conversation_service.dart';
import 'package:chat/views/home/widgets/conversation_item.dart';
import 'package:chat/widgets/custom_easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:get/get.dart';
import 'package:permission_handler/permission_handler.dart';

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: AppColors.white,
      appBar: _appBar(),
      body: EasyRefresh(
        header: CustomEasyRefresh.header,
        // firstRefresh: true,
        onRefresh: () async {
          await TimConversationService.to.fetchList();
        },
        child: GetX<TimConversationService>(
          builder: (_) {
            return _.conversationList.isEmpty
                ? ConstrainedBox(
                    constraints: BoxConstraints(minHeight: Get.height / 2),
                    child: CustomEasyRefresh.empty(text: '暂无消息'),
                  )
                : ListView.separated(
                    shrinkWrap: true,
                    physics: const ClampingScrollPhysics(),
                    itemCount: _.conversationList.length,
                    itemBuilder: (context, index) {
                      return ConversationItem(_.conversationList[index]!);
                    },
                    separatorBuilder: (context, index) {
                      return const Divider(
                        height: 0,
                        indent: 72,
                      );
                    },
                  );
          },
        ),
      ),
    );
  }

  PreferredSizeWidget _appBar() {
    return AppBar(
      title: const Text('消息'),
      actions: [
        IconButton(
          onPressed: () {
            Get.toNamed(AppRoutes.search);
          },
          icon: const Icon(Icons.search_outlined),
        ),
        PopupMenuButton<String>(
          onSelected: (String value) {
            switch (value) {
              case 'A':
                Get.toNamed(UserRoutes.qrCode);
                break;
              case 'B':
                Get.toNamed(ContactRoutes.groupCreate);
                break;
              case 'C':
                Get.toNamed(ContactRoutes.friendRequest);
                break;
              case 'D':
                Permission.camera.request().isGranted.then((value) {
                  if (value) {
                    Get.toNamed(AppRoutes.scan);
                  }
                });
                break;
            }
          },
          tooltip: '',
          offset: const Offset(0, 56),
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(8),
          ),
          icon: const Icon(
            Icons.add,
          ),
          itemBuilder: (_) {
            return [
              _popupMenuItem('我的二维码', 'A', Icons.qr_code_outlined),
              _popupMenuItem('发起群聊', 'B', Icons.textsms),
              _popupMenuItem('添加朋友', 'C', Icons.person_add_alt),
              _popupMenuItem('扫一扫', 'D', Icons.photo_camera),
            ];
          },
        )
      ],
    );
  }

  /// 右上角弹出菜单
  PopupMenuItem<String> _popupMenuItem(
    String text,
    String value,
    IconData icon,
  ) {
    return PopupMenuItem(
      value: value,
      child: Row(
        children: [
          Icon(
            icon,
            color: AppColors.primary,
            size: 18,
          ),
          const SizedBox(width: 8),
          Text(
            text,
            style: const TextStyle(
              fontSize: 14,
            ),
          ),
        ],
      ),
    );
  }

  // /// 左侧抽题
  // Widget _drawer() {
  //   return Drawer(
  //     child: ListView(
  //       children: [
  //         GetX<UserController>(builder: (_) {
  //           return DrawerHeader(
  //             child: Column(
  //               crossAxisAlignment: CrossAxisAlignment.start,
  //               children: [
  //                 CustomCircleAvatar(
  //                   _.userInfo.value!.avatar,
  //                   size: 72,
  //                 ),
  //                 const SizedBox(height: 8),
  //                 Text(
  //                   _.userInfo.value!.nickname,
  //                   style: const TextStyle(
  //                     fontSize: 24,
  //                     fontWeight: FontWeight.bold,
  //                   ),
  //                 ),
  //               ],
  //             ),
  //           );
  //         }),
  //         ListTile(
  //           onTap: () {
  //             Get.back();
  //             Get.toNamed(UserRoutes.info);
  //           },
  //           leading: const Icon(Icons.info_outlined),
  //           title: const Text('修改资料'),
  //         ),
  //         const Divider(height: 0),
  //         ListTile(
  //           onTap: () {
  //             Get.back();
  //             Get.toNamed(
  //               ImRoutes.friend,
  //               arguments: {
  //                 'name_card': false,
  //               },
  //             );
  //           },
  //           leading: const Icon(Icons.person_outlined),
  //           title: const Text('我的好友'),
  //         ),
  //         const Divider(height: 0),
  //         ListTile(
  //           onTap: () {
  //             Get.back();
  //             Get.toNamed(ImRoutes.group);
  //           },
  //           leading: const Icon(Icons.group_outlined),
  //           title: const Text('我的群组'),
  //         ),
  //         const Divider(height: 0),
  //         ListTile(
  //           onTap: () {
  //             Get.back();
  //             Get.toNamed(ImRoutes.blcok);
  //           },
  //           leading: const Icon(Icons.block_flipped),
  //           title: const Text('黑名单'),
  //         ),
  //         const Divider(height: 0),
  //         ListTile(
  //           onTap: () {
  //             Get.back();
  //             Get.toNamed(ImRoutes.friendRequest);
  //           },
  //           leading: const Icon(Icons.person_add_alt_outlined),
  //           title: const Text('好友申请'),
  //         ),
  //         const Divider(height: 0),
  //         ListTile(
  //           onTap: () {
  //             Get.back();
  //             Get.toNamed(ImRoutes.setting);
  //           },
  //           leading: const Icon(Icons.settings_outlined),
  //           title: const Text('消息设置'),
  //         ),
  //         const Divider(height: 0),
  //       ],
  //     ),
  //   );
  // }
}
