//
//  AlanManager.m
//
//  Created by Alan AI, Inc on 30.10.2019.
//

#import "AlanSDK.h"
#import "AlanManager.h"

@implementation AlanManager

RCT_EXPORT_MODULE();

- (dispatch_queue_t)methodQueue
{
    return dispatch_get_main_queue();
}

+ (instancetype)shared
{
    static AlanManager *shared = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        shared = [[self alloc] init];
        [AlanLog setEnableLogging:YES];
    });
    return shared;
}

RCT_EXPORT_METHOD(playText:(NSString *)text)
{
    if( AlanManager.shared.alanButton == nil )
    {
        return;
    }
    [AlanManager.shared.alanButton playText:text];
}

RCT_EXPORT_METHOD(playData:(NSDictionary *)data)
{
    if( AlanManager.shared.alanButton == nil )
    {
        return;
    }
    [AlanManager.shared.alanButton playData:data];
}

RCT_EXPORT_METHOD(showButton)
{
    if( AlanManager.shared.alanButton == nil )
    {
        return;
    }
    AlanManager.shared.alanButton.hidden = NO;
}

RCT_EXPORT_METHOD(hideButton)
{
    if( AlanManager.shared.alanButton == nil )
    {
        return;
    }
    AlanManager.shared.alanButton.hidden = YES;
}

RCT_EXPORT_METHOD(callProjectApi:(NSString*)method params:(NSDictionary*)params callback:(RCTResponseSenderBlock)callback)
{
    if( AlanManager.shared.alanButton == nil )
    {
        return;
    }
    [AlanManager.shared.alanButton call:method withParams:params callback:^(NSError *error, NSString *object) {
        if( error )
        {
            callback(@[error, [NSNull null]]);
        }
        else
        {
            callback(@[[NSNull null], object]);
        }
    }];
}

RCT_EXPORT_METHOD(setVisualState:(NSDictionary*)visual)
{
    if( AlanManager.shared.alanButton == nil )
    {
        return;
    }
    [AlanManager.shared.alanButton setVisual:visual];
}

RCT_EXPORT_METHOD(activate)
{
    if( AlanManager.shared.alanButton == nil )
    {
        return;
    }
    [AlanManager.shared.alanButton activate];
}

RCT_EXPORT_METHOD(deactivate)
{
    if( AlanManager.shared.alanButton == nil )
    {
        return;
    }
    [AlanManager.shared.alanButton deactivate];
}

@end


