//
//  Helper.m
//  rainbow
//
//  Created by Doha Tubaileh on 8/18/19.
//  Copyright © 2019 Facebook. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <React/RCTBridgeModule.h>
#import <React/RCTLog.h>
#import "Rainbow/Rainbow.h"
#import "IEventEmmiter.h"
#import <React/RCTEventEmitter.h>
#import <React/RCTEventDispatcher.h>

#import "AuthenticationEventsNames.h"
#import "ContactsEventsNames.h"
#import "WebRtcEvents.h"
#import "PBXEventsNames.h"
#import "ConversationEventsNames.h"
#import "CallLogEventsNames.h"
#import "MessageEventsNames.h"
#import "AppConfigEventsNames.h"
#import "BuubleEventsNames.h"
#import "InvitationEventsNames.h"
#import "SearchEventsNames.h"
#import "LoggerEventsNames.h"
#import "ConferenceEventsNames.h"
#import "SharedFileEventsNames.h"

@interface IEventEmmiter ()
@property (nonatomic, strong) RCTBridge *rainbowBridge;
@property (nonatomic, strong) RCTBridge *call_activityBridge;
@property (nonatomic, strong) NSArray *eventsArray;
@property (nonatomic, strong) NSMutableArray *eventQueue;
@property (nonatomic, assign) BOOL bridgeReady;
@property (nonatomic, assign) BOOL  hasListeners;


@end

@implementation IEventEmmiter

RCT_EXPORT_MODULE();
- (instancetype)init {
  self = [super init];
  if (self) {
    _eventQueue = [NSMutableArray new];
    _bridgeReady = NO;
  }
  return self;
}
-(void)startObserving {
    NSLog(@"[IEventEmmiter] startObserving");
    
    _hasListeners = YES;
    
    // If there are queued events, start emitting them after observing starts
    for (NSDictionary *event in _eventQueue) {
        [self sendEventWithName:event[@"name"] body:event[@"body"]];
    }
    
    // Clear the event queue
    [_eventQueue removeAllObjects];
}
-(void)stopObserving {
    NSLog(@"[IEventEmmiter] stopObserving");

    _hasListeners = NO;
    // Remove upstream listeners, stop unnecessary background tasks
}

- (void)setBridge:(RCTBridge *)bridge {
  [super setBridge:bridge];
  _bridgeReady = YES;

  // Flush queued events once the bridge is ready
  for (NSDictionary *event in _eventQueue) {
    [self sendEventWithName:event[@"name"] body:event[@"body"]];
  }
  [_eventQueue removeAllObjects];
}

- (void)sendEvent:(NSString *)eventName eventBody:(id)eventBody {
    if (self.bridge && _bridgeReady) {
        NSLog(@"[IEventEmitter] sendEvent %@", eventName);
        
        // Ensure listeners are registered before sending the event
        if (self.hasListeners) {
            [self sendEventWithName:eventName body:eventBody];
        } else {
            [_eventQueue addObject:@{@"name": eventName, @"body": eventBody}];
                        NSLog(@"[IEventEmitter] No listeners registered for event %@", eventName);
            NSLog(@"[IEventEmitter] No listeners registered for event %@", eventName);
        }
    } else {
        [_eventQueue addObject:@{@"name": eventName, @"body": eventBody}];
        NSLog(@"[IEventEmitter] Bridge is not set, cannot send event %@", eventName);
    }
}


- (NSArray<NSString *> *)supportedEvents {
    NSLog(@"[IEventEmmiter] supportedEvents %lu.", (unsigned long)_eventsArray.count);

        if (!_eventsArray && _eventsArray.count == 0) {
            _eventsArray = [NSArray array];
            // Authentication
            _eventsArray = [_eventsArray arrayByAddingObjectsFromArray:AuthenticationEventsArray];
            // Contacts
            _eventsArray = [_eventsArray arrayByAddingObjectsFromArray:ContactsEventsArray];
            // WebRTC
            _eventsArray = [_eventsArray arrayByAddingObjectsFromArray:WebRtcEventsArray];
            // PBX
            _eventsArray = [_eventsArray arrayByAddingObjectsFromArray:PBXEventsArray];
            // Conversations
            _eventsArray = [_eventsArray arrayByAddingObjectsFromArray:ConversationEventsArray];
            // Call Logs
            _eventsArray = [_eventsArray arrayByAddingObjectsFromArray:CallLogEventsArray];
            // Messages
            _eventsArray = [_eventsArray arrayByAddingObjectsFromArray:MessageEventsArray];
            // App Config
            _eventsArray = [_eventsArray arrayByAddingObjectsFromArray:AppConfigurationArray];
            // Bubbles
            _eventsArray = [_eventsArray arrayByAddingObjectsFromArray:BuubleEventsNamesArray];
            // Invitations
            _eventsArray = [_eventsArray arrayByAddingObjectsFromArray:InvitationEventNameArray];
            // Search
            _eventsArray = [_eventsArray arrayByAddingObjectsFromArray:SearchEventsArray];
            // Logger
            _eventsArray = [_eventsArray arrayByAddingObjectsFromArray:LoggerEventsArray];
            // Conference
            _eventsArray = [_eventsArray arrayByAddingObjectsFromArray:ConferenceEventsArray];
            // Shared File
            _eventsArray = [_eventsArray arrayByAddingObjectsFromArray:SharedFileEventsArray];
        }
        return _eventsArray;
}

+ (BOOL)requiresMainQueueSetup {
    return NO;
}


@end

