import 'dart:developer';

import 'package:mc_common_app/classes/app_state.dart';
import 'package:mc_common_app/extensions/string_extensions.dart';
import 'package:mc_common_app/utils/enums.dart';

class ChatMessageModel {
  int? id;
  String? senderUserID;
  String? receiverUserID;
  String? senderName;
  int? messageType;
  ChatMessageTypeEnum? chatMessageTypeEnum;
  String? chatText;
  int? requestID;
  int? reqOfferID;
  int? serviceProviderID;
  int? offerStatus;
  ReqOffer? reqOffer;
  List<MessageImageModel>? messageImages;
  bool? isRead;
  String? readOn;
  bool? isMyMessage;

  ChatMessageModel({
    this.id,
    this.senderUserID,
    this.receiverUserID,
    this.senderName,
    this.messageType,
    this.chatMessageTypeEnum,
    this.chatText,
    this.requestID,
    this.reqOfferID,
    this.serviceProviderID,
    this.offerStatus,
    this.reqOffer,
    this.messageImages,
    this.isRead,
    this.readOn,
    this.isMyMessage,
  });

  @override
  String toString() {
    return 'ChatMessageModel{id: $id, senderUserID: $senderUserID, senderName: $senderName, messageType: $messageType, chatMessageTypeEnum: $chatMessageTypeEnum, chatText: $chatText, requestID: $requestID, reqOfferID: $reqOfferID, serviceProviderID: $serviceProviderID, offerStatus: $offerStatus, reqOffer: ${reqOffer.toString()}, isRead: $isRead, readOn: $readOn, isMyMessage: $isMyMessage}';
  }

  ChatMessageModel.fromJson(Map<String, dynamic> json, {bool isFromNetwork = true, bool isForReqOfferImagesURLs = false}) {
    final myUserId = AppState().getUser.data!.userInfo!.userId.toString().toUpperCase();
    id = json['id'];
    senderUserID = json['senderUserID'];
    receiverUserID = json['receiverUserID'] ?? "";
    senderName = json['senderName'];
    messageType = json['messageType'];
    chatMessageTypeEnum = (json['messageType'] as int).toChatMessageTypeEnum();
    chatText = json['chatText'];
    requestID = json['requestID'];
    reqOfferID = json['reqOfferID'];
    serviceProviderID = json['serviceProviderID'];
    offerStatus = json['offerStatus'];
    reqOffer = json['reqOffer'] != null ? ReqOffer.fromJson(json['reqOffer'], isFromNetwork, isForReqOfferImagesURLs) : null;
    if (json['reqOfferImages'] != null) {
      messageImages = <MessageImageModel>[];
      json['reqOfferImages'].forEach((v) {
        messageImages!.add(MessageImageModel.fromJson(v, isFromNetwork));
      });
    }
    isRead = json['isRead'];
    readOn = json['readOn'];
    isMyMessage = (json['senderUserID']).toString().toUpperCase() == myUserId;
  }
}

class ReqOffer {
  int? id;
  int? requestID;
  int? serviceProviderID;
  int? offerStatus;
  String? offerStatusText;
  String? comment;
  String? serviceItemName;
  int? manufacturedById;
  String? manufacturedOn;
  double? price;
  bool? isDeliveryAvailable;
  RequestsTypeEnum? requestsTypeEnum;
  RequestOfferStatusEnum? requestOfferStatusEnum;
  List<MessageImageModel>? reqOfferImages;

  ReqOffer({
    this.id,
    this.requestID,
    this.serviceProviderID,
    this.offerStatus,
    this.offerStatusText,
    this.comment,
    this.serviceItemName,
    this.manufacturedById,
    this.manufacturedOn,
    this.price,
    this.isDeliveryAvailable,
    this.requestOfferStatusEnum,
    this.requestsTypeEnum,
    this.reqOfferImages,
  });

  ReqOffer.fromJson(Map<String, dynamic> json, bool isFromNetwork, bool isForReqOfferImagesURLs) {
    id = json['id'];
    requestID = json['requestID'];
    serviceProviderID = json['serviceProviderID'];
    offerStatus = json['offerStatus'];
    offerStatusText = json['offerStatusText'];
    comment = json['comment'];
    serviceItemName = json['serviceItem'];
    manufacturedById = json['offeredItemCreatedBy'];
    manufacturedOn = json['offeredItemCreatedOn'];
    price = json['price'];
    isDeliveryAvailable = json['isDeliveryAvailable'];
    requestOfferStatusEnum = ((json['offerStatus']) as int).toRequestOfferStatusEnum();
    requestsTypeEnum = RequestsTypeEnum.serviceRequest;
    // if (isForReqOfferImagesURLs) {
    //   if (json['reqOfferImagesURLs'] != null) {
    //     reqOfferImages = <MessageImageModel>[];
    //     json['reqOfferImagesURLs'].forEach((v) {
    //       reqOfferImages!.add(MessageImageModel.fromJson(v, isFromNetwork));
    //     });
    //   }
    // } else {
    if (json['reqOfferImages'] != null) {
      reqOfferImages = <MessageImageModel>[];
      json['reqOfferImages'].forEach((v) {
        reqOfferImages!.add(MessageImageModel.fromJson(v, isFromNetwork));
      });
    }
    // }
  }

  @override
  String toString() {
    return 'ReqOffer{id: $id, requestID: $requestID, serviceProviderID: $serviceProviderID, offerStatus: $offerStatus, offerStatusText: $offerStatusText, comment: $comment, serviceItemName: $serviceItemName, manufacturedById: $manufacturedById, manufacturedOn: $manufacturedOn, price: $price, requestsTypeEnum: $requestsTypeEnum, requestOfferStatusEnum: $requestOfferStatusEnum}';
  }
}

class OfferRequestCommentModel {
  int? index;
  String? title;
  bool? isSelected;

  OfferRequestCommentModel({this.index, this.title, this.isSelected});
}

class MessageImageModel {
  int? id;
  String? imageUrl;
  String? imageStr;
  String? imagePath;
  bool? isFromNetwork;
  int? reqOfferID;

  MessageImageModel({
    this.id,
    this.imageUrl,
    this.imageStr,
    this.imagePath,
    this.reqOfferID,
    this.isFromNetwork = false,
  });

  MessageImageModel.fromJson(Map<String, dynamic> json, bool fromNetwork) {
    id = json['id'];
    imageUrl = json['imageUrl'];
    imageStr = json['imageStr'];
    imagePath = json[''];
    reqOfferID = json['reqOfferID'];
    isFromNetwork = fromNetwork;
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'imageStr': imageStr,
      'reqOfferID': reqOfferID, // We don't include 'isFromNetwork' since it's set manually when parsing JSON
    };
  }

  @override
  String toString() {
    return 'MessageImageModel{id: $id, imageUrl: $imageUrl, imageStr: $imageStr, imagePath: $imagePath, isFromNetwork: $isFromNetwork, reqOfferID: $reqOfferID}';
  }
}
