//
//  DyteSelfParticipant.m
//  DyteMobile
//
//  Created by Rohit Bhatia on 6/24/21.
//  Copyright © 2021 Facebook. All rights reserved.
//



#import "DyteSelfParticipant.h"

@implementation DyteSelfParticipant


- (instancetype)init {
    self = [super init];
    return self;
}



-(void)setAllValueNil {
    _name = nil;
    _id = nil;
    _picture = nil;
    _isScreensharing = nil;
    _videoEnabled = nil;
    _audioEnabled = nil;
    _isPinned = nil;
}

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


- (void)setUserInfo:(NSString*)id
name:(NSString*)name
picture:(NSString*)picture {
    _name = name;
    _id = id;
    _picture = picture;
}

- (void)setSelfVideo:(BOOL)value {
    _videoEnabled = value;
}

- (void)setSelfAudioEnabled:(BOOL)value {
    _audioEnabled = value;
}

- (void)setSelfPin:(BOOL)value {
    _isPinned = value;
}


// This message is sent from dyte helper to JS
- (void) emmitMessageToDyteHelper:(NSString*)eventName {
    NSDictionary *supportedEvents = @{
        @"SELF_TOGGLE_AUDIO": @(true),
        @"SELF_TOGGLE_VIDEO": @(true),
        @"SELF_PIN": @(true),
        @"SELF_UNPIN": @(true),
        @"SELF_TOGGLE_SCREENSHARING": @(true),
        @"LEAVE_ROOM": @(true)
    };
    if ([supportedEvents valueForKey:eventName]) {
        NSDictionary *userInfo = @{
            @"type": eventName,
        };
        [[NSNotificationCenter defaultCenter]
            postNotificationName:@"DyteHelperToJSNotification"
            object:self
            userInfo:userInfo
         ];
    } else {
        NSLog(@"⚠️ Event not supported");
    }
}

- (void)enableAudio {
   //if audio Enabled is false then only send message upwarss
    if (!_audioEnabled) {
        [self emmitMessageToDyteHelper:@"SELF_TOGGLE_AUDIO"];
    }
}
- (void)enableVideo {
    if (!_videoEnabled) {
        [self emmitMessageToDyteHelper:@"SELF_TOGGLE_VIDEO"];
    }
}
- (void)disableAudio {
    if (_audioEnabled) {
        [self emmitMessageToDyteHelper:@"SELF_TOGGLE_AUDIO"];
    }
    
}
- (void)disableVideo {
    if (_videoEnabled) {
        [self emmitMessageToDyteHelper:@"SELF_TOGGLE_VIDEO"];
    }
}

- (void)selfPin {
    [self emmitMessageToDyteHelper:@"SELF_PIN"];
}
- (void)selfUnpin {
    [self emmitMessageToDyteHelper:@"SELF_UNPIN"];
}
- (void)selfScreenSharing {
    NSLog(@"⚠️ Screensharing is currently not supported in iOS");
}

- (void)leaveRoom {
    [self emmitMessageToDyteHelper:@"LEAVE_ROOM"];
}
@end
