package com.rn_bxl;

import com.bixolon.BixolonConst;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.bridge.WritableNativeArray;

import com.bixolon.mpos.MPosControllerScanner;


public class MPosControllerScannerProxy extends ReactContextBaseJavaModule {

    private String TAG = MPosControllerScannerProxy.class.getSimpleName();
    private MPosControllerScanner mPosControllerScanner;

    public MPosControllerScannerProxy(ReactApplicationContext reactContext) {
        super(reactContext);
        mPosControllerScanner = new MPosControllerScanner();
    }

    @Override
    public String getName() {
        return "MPosControllerScannerProxy";
    }

    @ReactMethod
    protected void selectInterface(int interfaceType, String address, Promise promise){
        long ret = -1;
        try{
            ret = mPosControllerScanner.selectInterface(interfaceType,address);
            promise.resolve((int)ret);
        }catch(Exception e){
            promise.reject(e.getMessage());
        }
    }

    @ReactMethod
    protected void selectCommandMode(int commandMode, Promise promise){
        long ret = -1;
        try{
            ret = mPosControllerScanner.selectCommandMode(commandMode);
            promise.resolve((int)ret);
        }catch(Exception e){
            promise.reject(e.getMessage());
        }
    }

    @ReactMethod
    protected void openService(int deviceID, int timeout, Promise promise){
        long ret = -1;
        try{
            ret = mPosControllerScanner.openService(deviceID, timeout, getReactApplicationContext());
            promise.resolve((int)ret);
        }catch(Exception e){
            promise.reject(e.getMessage());
        }
    }

    @ReactMethod
    protected void closeService(int timeout, Promise promise){
        long ret = -1;
        try{
            ret = mPosControllerScanner.closeService(timeout);
            promise.resolve((int)ret);
        }catch(Exception e){
            promise.reject(e.getMessage());
        }
    }

    @ReactMethod
    protected void isOpen(Promise promise){
        try{
            boolean ret = mPosControllerScanner.isOpen();
            if(ret == true){
                promise.resolve(1);
            }else{
                promise.resolve(0);
            }
        }catch(Exception e){
            promise.reject(e.getMessage());
        }
    }


    @ReactMethod
    protected void getDeviceId(Promise promise){
        try{
            long ret = mPosControllerScanner.getDeviceId();
            promise.resolve((int)ret);
        }catch(Exception e){
            promise.reject(e.getMessage());
        }
    }

    @ReactMethod
    protected void setTransaction(int mode,Promise promise){
        try{
            //0 == success
            long ret = mPosControllerScanner.setTransaction(mode);
            promise.resolve((int)ret);
        }catch(Exception e){
            promise.reject(e.getMessage());
        }
    }

    @ReactMethod
    protected void setReadMode(int control,Promise promise){
        try{
            //0 == success
            long ret = mPosControllerScanner.setReadMode(control);
            promise.resolve((int)ret);
        }catch(Exception e){
            promise.reject(e.getMessage());
        }
    }

    @ReactMethod
    protected void setKeepNetworkConnection(int enable,Promise promise){
        try{
            //currently, there's no matched method existed.
            promise.resolve(-1);
        }catch(Exception e){
            promise.reject(e.getMessage());
        }
    }

    @ReactMethod
    protected void getOccuredData(Promise promise){
        try{
            byte[] data = mPosControllerScanner.getOccuredData();
            WritableArray rnRet = new WritableNativeArray();
            for(int i=0; i<data.length; i++){
                rnRet.pushInt((int)data[i]);
            }
            promise.resolve(rnRet);
        }catch(Exception e){
            promise.reject(e.getMessage());
        }
    }
    
}

