import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:get/get.dart';
import 'package:taafee_mobile/common/const/const.dart';
import 'package:taafee_mobile/common/extensions/widget_extension.dart';
import 'package:taafee_mobile/common/widgets/text.dart';
import 'package:taafee_mobile/common/widgets/toast.dart';
import 'package:taafee_mobile/core/routing/routing_manager.dart';
import 'package:taafee_mobile/features/chat/data_layer/model/chat_user.dart';
import 'package:taafee_mobile/features/chat/presentation_layer/widgets/circle_avatar.dart';
import 'package:taafee_mobile/features/home/business_logic_layer/home_controller.dart';
import '../../business logic layer/chat_controller.dart';
import '../../data_layer/model/room.dart';

// ignore: must_be_immutable
class AppBarChatWidget extends StatelessWidget implements PreferredSizeWidget {
  @override
  Size get preferredSize => const Size.fromHeight(100);
  final HomeController homeController = Get.find<HomeController>();
  final ChatController chatController = Get.find<ChatController>();
  final RoomType roomType;
  final void Function()? onBack;
  AppBarChatWidget({
    super.key,
    required this.chatUser,
    this.avatar,
    required this.roomType,
    this.onBack,
  });
  ChatUser? chatUser;
  final String? avatar;
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      width: Get.width,
      height: 70,
      child: Column(
        children: [
          Row(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Obx(() => (!homeController.isArabic.value)
                  ? SvgPicture.asset("assets/icons/arrow-left.svg")
                  : SvgPicture.asset("assets/icons/arrow right.svg")).onTap(
                () {
                  if (roomType == RoomType.private) {
                    chatController.updateRoom();
                  }
                  onBack?.call();
                  RoutingManager.back();
                },
              ).expanded(Responsive.isTablet() ? 0 : 1),
              if (Responsive.isTablet())
                const SizedBox(
                  width: 12,
                ),
              CircleAvatarWidget(
                isUserAvatar: false,
                avatarImageLink: avatar,
                radius: 26,
              ).expanded(Responsive.isTablet() ? 0 : 2),
              if (Responsive.isTablet())
                const SizedBox(
                  width: 12,
                ),
              BoldTextWidget(
                chatUser?.name ?? ('support'.tr),
                color: AppColors.textColor,
                fontSize: 14,
              ).expanded(Responsive.isTablet() ? 0 : 6),
              if (Responsive.isTablet()) Container().expanded(8),
              Obx(() {
                return Visibility(
                  visible: chatController.currentRoom.value!.state ==
                      RoomState.active,
                  child: PopupMenuButton(
                      position: PopupMenuPosition.under,
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(12),
                      ),
                      itemBuilder: (context) {
                        return <PopupMenuItem>[
                          PopupMenuItem(
                              onTap: () {
                                if (chatController.connectionState.value ==
                                    SocketConnectionState.connected) {
                                  chatController.blockRoom(onSuccess: () {
                                    // RoutingManager.back();
                                  });
                                } else {
                                  Toast.showToast(
                                      'you_have_no_internet_connection'.tr);
                                }
                              },
                              child: Row(
                                children: [
                                  Icon(
                                    Icons.block,
                                    color: AppColors.textColor,
                                  ),
                                  const SizedBox(
                                    width: 8,
                                  ),
                                  BoldTextWidget(
                                    'Block User',
                                    color: AppColors.textColor,
                                  ),
                                ],
                              )),
                        ];
                      }),
                );
              })
            ],
          )
              .paddingSymmetric(horizontal: 10, vertical: 4)
              .expanded(Responsive.isTablet() ? 4 : 4),
        ],
      ),
    );
  }
}
