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

#import "Room+DictRepresentation.h"
#import "NSData+Base64String.h"
#import "NSString+PictureData.h"
#import "Contact+DictRepresentation.h"
#import "DefaultBubbleIcon.h"
#import "SearchGroupsTypes.h"
#import "AvatarGenerator.h"
#import "Participant+DictRepresentation.h"

#define KBubbleType @"Bubble"

@implementation Room (DictRepresentation)

-(NSDictionary*)roomDictionaryRepresentation {
    NSMutableDictionary *roomDic = [[NSMutableDictionary alloc] init];
    if (self.displayName != nil) {
        [roomDic setValue:self.displayName forKey:@"name"];
    }else{
        [roomDic setValue:@"" forKey:@"name"];
    }
    if (self.topic) {
        [roomDic setValue:self.topic forKey:@"topic"];
    }else{
        [roomDic setValue:@"" forKey:@"topic"];
    }
    if (self.photoData != nil) {
        [roomDic setValue:[self getBubblePhotoUrlString] forKey:@"imageURL"];
    } else {
        [roomDic setValue:[self displayName] forKey:@"nameInitials"];
    }
    if (self.hasActiveWebRTCConference && self.conference != nil) {
        [roomDic setValue:[NSNumber numberWithBool:self.conference.isConnectedState] forKey:@"amIConnected"];

    }
    [roomDic setValue:self.jid forKey:@"jId"];
    [roomDic setValue:self.rainbowID forKey:@"id"];
    [roomDic setValue:[NSNumber numberWithBool:self.isMyRoom] forKey:@"isMyUserOwner"];
    [roomDic setValue:[NSNumber numberWithBool:self.isAdmin] forKey:@"isMyUserModerator"];
    [roomDic setValue:self.customData forKey:@"customData"];
    [roomDic setValue:self.creator.contactDictionaryRepresentation forKey:@"bubbleOwner"];
    [roomDic setValue:[NSNumber numberWithBool:self.hasActiveWebRTCConference] forKey:@"hasActiveConference"];
    [roomDic setValue: [self processBubbleParticipants] forKey:@"participants"];
    [roomDic setValue: KBubbleType forKey:@"type"];
    return roomDic;
}

-(NSString*)getDefaultBubbleBase64Photo {
    //in case the Bubble dont have IMG, the draw these Bubble by compine participant in on Photo
    NSMutableOrderedSet<Contact *> *participantsToDisplay = nil;
    participantsToDisplay = [NSMutableOrderedSet orderedSetWithArray:self.participants];
    return UIImagePNGRepresentation([AvatarGenerator drawPhotoFromOrderedList:participantsToDisplay]).encodeToBase64String;
}

- (NSString*) getBubblePhotoUrlString {
    if (self.photoData != nil) {
        NSString *serverName = [ServicesManager.sharedInstance myUser].server.serverDisplayedName;
        NSString *imageURL = [NSString stringWithFormat:@"https://%@/api/room-avatar/%@?size=256",serverName, self.rainbowID];
        return imageURL;
    }
    else {
        return self.displayName;
    }
}

-(NSArray*) processBubbleParticipants {
    NSMutableArray<NSDictionary*> *participantsArray = [[NSMutableArray alloc] init];
    for (Participant *item in self.displayableParticipants) {
        if(item.status == ParticipantStatusAccepted || item.status == ParticipantStatusInvitedGuest || item.status == ParticipantStatusInvited){
            NSDictionary *participantDictionary = item.participantToDictionaryRepresentation;
            [participantsArray addObject: participantDictionary];
        }
    }

    return participantsArray;
}

@end
