package com.rn_bxl;

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.ReadableArray;
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.bridge.WritableNativeArray;

import com.bixolon.mpos.MPosControllerLabelPrinter;


import java.nio.charset.StandardCharsets;


public class MPosControllerLabelPrinterProxy extends ReactContextBaseJavaModule {

    private final ReactApplicationContext reactContext;
    private MPosControllerLabelPrinter mPosControllerLabelPrinter;

    public MPosControllerLabelPrinterProxy(ReactApplicationContext reactContext) {
        super(reactContext);
        this.reactContext = reactContext;
        mPosControllerLabelPrinter = new MPosControllerLabelPrinter();
    }

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

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

    @ReactMethod
    protected void isOpen(Promise promise){
        try{
            boolean ret = mPosControllerLabelPrinter.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 = mPosControllerLabelPrinter.getDeviceId();
            promise.resolve((int)ret);
        }catch(Exception e){
            promise.reject(e.getMessage());
        }
    }

    @ReactMethod
    protected void directIO(ReadableArray data, Promise promise){
        try{
            byte[] bytes = new byte[data.size()];

            for (int i = 0; i < data.size(); i ++) {
                bytes[i] = (byte) data.getInt(i);
            }

            long ret = mPosControllerLabelPrinter.directIO(bytes);
            promise.resolve((int)ret);
        }catch(Exception e){
            promise.reject(e.getMessage());
        }
    }

    @ReactMethod
    protected void setTransaction(int mode,Promise promise){
        try{
            //0 == success
            long ret = mPosControllerLabelPrinter.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 = mPosControllerLabelPrinter.setReadMode(control);
            promise.resolve((int)ret);
        }catch(Exception e){
            promise.reject(e.getMessage());
        }
    }

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

    @ReactMethod
    protected void printBuffer(int numberOfCopies,Promise promise){
        try{
            long ret = mPosControllerLabelPrinter.printBuffer(numberOfCopies);
            promise.resolve((int)ret);
        }catch(Exception e){
            promise.reject(e.getMessage());
        }
    }

    @ReactMethod
    protected void drawTextDeviceFont(String data,
                                    int xPos,
                                    int yPos,
                                    String fontSelection,
                                    int fontWidth,
                                    int fontHeight,
                                    int rightSpace,
                                    int rotation,
                                    int reverse,
                                    int bold,
                                    int rightToLeft,
                                    int alignment,
                                    Promise promise){
        try{
                boolean bReverse;
                boolean bBold;
                boolean bRightToLeft;
                char charFontSelection = fontSelection.charAt(0);
                if(reverse != 0){
                    bReverse = true;
                }else{
                    bReverse = false;
                }
                if(bold != 0){
                    bBold = true;
                }else{
                    bBold = false;
                }
                if(rightToLeft != 0){
                    bRightToLeft = true;
                }else{
                    bRightToLeft = false;
                }
                long ret = mPosControllerLabelPrinter.drawTextDeviceFont(data,xPos,yPos,charFontSelection,fontWidth,fontHeight,rightSpace,rotation,bReverse,bBold,bRightToLeft,alignment);
                promise.resolve((int)ret);
        }catch(Exception e){
            promise.reject(e.getMessage());
        }
    }

    @ReactMethod
    protected void drawTextVectorFont(String data,
                                        int xPos,
                                        int yPos,
                                        String fontSelection,
                                        int fontWidth,
                                        int fontHeight,
                                        int rightSpace,
                                        int rotation,
                                        int reverse,
                                        int bold,
                                        int italic,
                                        int rightToLeft,
                                        int alignment,
                                        Promise promise){
        try{
            boolean bReverse;
            boolean bBold;
            boolean bItalic;
            boolean bRightToLeft;
            if(reverse != 0){
                bReverse = true;
            }else{
                bReverse = false;
            }
            if(bold != 0){
                bBold = true;
            }else{
                bBold = false;
            }
            if(italic != 0){
                bItalic = true;
            }else{
                bItalic = false;
            }
            if(rightToLeft != 0){
                bRightToLeft = true;
            }else{
                bRightToLeft = false;
            }
            long ret = mPosControllerLabelPrinter.drawTextVectorFont(data,xPos,yPos,fontSelection.charAt(0),fontWidth,fontHeight,rightSpace,rotation,bReverse,bBold,bItalic,bRightToLeft,alignment);
            promise.resolve((int)ret);
        }catch(Exception e){
            promise.reject(e.getMessage());
        }
    }

    @ReactMethod
    protected void drawBarcode1D(String data,
                                int xPos,
                                int yPos,
                                int barcodeType,
                                int widthNarrow,
                                int widthWide,
                                int height,
                                int hri,
                                int quietZoneWidth,
                                int rotation,
                                Promise promise){
        try{
            long ret = mPosControllerLabelPrinter.drawBarcode1D(data,xPos,yPos,barcodeType,widthNarrow,widthWide,height,hri,quietZoneWidth,rotation);
            promise.resolve((int)ret);
        }catch(Exception e){
            promise.reject(e.getMessage());
        }
    }

    @ReactMethod
    protected void drawBarcodeMaxiCode(String data, int xPos, int yPos, int mode, Promise promise){
        try{
            long ret = mPosControllerLabelPrinter.drawBarcodeMaxiCode(data,xPos,yPos,mode);
            promise.resolve((int)ret);
        }catch(Exception e){
            promise.reject(e.getMessage());
        }
    }
    @ReactMethod
    protected void drawBarcodePDF417(String data,
                                    int xPos,
                                    int yPos,
                                    int maximumRowCount,
                                    int maximumColumnCount,
                                    int errorCorrectionLevel,
                                    int dataCompressionMethod,
                                    int hri,
                                    int barcodeOriginPoint,
                                    int moduleWidth,
                                    int barHeight,
                                    int rotation,
                                    Promise promise){
        try{
            boolean hriBool;
            if(hri == 0){
                hriBool = false;
            }else{
                hriBool = true;
            }
            long ret = mPosControllerLabelPrinter.drawBarcodePDF417(data,xPos,yPos,maximumRowCount,maximumColumnCount,errorCorrectionLevel,dataCompressionMethod,hriBool,barcodeOriginPoint,moduleWidth,barHeight,rotation);
            promise.resolve((int)ret);
        }catch(Exception e){
            promise.reject(e.getMessage());
        }
    }

    @ReactMethod
    protected void drawBarcodeQRCode(String data, int xPos, int yPos, int size, int model, int errorCorrectionLevel, int rotation,  Promise promise){
        try{
            long ret = mPosControllerLabelPrinter.drawBarcodeQRCode(data,xPos,yPos,size,model,errorCorrectionLevel,rotation);
            promise.resolve((int)ret);
        }catch(Exception e){
            promise.reject(e.getMessage());
        }
    }


    @ReactMethod
    protected void drawBarcodeDataMatrix(String data, int xPos, int yPos, int size, int reverse, int rotation,  Promise promise){
        try{
            boolean bReverse;
            if(reverse != 0){
                bReverse = true;
            }else{
                bReverse = false;
            }
            long ret = mPosControllerLabelPrinter.drawBarcodeDataMatrix(data,xPos,yPos,size,bReverse,rotation);
            promise.resolve((int)ret);
        }catch(Exception e){
            promise.reject(e.getMessage());
        }
    }

    @ReactMethod
    protected void drawBarcodeAztec(String data,
                                    int xPos,
                                    int yPos,
                                    int size,
                                    int extendedChannel,
                                    int errorCorrectionLevel,
                                    int menuSymbol,
                                    int numberOfSymbols,
                                    String optionalID,
                                    int rotation,
                                    Promise promise){
        try{
            boolean bExtendedChannel;
            boolean bMenuSymbol;
            if(extendedChannel != 0){
                bExtendedChannel = true;
            }else{
                bExtendedChannel = false;
            }
            if(menuSymbol != 0){
                bMenuSymbol = true;
            }else{
                bMenuSymbol = false;
            }
            long ret = mPosControllerLabelPrinter.drawBarcodeAztec(data, xPos, yPos, size, bExtendedChannel, errorCorrectionLevel, bMenuSymbol, numberOfSymbols, optionalID, rotation);
            promise.resolve((int)ret);
        }catch(Exception e){
            promise.reject(e.getMessage());
        }
    }


    @ReactMethod
    protected void drawBarcodeCode49(String data,
                                    int xPos,
                                    int yPos,
                                    int widthNarrow,
                                    int widthWide,
                                    int height,
                                    int hri,
                                    int startingMode,
                                    int rotation,
                                    Promise promise){
        try{
            long ret = mPosControllerLabelPrinter.drawBarcodeCode49(data,
                                                                xPos,
                                                                yPos,
                                                                widthNarrow,
                                                                widthWide,
                                                                height,
                                                                hri,
                                                                startingMode,
                                                                rotation);
            promise.resolve((int)ret);
        }catch(Exception e){
            promise.reject(e.getMessage());
        }
    }

    @ReactMethod
    protected void drawBarcodeCodaBlock(String data,
                                        int xPos,
                                        int yPos,
                                        int widthNarrow,
                                        int widthWide,
                                        int height,
                                        int securityLevel,
                                        int numberOfCharactersPerRow,
                                        char mode,
                                        int rowsToEncode,
                                        Promise promise){
        try{
            boolean bSecurityLevel;
            if(securityLevel == 0){
                bSecurityLevel = false;
            }else{
                bSecurityLevel = true;
            }
            long ret = mPosControllerLabelPrinter.drawBarcodeCodaBlock(data,xPos,yPos,widthNarrow,widthWide,height,bSecurityLevel,numberOfCharactersPerRow,mode,rowsToEncode);
            promise.resolve((int)ret);
        }catch(Exception e){
            promise.reject(e.getMessage());
        }
    }

    @ReactMethod
    protected void drawBarcodeMicroPDF(String data,
                                        int xPos,
                                        int yPos,
                                        int moduleWidth,
                                        int moduleHeight,
                                        int mode,
                                        int rotation,
                                        Promise promise){
        try{
            long ret = mPosControllerLabelPrinter.drawBarcodeMicroPDF(data,
                                                                    xPos,
                                                                    yPos,
                                                                    moduleWidth,
                                                                    moduleHeight,
                                                                    mode,
                                                                    rotation);
        promise.resolve((int)ret);
        }catch(Exception e){
            promise.reject(e.getMessage());
        }
    }

    @ReactMethod
    protected void drawBarcodeIMB(String data,int xPos,int yPos,int hri,int rotation,Promise promise){
        try{
            boolean bHri;
            if(hri == 0){
                bHri = false;
            }else{
                bHri = true;
            }
            long ret = mPosControllerLabelPrinter.drawBarcodeIMB( data, xPos, yPos,bHri, rotation);
            promise.resolve((int)ret);
        }catch(Exception e){
            promise.reject(e.getMessage());
        }
    }

    @ReactMethod
    protected void drawBarcodeMSI(String data,
                                    int xPos,
                                    int yPos,
                                    int narrowWidth,
                                    int widthWide,
                                    int height,
                                    int checkDigitSelection,
                                    int printCheckDigit,
                                    int hri,
                                    int rotation,
                                    Promise promise){
        try{
            boolean bPrintCheckDigit;
            if(printCheckDigit == 0){
                bPrintCheckDigit = false;
            }else{
                bPrintCheckDigit = true;
            }
            long ret = mPosControllerLabelPrinter.drawBarcodeMSI(data,
                                                            xPos,
                                                            yPos,
                                                            narrowWidth,
                                                            widthWide,
                                                            height,
                                                            checkDigitSelection,
                                                            bPrintCheckDigit,
                                                            hri,
                                                            rotation);
            promise.resolve((int)ret);
        }catch(Exception e){
            promise.reject(e.getMessage());
        }
    }

    @ReactMethod
    protected void drawBarcodePlessey(String data,
                                        int xPos,
                                        int yPos,
                                        int widthNarrow,
                                        int widthWide,
                                        int height,
                                        int printCheckDigit,
                                        int hri,
                                        int rotation,
                                        Promise promise){
        try{
            boolean bPrintCheckDigit;
            if(printCheckDigit == 0){
                bPrintCheckDigit = false;
            }else{
                bPrintCheckDigit = true;
            }
            long ret = mPosControllerLabelPrinter.drawBarcodePlessey(data,xPos,yPos, widthNarrow,widthWide,height,bPrintCheckDigit,hri,rotation);
            promise.resolve((int)ret);
        }catch(Exception e){
            promise.reject(e.getMessage());
        }
    }

    @ReactMethod
    protected void drawBarcodeTLC39(String data,
                                    int xPos,
                                    int yPos,
                                    int narrowWidth,
                                    int widthWide,
                                    int height,
                                    int rowHeightMicroPDF417,
                                    int narrowWidthOfMicroPDF417,
                                    int rotation,
                                    Promise promise){
        try{
            long ret = mPosControllerLabelPrinter.drawBarcodeTLC39(data,xPos,yPos,narrowWidth,widthWide,height,rowHeightMicroPDF417,narrowWidthOfMicroPDF417,rotation);
            promise.resolve((int)ret);
        }catch(Exception e){
            promise.reject(e.getMessage());
        }
    }

    @ReactMethod
    protected void drawBarcodeRSS(String data,
                                    int xPos,
                                    int yPos,
                                    int barcodeType,
                                    int magnification,
                                    int separatorHeight,
                                    int barcodeHeight,
                                    int segmentWidth,
                                    int rotation,
                                    Promise promise){
        try{
            long ret = mPosControllerLabelPrinter.drawBarcodeRSS(data,
                                                                xPos,
                                                                yPos,
                                                                barcodeType,
                                                                magnification,
                                                                separatorHeight,
                                                                barcodeHeight,
                                                                segmentWidth,
                                                                rotation);
            promise.resolve((int)ret);
        }catch(Exception e){
            promise.reject(e.getMessage());
        }
    }

    @ReactMethod
    protected void drawBlock(int startPosX,
                            int startPosY,
                            int endPosX,
                            int endPosY,
                            String option,
                            int thickness,
                            Promise promise){
        try{
            long ret = mPosControllerLabelPrinter.drawBlock(startPosX,
                                                            startPosY,
                                                            endPosX,
                                                            endPosY,
                                                            option,
                                                            thickness);
            promise.resolve((int)ret);
        }catch(Exception e){
            promise.reject(e.getMessage());
        }
    }

    @ReactMethod
    protected void drawCircle(int startPosX, int startPosY, int size, int multiplier, Promise promise){
        try{
            //endPosX and endPosY are not supported.
            long ret = mPosControllerLabelPrinter.drawCircle(startPosX, startPosY, size, multiplier);
            promise.resolve((int)ret);
        }catch(Exception e){
            promise.reject(e.getMessage());
        }
    }

    @ReactMethod
    protected void drawImageFile(String filename, int startPosX, int startPosY, int width,int threshold,int ditheringType,int compressType, Promise promise){
        try{
            //int threshold,int ditheringType,and int compressType are not supported.
            long ret = mPosControllerLabelPrinter.drawImageFile(filename, startPosX, startPosY, width);
            promise.resolve((int)ret);
        }catch(Exception e){
            promise.reject(e.getMessage());
        }
    }

    @ReactMethod
    protected void drawBase64Image(String base64String, int startPosX, int startPosY, int width,int threshold,int ditheringType,int compressType, Promise promise){
        try{
            long ret = mPosControllerLabelPrinter.drawBase64Image(base64String, startPosX, startPosY, width,threshold,ditheringType,compressType,true);
            promise.resolve((int)ret);
        }catch(Exception e){
            promise.reject(e.getMessage());
        }
    }

    @ReactMethod
    protected void setKeepNetworkConnection(int enable,Promise promise){
        try{
            mPosControllerLabelPrinter.setKeepNetworkConnection(enable);
            promise.resolve((int)0);
        }catch(Exception e){
            promise.reject(e.getMessage());
        }
    }

    @ReactMethod
    protected void setCharacterset(int characterset, int internationalCharset, Promise promise){
        try{
            long ret =  mPosControllerLabelPrinter.setCharacterset(characterset, internationalCharset);
            promise.resolve((int)ret);
        }catch(Exception e){
            promise.reject(e.getMessage());
        }
    }

    @ReactMethod
    protected void setPrintingType(char printingType, Promise promise){
        try{
            long ret =  mPosControllerLabelPrinter.setPrintingType(printingType);
            promise.resolve((int)ret);
        }catch(Exception e){
            promise.reject(e.getMessage());
        }
    }

    @ReactMethod
    protected void setMargin(int horizontalMargin, int verticalMargin, Promise promise){
        try{
            long ret =  mPosControllerLabelPrinter.setMargin(horizontalMargin,verticalMargin);
            promise.resolve((int)ret);
        }catch(Exception e){
            promise.reject(e.getMessage());
        }
    }

    @ReactMethod
    protected void setLength(int labelLength, int gapLength, String mediaType, int offsetLength, Promise promise){
        try{
            long ret =  mPosControllerLabelPrinter.setLength(labelLength,gapLength,mediaType,offsetLength);
            promise.resolve((int)ret);
        }catch(Exception e){
            promise.reject(e.getMessage());
        }
    }

    @ReactMethod
    protected void setWidth(int labelWidth, Promise promise){
        try{
            long ret =  mPosControllerLabelPrinter.setWidth(labelWidth);
            promise.resolve((int)ret);
        }catch(Exception e){
            promise.reject(e.getMessage());
        }
    }

    @ReactMethod
    protected void setSpeed(int speed, Promise promise){
        try{
            long ret =  mPosControllerLabelPrinter.setSpeed(speed);
            promise.resolve((int)ret);
        }catch(Exception e){
            promise.reject(e.getMessage());
        }
    }

    @ReactMethod
    protected void setDensity(int density, Promise promise){
        try{
            long ret =  mPosControllerLabelPrinter.setDensity(density);
            promise.resolve((int)ret);
        }catch(Exception e){
            promise.reject(e.getMessage());
        }
    }

    @ReactMethod
    protected void setOrientation(String orientation, Promise promise){
        try{
            long ret =  mPosControllerLabelPrinter.setOrientation(orientation.charAt(0));
            promise.resolve((int)ret);
        }catch(Exception e){
            promise.reject(e.getMessage());
        }
    }

    @ReactMethod
    protected void setOffset(int length, Promise promise){
        try{
            long ret =  mPosControllerLabelPrinter.setOffset(length);
            promise.resolve((int)ret);
        }catch(Exception e){
            promise.reject(e.getMessage());
        }
    }

    @ReactMethod
    protected void setCuttingPosition(int cuttingPosition, Promise promise){
        try{
            long ret =  mPosControllerLabelPrinter.setCuttingPosition(cuttingPosition);
            promise.resolve((int)ret);
        }catch(Exception e){
            promise.reject(e.getMessage());
        }
    }

    @ReactMethod
    protected void setAutoCutter(int isAutoCutter,int period, Promise promise){
        try{
            boolean bIsAutoCutter;
            if(isAutoCutter == 0){
                bIsAutoCutter = false;
            }else{
                bIsAutoCutter = true;
            }
            long ret =  mPosControllerLabelPrinter.setAutoCutter(bIsAutoCutter,period);
            promise.resolve((int)ret);
        }catch(Exception e){
            promise.reject(e.getMessage());
        }
    }

    @ReactMethod
    protected void setRewinder(int enable,Promise promise){
        try{
            long ret =  -1;
            if(enable == 0){
                ret = mPosControllerLabelPrinter.setRewinder(false);
            }else{
                ret = mPosControllerLabelPrinter.setRewinder(true);
            }
            promise.resolve((int)ret);
        }catch(Exception e){
            promise.reject(e.getMessage());
        }
    }

    @ReactMethod
    protected void setupRFID(int rfidType, int numberOfRetries, int numberOfLabel, int radioPower, Promise promise){
        try{
            long ret =  mPosControllerLabelPrinter.setupRFID( rfidType, numberOfRetries, numberOfLabel, radioPower);
            promise.resolve((int)ret);
        }catch(Exception e){
            promise.reject(e.getMessage());
        }
    }

    @ReactMethod
    protected void setEPCDataStructure(int totalSize, String fieldSizes, Promise promise){
        try{
            long ret =  mPosControllerLabelPrinter.setEPCDataStructure(totalSize, fieldSizes);
            promise.resolve((int)ret);
        }catch(Exception e){
            promise.reject(e.getMessage());
        }
    }

    @ReactMethod
    protected void setRFIDPosition(int transPosition, Promise promise){
        try{
            long ret =  mPosControllerLabelPrinter.setRFIDPosition(transPosition);
            promise.resolve((int)ret);
        }catch(Exception e){
            promise.reject(e.getMessage());
        }
    }

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

    @ReactMethod
    protected void writeRFID(int dataType, int startingBlockNumber, int dataLength, String data,Promise promise){
        try{
            long ret =  mPosControllerLabelPrinter.writeRFID( dataType,startingBlockNumber , dataLength, data);
            promise.resolve((int)ret);
        }catch(Exception e){
            promise.reject(e.getMessage());
        }
    }

    @ReactMethod
    protected void setRFIDPassword(String oldAccessPwd, String oldKillPwd, String newAccessPwd, String newKillPwd,Promise promise){
        try{
            long ret =  mPosControllerLabelPrinter.setRFIDPassword(oldAccessPwd, oldKillPwd , newAccessPwd, newKillPwd);
            promise.resolve((int)ret);
        }catch(Exception e){
            promise.reject(e.getMessage());
        }
    }


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

    @ReactMethod
    protected void getModelName(Promise promise){
        try{
            String ret = mPosControllerLabelPrinter.getModelName();
            promise.resolve(ret);
        }catch(Exception e){
            promise.reject(e.getMessage());
        }
    }

    @ReactMethod
    protected void getFirmwareVersion(Promise promise){
        try{
            String ret = mPosControllerLabelPrinter.getFirmwareVersion();
            promise.resolve(ret);
        }catch(Exception e){
            promise.reject(e.getMessage());
        }
    }

    @ReactMethod
    protected void getOccuredData(Promise promise){
        try{
            byte[] data = mPosControllerLabelPrinter.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());
        }
    }
}
