//
//  RNCCManager.m
//  RNContextCenter
//
//  Created by Shaffiulla Khan on 07/01/19.
//  Copyright © 2019 Impekable LLC. All rights reserved.
//

#import "RNCCManager.h"
#import "RNCCClient.h"
#import <React/RCTUtils.h>
#import <React/RCTEventDispatcher.h>
#import "RCTConvert+ContextCenter.h"

@interface RNCCManager() <TwilioAccessManagerDelegate>
@end

@implementation RNCCManager
@synthesize bridge = _bridge;
@synthesize twilioAccessManager;

#pragma mark Shared / Singleton Methods
+ (instancetype)shared {
    static RNCCManager *sharedManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedManager = [[self alloc] init];
    });
    return sharedManager;
}

- (NSArray<NSString *> *)supportedEvents {
    return @[];
}
RCT_EXPORT_MODULE(CCManager)

#pragma mark TwilioAccessManager Util function
RCT_REMAP_METHOD(initWithToken, token:(NSString *)token resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject){
    RNCCManager *manager = [RNCCManager shared];
    manager.twilioAccessManager = [TwilioAccessManager accessManagerWithToken:token delegate:self];
    resolve([RCTConvert twilioAccessManager:manager.twilioAccessManager]);
}

RCT_REMAP_METHOD(updateToken, token:(NSString *)token updateToken_resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject){
    RNCCManager *manager = [RNCCManager shared];
    [manager.twilioAccessManager updateToken:token];
    resolve([RCTConvert twilioAccessManager:manager.twilioAccessManager]);
}

RCT_EXPORT_METHOD(setupTwilio){
    RNCCManager *manager = [RNCCManager shared];
    __weak RNCCClient *ccClient = [RNCCClient shared];
    [manager.twilioAccessManager registerClient:ccClient.twilioClient forUpdates:^(NSString * _Nonnull token) {
        [ccClient.twilioClient updateToken:token completion:^(TCHResult * _Nonnull result) {
            if (!result.isSuccessful) {
                NSLog(@"error updating token: %@", result.error);
            }
        }];
    }];
}

#pragma mark TwilioAccessManager Delagate
/** Called when the access token is within 3 minutes of expiring.
 
 @param accessManager The access manager instance that needs to be updated.
 */
- (void)accessManagerTokenWillExpire:(nonnull TwilioAccessManager *)accessManager{
    NSLog(@"accessManagerTokenWillExpire");
    [self.bridge.eventDispatcher sendAppEventWithName:@"accessManagerTokenWillExpire" body:nil];
}

/** Called when the access token has expired.  May be called after expiry if the application
 was in backgrounded at the time of expiration.
 
 @param accessManager The access manager instance that needs to be updated.
 */
- (void)accessManagerTokenExpired:(nonnull TwilioAccessManager *)accessManager{
    NSLog(@"accessManagerTokenExpired");
    [self.bridge.eventDispatcher sendAppEventWithName:@"accessManagerTokenExpired" body:nil];
}

/** Called if an access token is provided that is in an invalid format.
 
 @param accessManager The access manager instance that experienced the error.
 */
- (void)accessManagerTokenInvalid:(nonnull TwilioAccessManager *)accessManager{
    NSLog(@"accessManagerTokenInvalid");
    [self.bridge.eventDispatcher sendAppEventWithName:@"accessManagerTokenInvalid" body:nil];
}


@end
