import 'package:cached_network_image/cached_network_image.dart';
import 'package:chat_bubbles/bubbles/bubble_normal_image.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:taafee_mobile/common/const/const.dart';
import 'package:taafee_mobile/features/chat/data_layer/model/message.dart';
import '../../../../core/local_storage/local_storage.dart';
import '../../../../core/routing/routing_manager.dart';
import '../../business logic layer/chat_controller.dart';

class ImageMessage extends StatelessWidget {
  ImageMessage(
      {super.key,
      this.dismissibleKey = const Key('image'),
      required this.textFieldFocusNode,
      required this.messageModel});

  final Key dismissibleKey;

  final MessageModel messageModel;
  final FocusNode textFieldFocusNode;
  final ChatController chatController = Get.find<ChatController>();
  @override
  Widget build(BuildContext context) {
    return Dismissible(
      key: dismissibleKey,
      confirmDismiss: (direction) async {
        chatController.updateReplyModel(
          messageModel: messageModel,
        );
        if (!chatController.isReplying.value) {
          chatController.toggleIsReplying();
        }

        FocusScope.of(context).requestFocus(textFieldFocusNode);
        return false;
      },
      onDismissed: null,
      child: BubbleNormalImage(
          color: Colors.transparent,
          tail: false,
          isSender: messageModel.direction == MessageDirection.received
              ? false
              : true,
          id: messageModel.id.toString(),
          onTap: () {
            RoutingManager.to(
              RouteName.imagesGalleryView,
              arguments: (messageModel.content != '')
                  ? [
                      Domain.chatFiles + messageModel.content,
                    ]
                  : FileImage(messageModel.temporaryFile!),
            );
          },
          image: messageModel.content != ''
              ? CachedNetworkImage(
                  fit: BoxFit.cover,
                  imageUrl: Domain.chatFiles + messageModel.content,
                  httpHeaders: {
                    "Authorization": LocalStorage().getChatToken() ?? "",
                  },
                )
              : (messageModel.temporaryFile != null)
                  ? Image.file(
                      messageModel.temporaryFile!,
                      fit: BoxFit.cover,
                    )
                  : Image.network(
                      Domain.domain + networkImageTest.substring(6),
                      fit: BoxFit.cover,
                    )),
      // child: Container(
      //   width: Get.width * .7,
      //   height: 80,
      //   margin: const EdgeInsets.symmetric(horizontal: 10, vertical: 5),
      //   decoration: BoxDecoration(
      //     borderRadius: BorderRadius.circular(8),
      //     image: (messageModel.content != '')
      //         ? DecorationImage(
      //             fit: BoxFit.cover,
      //             image: CachedNetworkImageProvider(
      //               Domain.chatFiles + messageModel.content,
      //               headers: {
      //                 "Authorization": LocalStorage().getChatToken() ?? "",
      //               },
      //             ),
      //           )
      //         : (messageModel.temporaryFile != null)
      //             ? DecorationImage(
      //                 fit: BoxFit.cover,
      //                 image: FileImage(messageModel.temporaryFile!),
      //               )
      //             : DecorationImage(
      //                 fit: BoxFit.cover,
      //                 image: NetworkImage(Domain.domain + networkImageTest.substring(6)),
      //               ),
      //   ),
      // ).onTap(() {
      // RoutingManager.to(
      //   RouteName.imagesGalleryView,
      //   arguments: (messageModel.content != '')
      //       ? [
      //           Domain.chatFiles + messageModel.content,
      //         ]
      //       //   headers: {
      //       //     "Authorization":
      //       //         "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOjYyLCJkZXZpY2VJZCI6NDE5LCJpYXQiOjE2OTI1MzIzNzV9.oju3XMeIU5ggr-L3S-kMJIZTeAs8YuD_XwcfcS8r1wY",
      //       //   },
      //       // )
      //       : FileImage(messageModel.temporaryFile!),
      // );
      //  }
      //    ),
    );
  }
}
