// This file is root file responsible for responding to events from JS and sending events to JS

#import "DyteHelper.h"
#import <React/RCTConvert.h>
#import "DyteSelfParticipant+Private.h"
#import "DyteParticipants+Private.h"
#import <React/RCTEventEmitter.h>
#import "DyteMeetingInfo+Private.h"
#import "DyteDelegateHandler.h"
#import "DyteDeviceOrientation.h"
//Reasoning behind certain things
//Note: this isn't written by an experienced iOS developer
//Why are we sending
@implementation DyteHelper {
    NSString *_currentEvent;
    DyteMeetingInfo *_meetingInfo;
    DyteSelfParticipant *_selfInfo;
    DyteParticipants *_participants;
    DyteDeviceOrientation *_deviceOrientation;
}

- (instancetype)init
{

    if (self = [super init]) {
        _selfInfo = [DyteSelfParticipant sharedInstance];
        _participants = [DyteParticipants sharedInstance];
        _meetingInfo = [DyteMeetingInfo sharedInstance];
        _deviceOrientation = [DyteDeviceOrientation sharedInstance];
        _currentEvent = nil; //We don't do anything with _current Event
        [self initMessageEmit];
    }
    return self;
}


- (void) dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

+ (BOOL)requiresMainQueueSetup {
    return NO;
}

- (void)initMessageEmit {
    [[NSNotificationCenter defaultCenter] addObserver:self
            selector:@selector(receiveNotification:)
            name:@"DyteHelperToJSNotification"
            object:nil];
}


- (void) receiveNotification:(NSNotification *) notification
{
    if ([[notification name] isEqualToString:@"DyteHelperToJSNotification"]) {
        NSString* type = notification.userInfo[@"type"];
        if ([notification.userInfo objectForKey:@"data"]) {
            NSDictionary* data = notification.userInfo[@"data"];
            [self sendEventWithName:type body:data];
        } else {
            [self sendEventWithName:type body:NULL];
        }
    }
}

RCT_EXPORT_MODULE(DyteHelper)

// Self Participant Method
// -------
RCT_EXPORT_METHOD(SDKSelfVideo:(BOOL)enabled) {
    [_selfInfo setSelfVideo:enabled];
}
//

RCT_EXPORT_METHOD(SDKSelfBasic:
                  (NSString*)id
                  name:(NSString*)name
                  picture:(NSString*)picture
){
    [_selfInfo setUserInfo:id name:name picture:picture];
}

RCT_EXPORT_METHOD(SDKSelfAudio:(BOOL)enabled) {
    [_selfInfo setSelfAudioEnabled:enabled];
}

RCT_EXPORT_METHOD(SDKSelfPin:(BOOL)isPinned) {
    [_selfInfo setSelfPin:isPinned];
}

RCT_EXPORT_METHOD(SDKSelfScreenshare:(BOOL)isScreensharing) {
 //TODO: Implement Screen share
}
// --- End of Self Participant


// DyteMeetingInfo
// ------
RCT_EXPORT_METHOD(SDKMeetingConnect) {
 [_meetingInfo setMeetingConnected:true];
    [[DyteDelegateHandler new] call:NULL andSelector:@selector(meetingConnected)];
}

RCT_EXPORT_METHOD(SDKMeetingDisconnect) {
 [_meetingInfo setMeetingConnected:false];
    [[DyteDelegateHandler new] call:NULL andSelector:@selector(meetingDisconnect)];
    
}

RCT_EXPORT_METHOD(SDKMeetingJoined) {
  [_meetingInfo setMeetingJoined:true];
    [[DyteDelegateHandler new] call:NULL andSelector:@selector(meetingJoined)];
}

RCT_EXPORT_METHOD(SDKMeetingEnded) {

    [_selfInfo setAllValueNil];
    [_participants setAllValueNil];
    [_meetingInfo setAllValueNil];
    
    [[DyteDelegateHandler new] call:NULL andSelector:@selector(meetingEnded)];
  [_meetingInfo setMeetingJoined:false];
}

RCT_EXPORT_METHOD(stopService) {
    [[DyteDelegateHandler new] call:NULL andSelector:@selector(meetingEnded)];

}

RCT_EXPORT_METHOD(SDKActiveSpeaker:(NSString*)peerId) {
    [_meetingInfo setActiveSpeaker:peerId];
    [[DyteDelegateHandler new] call:peerId andSelector:@selector(activeSpeaker)];
}

// --- Dyte Participants
// -------
RCT_EXPORT_METHOD(SDKParticipantLeave:(NSString*)peerId) {
    [_participants onParticipantLeft:peerId];

}

RCT_EXPORT_METHOD(SDKParticipantJoin:
                  (NSString*)id
                  name:(NSString*)name
                  picture:(NSString*)picture
                  clientSpecificId:(NSString*)clientSpecificId
                  audioEnabled:(BOOL)audioEnabled
                  videoEnabled:(BOOL)videoEnabled
                  isPinned:(BOOL)isPinned
                  isScreensharing:(BOOL)isScreensharing
){
    [_participants createParticipant:id name:name picture:picture clientSpecificId:clientSpecificId audioEnabled:audioEnabled videoEnabled:videoEnabled isPinned:isPinned isScreensharing:isScreensharing];
}

RCT_EXPORT_METHOD(SDKParticipantUpdate:
                  (NSString*)id
                  audioEnabled:(BOOL)audioEnabled
                  videoEnabled:(BOOL)videoEnabled
                  isPinned:(BOOL)isPinned
                  isScreensharing:(BOOL)isScreensharing
){
    [_participants updateParticipant:id audioEnabled:audioEnabled videoEnabled:videoEnabled isPinned:isPinned isScreensharing:isScreensharing];
}

// -----
RCT_EXPORT_METHOD(SDKMessage:(NSString*)message) {
    [_meetingInfo setMessage:message];
    [[DyteDelegateHandler new] call:message andSelector:@selector(message)];
}

RCT_EXPORT_METHOD(SDKRoomMessage:(NSString*)roomMessage) {
    [_meetingInfo setRoomMessage:roomMessage];
    [[DyteDelegateHandler new] call:roomMessage andSelector:@selector(roomMessage)];
}


// Device Orientation
RCT_EXPORT_METHOD(isForcedLandscape:(RCTPromiseResolveBlock)resolve
                  reject:(RCTPromiseRejectBlock)reject) {
    BOOL isForcedLandscape = [_deviceOrientation isForcedLandscapeToggled];
    resolve(@{
        @"isWiredHeadsetPluggedIn": isForcedLandscape ? @YES : @NO,
    });
}

RCT_EXPORT_METHOD(forceLandscape) {
    [_deviceOrientation setForceLandscape];
}

RCT_EXPORT_METHOD(resetOrientation) {
    [_deviceOrientation gettOrientation];
}



- (NSArray<NSString *> *)supportedEvents {
    return @[@"SELF_TOGGLE_AUDIO", @"SELF_TOGGLE_VIDEO", @"SELF_PIN", @"SELF_UNPIN", @"SELF_TOGGLE_SCREENSHARING", @"UPDATE_UICONFIG, LEAVE_ROOM"];
}


@end
