//
//  DyteParticipants.m
//  react-native-dyte-mobile
//
//  Created by Rohit Bhatia on 6/28/21.
//

#import "DyteParticipants.h"
#import "DyteParticipant.h"
#import "DyteDelegateHandler.h"

@implementation DyteParticipants {
    NSMutableDictionary *participantsKey;
}


+ (instancetype)sharedInstance {
      static DyteParticipants *sharedInstance = nil;
      static dispatch_once_t DyteParticipantsOnceToken;
      dispatch_once(&DyteParticipantsOnceToken, ^{
          sharedInstance = [[self alloc] init];
      });
      return sharedInstance;
}

- (void) dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    //https://stackoverflow.com/questions/22617379/xcode-5-dealloc-void-arc-error
    //[super dealloc];
}

- (instancetype)init {
    if (self = [super init]) {
        //Initalizing Empty Participants
        _participants = [NSMutableArray new];
        participantsKey = [NSMutableDictionary new];
    }
    return self;
}

-(void)setAllValueNil {
    _participants = [NSMutableArray new];
    participantsKey = [NSMutableDictionary new];
}


- (NSInteger)doesParticipantsExist:(NSString*)id {
    if ([participantsKey valueForKey: id]) {
        return [(NSNumber*)[participantsKey valueForKey:id] integerValue];
    } else {
        return -1;
    }
}


- (void)createParticipant:(NSString*)id name:(NSString*)name picture:(NSString*)picture clientSpecificId:(NSString*)clientSpecificId audioEnabled:(BOOL)audioEnabled videoEnabled:(BOOL)videoEnabled isPinned:(BOOL)isPinned isScreensharing:(BOOL)isScreensharing {
    //Checking if participant does not exist, then only create new array
    // -1, participant does not exist
    if ([self doesParticipantsExist:id] == -1) {
        // Creating participant object
        DyteParticipant *participant = [DyteParticipant new];
        participant.audioEnabled = audioEnabled;
        participant.videoEnabled = videoEnabled;
        participant.isPinned = isPinned;
        participant.picture = picture;
        participant.name = name;
        participant.id = id;
        participant.isScreensharing = isScreensharing;
        participant.clientSpecificId = clientSpecificId;
        // Adding participant in array
        [_participants addObject:participant];
        // Adding key value pair in object
        [participantsKey setObject:@(true) forKey:id];
        // Sending Message to Dyte Delegate
        [[DyteDelegateHandler new] call:participant andSelector:@selector(participantJoin:)];
    }
}

- (void)updateParticipant:(NSString*)id
audioEnabled:(BOOL)audioEnabled
videoEnabled:(BOOL)videoEnabled
isPinned:(BOOL)isPinned
isScreensharing:(BOOL)isScreensharing {
    if ([self doesParticipantsExist:id] != -1) {
        //Getting Index
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"id == %@", id];
        NSArray *filteredArray = [_participants filteredArrayUsingPredicate:predicate];
        // Getting object at particular index
        DyteParticipant *participant = filteredArray.firstObject;
        participant.audioEnabled = audioEnabled;
        participant.videoEnabled = videoEnabled;
        participant.isPinned = isPinned;
        participant.isScreensharing = isScreensharing;
      [[DyteDelegateHandler new] call:participant andSelector:@selector(participantUpdate:)];

    }
}

- (void)onParticipantLeft:(NSString*)id {
    if ([self doesParticipantsExist:id] != -1) {
      for (int i=0; i<[_participants count]; i++) {
        DyteParticipant *participant = [_participants objectAtIndex:i];
        NSString* participantId = [participant id];
        if ([participantId isEqualToString:id]) {
            [[DyteDelegateHandler new] call:participant andSelector:@selector(participantLeave:)];
          // if exist remove it form arra
          [_participants removeObjectAtIndex:i];
          // remove if from object
          [participantsKey removeObjectForKey:id];
         
        }
      }
    }
}



 
@end
