//
//  Conversation+DictRepresentation.m
//  rainbow
//
//  Created by Doha Tubaileh on 9/29/19.
//  Copyright © 2019 Facebook. All rights reserved.
//

#import "Conversation+DictRepresentation.h"
#import "Contact+DictRepresentation.h"
#import "NSDate+ReactDateFormatter.h"
#import "CallLog+CallStateToReactConverter.h"
#import "NSData+Base64String.h"
#import "DefaultBubbleIcon.h"
#import "Room+DictRepresentation.h"
#import "SearchGroupsTypes.h"

#define KConversationType @"Conversation"

@implementation Conversation (DictRepresentation)
-(NSMutableDictionary*)conversationDictionaryRepresentation {
    NSLog(@"[Conversation+DictRepresentation] conversationDictionaryRepresentation");
    NSMutableDictionary *conversationObject = [[NSMutableDictionary alloc]init];
    [conversationObject setValue:self.peer.jid forKey:@"jId"];
    if (self.lastMessage.date != nil) {
        [conversationObject setValue:self.lastMessage.date.changeDateFormatToReact forKey:@"date"];
    }
    [conversationObject setValue:[NSNumber numberWithInteger:self.unreadMessagesCount] forKey:@"nbMessages"];
    if (self.type == ConversationTypeRoom) {
        //Room
        Room *room = (Room*)self.peer;
        NSDictionary *roomDict = room.roomDictionaryRepresentation;
        [conversationObject setValue:roomDict forKey:@"bubble"];
        [conversationObject setValue:@YES forKey:@"isBubble"];
        [conversationObject setValue:roomDict[@"name"] forKey:@"name"];
        [conversationObject setValue:roomDict[@"imageURL"] forKey:@"imageURL"];
        [conversationObject setValue:@(room.conference != nil) forKey:@"hasActiveConference"];
    }
    else {
        RainbowContact *contact = (RainbowContact*)self.peer;
        NSDictionary *contactDic = contact.contactDictionaryRepresentation;
        [conversationObject setValue:contactDic[@"name"] forKey:@"name"];
        [conversationObject setValue:self.peer.jid forKey:@"contactJid"];
        [conversationObject setValue:contactDic[@"imageURL"] forKey:@"imageURL"];
        [conversationObject setValue:@NO forKey:@"isBubble"];
        [conversationObject setValue:[NSNumber numberWithBool:contact.isInRoster] forKey:@"peerIsRoster"];
        [conversationObject setValue:[NSNumber numberWithBool:contact.sentInvitationId != nil] forKey:@"peerIsInvited"];
        [conversationObject setValue:contact.contactDictionaryRepresentation forKey:@"contact"];
        [conversationObject setValue:[NSNumber numberWithBool:contact.isBot] forKey:@"isBot"];
    }
    //chack if last mesasage of
    if (self.lastMessage.type == MessageTypeWebRTC) {
        CallLog *callLog = self.lastMessage.callLog;
        NSString *callMSG = [NSString stringWithFormat:@"%@ %@",callLog.callStateConverterForReact,@"call"];
        [conversationObject setValue:callMSG forKey:@"lastMsg"];
    }//check if the last meaasge have file attachment and with no msg body, show file name
    else if (self.lastMessage.attachment != nil && self.lastMessage.body == nil) {
        NSString *fileName = self.lastMessage.attachment.fileName;
        [conversationObject setValue:fileName forKey:@"lastMsg"];
    }
    else {
        [conversationObject setValue:self.lastMessage.body == nil ? @"":self.lastMessage.body forKey:@"lastMsg"];
    }
    [conversationObject setValue:KConversationType forKey:@"type"];
    
    return conversationObject;
}


@end
