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

#import "Call+DictRepresentation.h"
#import "Contact+DictRepresentation.h"
#import "NSData+Base64String.h"
#import "Contact+DictRepresentation.h"
#import "AvatarGenerator.h"
@implementation Call (DictRepresentation)

-(NSDictionary*)pBxCallToDictionaryRepresentation {

    NSLog(@"[Call+DictRepresentation.h] pBxCallToDictionaryRepresentation");
    NSMutableDictionary *callDic = [[NSMutableDictionary alloc] init];
    Contact *callPeer = (Contact*)[self remoteParty];
    NSDictionary *peerContactDic = [callPeer contactDictionaryRepresentation];
    BOOL isConference = [self.callCause isEqualToString:kCallsConferenced];
    BOOL isHeldByRemote = self.status == CallStatusHeldByRemote? YES:NO;
    if (isConference && self.participants.count > 1) {

        NSMutableOrderedSet<Contact *> *participantsToDisplay = nil;
        participantsToDisplay = [NSMutableOrderedSet orderedSetWithArray:self.participants];
        NSMutableString *conferenceName = [NSString stringWithFormat:@"%@ / %@",self.participants.firstObject.fullName, self.participants.lastObject.fullName].mutableCopy;
        [callDic setValue:UIImagePNGRepresentation([AvatarGenerator drawPhotoFromOrderedList:participantsToDisplay]).encodeToBase64String forKey:@"participantPic"];
        [callDic setValue:conferenceName forKey:@"participantName"];
    }
    else {
        [callDic setValue:peerContactDic[@"imageURL"] forKey:@"participantPic"];
        [callDic setValue:peerContactDic[@"contactName"] forKey:@"participantName"];
    }

    [callDic setValue:@NO forKey:@"wasInitiatedWithVideo"];
    [callDic setValue:[NSNumber numberWithBool:isHeldByRemote] forKey:@"isHeldByDistant"];
    [callDic setValue:[self callStateConverter] forKey:@"callState"];
    [callDic setValue:[NSNumber numberWithBool:isConference] forKey:@"isConference"];

    NSTimeInterval nowEpochSeconds = [self.connectionDate timeIntervalSince1970]*1000.0;
    [callDic setValue:[NSNumber numberWithDouble:nowEpochSeconds] forKey:@"startTime"];


    [callDic setValue:@YES forKey:@"isPbX"];
    [callDic setValue:self.callRef forKey:@"callReferenceId"];
    BOOL isVoiceMailAllowed = [ServicesManager sharedInstance].myUser.isAllowedToUseTelephonyVoiceMail;
    [callDic setValue:[NSNumber numberWithDouble:isVoiceMailAllowed] forKey:@"isVoiceMailAllowed"];

    BOOL hasPbxAccess = [ServicesManager sharedInstance].myUser.isAllowedToUseTelephony;
    [callDic setValue:[NSNumber numberWithDouble:hasPbxAccess] forKey:@"hasPbxAccess"];
    [callDic setValue:@"PBX" forKey:@"currentTypeName"];

    return callDic;

}

-(NSString*)callStateConverter {
    if ([self status] == CallStatusEstablished || [self status] == CallStatusConnecting ) {
        return @"ACTIVE";
    }
    if ([self status] == CallStatusHeldByRemote || [self status] == CallStatusHeldByUser) {
        return @"HELD";
    }
    if ([self isIncoming] == YES) {
        return @"RINGING_INCOMING";
    }
    else {
        return @"RINGING_OUTGOING";
    }
}

@end
