import 'package:adaptive_dialog/adaptive_dialog.dart';
import 'package:chat/configs/app_colors.dart';
import 'package:chat/controllers/private_controller.dart';
import 'package:chat/routes/contact_routes.dart';
import 'package:chat/services/tim/block_service.dart';
import 'package:chat/services/tim/friend_service.dart';
import 'package:chat/utils/ui_tools.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 ContactFriendProfileMorePage extends StatelessWidget {
  const ContactFriendProfileMorePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GetX<PrivateController>(builder: (_) {
      return Scaffold(
        appBar: AppBar(
          title: const Text('资料设置'),
        ),
        body: Column(
          children: [
            ActionItem(
              '设置备注',
              isFirst: true,
              isLast: true,
              extend: _.currentFriend.value.friendRemark,
              onTap: () {
                Get.toNamed(
                  ContactRoutes.friendRemark,
                );
              },
            ),
            const SizedBox(height: 8),
            ActionItem(
              '把他推荐给朋友',
              isFirst: true,
              isLast: true,
              onTap: () {
                Get.toNamed(
                  ContactRoutes.friendRecommend,
                );
              },
            ),
            const SizedBox(height: 8),
            ActionItem(
              '加入黑名单',
              isFirst: true,
              isLast: true,
              rightWidget: SizedBox(
                height: 24,
                child: Switch(
                  value: false,
                  onChanged: (e) async {
                    OkCancelResult result = await showOkCancelAlertDialog(
                      style: AdaptiveStyle.iOS,
                      context: Get.context!,
                      title: '加入黑名单',
                      message: '加入黑名单将会解除好友关系，不再接收他的消息',
                      okLabel: '确定',
                      cancelLabel: '取消',
                      defaultType: OkCancelAlertDefaultType.ok,
                    );

                    if (result == OkCancelResult.ok) {
                      var res = await TimBlockService.to.add(
                        _.currentFriend.value.userID,
                      );
                      if (res) {
                        UiTools.toast('加入黑名单成功');
                        Navigator.popUntil(context, (route) => route.isFirst);
                      }
                    }
                  },
                  materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
                ),
              ),
            ),
            const SizedBox(height: 8),
            ActionButton(
              '删除',
              hasTop: true,
              hasBottom: true,
              color: AppColors.red,
              onTap: () async {
                OkCancelResult result = await showOkCancelAlertDialog(
                  style: AdaptiveStyle.iOS,
                  context: Get.context!,
                  title: '删除好友',
                  message: '确定要删除该好友么？',
                  okLabel: '确定',
                  cancelLabel: '取消',
                  defaultType: OkCancelAlertDefaultType.ok,
                );

                if (result == OkCancelResult.ok) {
                  var res = await TimFriendService.to.delete(
                    _.currentFriend.value.userID,
                  );
                  if (res) {
                    UiTools.toast('好友删除成功');

                    /// 关闭所有页面
                    Navigator.popUntil(context, (route) => route.isFirst);
                  }
                }
              },
            ),
          ],
        ),
      );
    });
  }
}
