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.MPosControllerDallasKey;

public class MPosControllerDallasKeyProxy extends ReactContextBaseJavaModule {

    private String TAG = MPosControllerDallasKeyProxy.class.getSimpleName();
    private MPosControllerDallasKey mPosControllerDallasKey;

    public MPosControllerDallasKeyProxy(ReactApplicationContext reactContext) {
        super(reactContext);
        mPosControllerDallasKey = new MPosControllerDallasKey();
    }

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

    @ReactMethod
    protected void selectInterface(int interfaceType, String address, Promise promise){
        long ret = -1;
        try{
            ret = mPosControllerDallasKey.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 = mPosControllerDallasKey.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{
            if (deviceID <10 || deviceID > 19 ) {
                ret = mPosControllerDallasKey.openService(getReactApplicationContext());
            }else{
                ret = mPosControllerDallasKey.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 = mPosControllerDallasKey.closeService(timeout);
            promise.resolve((int)ret);
        }catch(Exception e){
            promise.reject(e.getMessage());
        }
    }

    @ReactMethod
    protected void isOpen(Promise promise){
        try{
            boolean ret = mPosControllerDallasKey.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 = mPosControllerDallasKey.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 = mPosControllerDallasKey.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 = mPosControllerDallasKey.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 = mPosControllerDallasKey.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());
        }
    }
    
}

