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

#import "RTCCall+DictRepresentation.h"
#import "Contact+DictRepresentation.h"
#import "Room+DictRepresentation.h"
#import "NSData+Base64String.h"
#import "ConferenceParticipant+DictRepresentation.h"

@implementation RTCCall (DictRepresentation)

-(NSDictionary*)rtcCallToDictionaryRepresentation {

    NSLog(@"[RTCCall+DictRepresentation] rtcCallToDictionaryRepresentation");
    NSMutableDictionary *callDic = [[NSMutableDictionary alloc] init];
    if ([self.peer class] == [RainbowContact class]) {
        Contact *callPeer = (Contact*)[self peer];
        [callDic setValue:self.callID forKey:@"callId"];
        [callDic setValue:[self callStateConverter] forKey:@"callState"];
        NSTimeInterval startCallEpochSeconds = [self.connectionDate timeIntervalSince1970]*1000.0;
        [callDic setValue:[NSNumber numberWithDouble:startCallEpochSeconds] forKey:@"startTime"];
        [callDic setValue:[callPeer contactDictionaryRepresentation] forKey:@"callPeer"];
        [callDic setValue:[NSNumber numberWithBool:self.isVideoEnabled] forKey:@"wasInitiatedWithVideo"];
        [callDic setValue:[NSNumber numberWithBool:self.isSharingEnabled] forKey:@"wasInitiatedWithShare"];
        [callDic setValue:[NSNumber numberWithBool:self.isLocalVideoEnabled] forKey:@"isLocalVideoEnabled"];
        [callDic setValue:[NSNumber numberWithBool:self.isRemoteVideoEnabled] forKey:@"isRemoteVideoEnabled"];
        [callDic setValue:[NSNumber numberWithInt:[self getVideoRemoteStreamCount]] forKey:@"videoRemoteStreamCount"];
        [callDic setValue:@"P2P" forKey:@"currentTypeName"];
    }
    return callDic;
}

-(NSDictionary*)conferenceCallToDictionaryRepresentation {
    NSLog(@"[RTCCall+DictRepresentation] conferenceCallToDictionaryRepresentation");
    NSMutableDictionary *callDic = [[NSMutableDictionary alloc] init];
    if ([self.peer class] == [Room class]) {
        Room *conferenceRoom = (Room*)[self peer];
        Conference *conference = conferenceRoom.conference;
        BOOL isConferenceWithVedio = conferenceRoom.conference.publishers.count > 0;
        Contact *activeTalker = [self getConferenceActiveTalkerContact:conferenceRoom.talkers];
        // ICall [parent type] Props
        [callDic setValue:[conferenceRoom roomDictionaryRepresentation] forKey:@"callPeer"];
        [callDic setValue:[self callStateConverter] forKey:@"callState"];
        NSTimeInterval startCallEpochSeconds = [self.connectionDate timeIntervalSince1970]*1000.0;
        [callDic setValue:[conferenceRoom rainbowID] forKey:@"callId"];
        [callDic setValue:[NSNumber numberWithDouble:startCallEpochSeconds] forKey:@"startTime"];
        // IConference
        [callDic setValue:[NSNumber numberWithBool:isConferenceWithVedio] forKey:@"wasInitiatedWithVideo"];
        [callDic setValue:[NSNumber numberWithBool:[self isLocalVideoEnabled]] forKey:@"isLocalVideoEnabled"];
        [callDic setValue:[NSNumber numberWithBool:[conference isLocked]] forKey:@"isLocked"];
        if (conference) {
            [callDic setValue:[self processConferenceAttendee:conference] forKey:@"attendees"];
        }
        [callDic setValue:[NSNumber numberWithBool: conference.isOwner] forKey:@"isOwner"];
        [callDic setValue:@"Conference" forKey:@"currentTypeName"];
        [callDic setValue:[NSNumber numberWithBool: conference.isConnectedState] forKey:@"hasMyUserJoinedConferenceCall"];
        [callDic setValue:[NSNumber numberWithBool: self.isSharingEnabled] forKey:@"isSharingEnabled"];
        if (self.isSharingEnabled && conference.sharingPublisher) {
            NSString *publisherID = conference.sharingPublisher.getRainbowId;
            ConferenceParticipant *sharingParticipant =  [conference getParticipantWithId:  publisherID];
            [callDic setValue: sharingParticipant.conferenceParticipantDictionaryRepresentation forKey:@"sharingParticipant"];
        }

        [callDic setValue:activeTalker.contactDictionaryRepresentation  forKey:@"activeSpeaker"];

    }
    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";
    }
}

-(NSArray*) processConferenceAttendee: (Conference *) roomConference{
    NSLog(@"processConferenceAttendee with count: %1lu", (unsigned long)roomConference.participants.count);
    NSMutableArray<NSDictionary*> *participantsArray = [[NSMutableArray alloc] init];
    for (ConferenceParticipant *participant in roomConference.participants) {
        NSMutableDictionary *paticipantDictionary = participant.conferenceParticipantDictionaryRepresentation.mutableCopy;
        [participantsArray addObject:paticipantDictionary];
    }
    return participantsArray;
}

- (int) getVideoRemoteStreamCount {
    // This a Dummy, but we need to go through this to be compatable with the Android SDK behavior
    if (self.isSharingEnabled && self.isRemoteVideoEnabled) {
        return 2;
    }
    else if (!self.isSharingEnabled && !self.isRemoteVideoEnabled){
        return 0;
    }
    else{
        return 1;
    }
}

- (Contact*)getConferenceActiveTalkerContact: (NSArray<ConferenceParticipant*> *)talkers {
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.isTalking == %@",@YES];
    ConferenceParticipant *talkerParticipant = [talkers filteredArrayUsingPredicate:predicate].firstObject;
    return  talkerParticipant.getContact;
}

@end

