//
//  RCTMPosControllerBcdProxy.m
//  RNBixolonBgateLibs
//
//  Created by OhDonggeon on 2023/11/07.
//

#import <Foundation/Foundation.h>
#import <React/RCTLog.h>
#import "RCTMPosControllerBCDProxy.h"

@import frameworkMPosSDK;

@interface RCTMPosControllerBCDProxy()
@property MPosControllerBCD* bcd;
@end

@implementation RCTMPosControllerBCDProxy

+ (BOOL)requiresMainQueueSetup {
    return NO;
}

- (id)init{
    if((self=[super init])){
        _bcd = [MPosControllerBCD new];
        
#ifdef DEBUG
        [MPosControllerBCD setEnableLog:YES saveFile:NO saveToHex:NO];
#endif
    }
    return self;
}

RCT_EXPORT_METHOD(selectInterface:(NSInteger)interfaceType
                  address:(NSString*)address
                  resolve:(RCTPromiseResolveBlock)resolve
                  rejecter:(RCTPromiseRejectBlock)reject)
{
    MPOS_RESULT result = [_bcd selectInterface:interfaceType address:address];
    NSString* resultMsg = [NSString stringWithFormat:@"RESULT_CODE: %ld",(long)result];
    if(result != MPOS_SUCCESS){
        reject(@"FAIL", resultMsg, nil);
        return;
    }
    resolve([NSNumber numberWithInteger:result]);
}


RCT_EXPORT_METHOD(selectCommandMode:(NSInteger)commandMode
                  resolve:(RCTPromiseResolveBlock)resolve
                  rejecter:(RCTPromiseRejectBlock)reject)
{
    MPOS_RESULT result = [_bcd selectCommandMode:commandMode];
    NSString* resultMsg = [NSString stringWithFormat:@"RESULT_CODE: %ld",(long)result];
    if(result != MPOS_SUCCESS){
        reject(@"FAIL", resultMsg, nil);
        return;
    }
    resolve([NSNumber numberWithInteger:result]);
}

RCT_EXPORT_METHOD(openService:(NSInteger)deviceID
                  timeout:(NSInteger)timeout
                  resolve:(RCTPromiseResolveBlock)resolve
                  rejecter:(RCTPromiseRejectBlock)reject)
{
    [_bcd setTimeout: timeout];
    MPOS_RESULT result = [_bcd openService:deviceID];
    NSString* resultMsg = [NSString stringWithFormat:@"RESULT_CODE: %ld",(long)result];
    if(result != MPOS_SUCCESS){
        reject(@"FAIL", resultMsg, nil);
        return;
    }
    resolve([NSNumber numberWithInteger:result]);
}

RCT_EXPORT_METHOD(closeService:(NSInteger)timeout
                  resolve:(RCTPromiseResolveBlock)resolve
                  rejecter:(RCTPromiseRejectBlock)reject)
{
    MPOS_RESULT result = [_bcd closeService:timeout];
    NSString* resultMsg = [NSString stringWithFormat:@"RESULT_CODE: %ld",(long)result];
    if(result != MPOS_SUCCESS){
        reject(@"FAIL", resultMsg, nil);
        return;
    }
    resolve([NSNumber numberWithInteger:result]);
}

RCT_EXPORT_METHOD(setTransaction:(NSInteger)mode
                  resolve:(RCTPromiseResolveBlock)resolve
                  rejecter:(RCTPromiseRejectBlock)reject)
{
    MPOS_RESULT result = [_bcd setTransaction:mode];
    NSString* resultMsg = [NSString stringWithFormat:@"RESULT_CODE: %ld",(long)result];
    if(result != MPOS_SUCCESS){
        reject(@"FAIL", resultMsg, nil);
        return;
    }
    resolve([NSNumber numberWithInteger:result]);
}

RCT_EXPORT_METHOD(setReadMode:(NSInteger)mode
                  resolve:(RCTPromiseResolveBlock)resolve
                  rejecter:(RCTPromiseRejectBlock)reject)
{
    MPOS_RESULT result = [_bcd setReadMode:mode];
    NSString* resultMsg = [NSString stringWithFormat:@"RESULT_CODE: %ld",(long)result];
    if(result != MPOS_SUCCESS){
        reject(@"FAIL", resultMsg, nil);
        return;
    }
    resolve([NSNumber numberWithInteger:result]);
}

RCT_EXPORT_METHOD(setKeepNetworkConnection:(NSInteger)enable
                  resolve:(RCTPromiseResolveBlock)resolve
                  rejecter:(RCTPromiseRejectBlock)reject)
{
    [_bcd setKeepNetworkConnection:enable];
    resolve([NSNumber numberWithInteger:0]);
}

RCT_EXPORT_METHOD(getDeviceId:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{
    NSInteger deviceId = [_bcd getDeviceId];
    if(deviceId < 0){
        reject(@"FAIL", [NSString stringWithFormat:@"DEVICE_ID: %ld",(long)deviceId], nil);
        return;
    }
    resolve([NSNumber numberWithInteger:deviceId]);
}

RCT_EXPORT_METHOD(isOpen:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{
    NSInteger isOpen = [_bcd isOpen];
    resolve([NSNumber numberWithInteger:isOpen]);
}

RCT_EXPORT_METHOD(getSDKVersion:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{
    resolve([_bcd getSDKVersion]);
}

RCT_EXPORT_METHOD(directIO:(NSArray<NSNumber*>*)data
                  resolve:(RCTPromiseResolveBlock)resolve
                  rejecter:(RCTPromiseRejectBlock)reject)
{
    NSMutableData* dataToSend = [NSMutableData data];
    for(NSNumber* number in data){
        char byte = [number charValue];
        [dataToSend appendBytes: &byte length:1];
    }
    MPOS_RESULT result = [_bcd directIO:dataToSend];
    NSString* resultMsg = [NSString stringWithFormat:@"RESULT_CODE: %ld",(long)result];
    if(result != MPOS_SUCCESS){
        reject(@"FAIL", resultMsg, nil);
        return;
    }
    resolve([NSNumber numberWithInteger:result]);
}

RCT_EXPORT_MODULE();

@end
