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.bixolon.mpos.MPosControllerBCD;
import com.bixolon.mpos.print.FontAttribute;

import java.io.File;
import java.nio.charset.StandardCharsets;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;

import android.util.Log;


public class MPosControllerBCDProxy extends ReactContextBaseJavaModule {

    private String TAG = MPosControllerBCDProxy.class.getSimpleName();
    private MPosControllerBCD mPosControllerBCD;

    public MPosControllerBCDProxy(ReactApplicationContext reactContext) {
        super(reactContext);
        mPosControllerBCD = new MPosControllerBCD();
    }

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

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

    @ReactMethod
    protected void isOpen(Promise promise){
        try{
            boolean ret = mPosControllerBCD.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 = mPosControllerBCD.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 = mPosControllerBCD.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 = mPosControllerBCD.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 setCharacterset(int codePage,Promise promise){
        try{
            //0 == success
            long ret = mPosControllerBCD.setCharacterset(codePage);
            promise.resolve((int)ret);
        }catch(Exception e){
            promise.reject(e.getMessage());
        }
    }

    @ReactMethod 
    protected void setInternationalCharacterSet(int internationalCharset,Promise promise){
        try{
            //0 == success
            long ret = mPosControllerBCD.setInternationalCharacterset(internationalCharset);
            promise.resolve((int)ret);
        }catch(Exception e){
            promise.reject(e.getMessage());
        }
    }

    @ReactMethod
    protected void displayString(String data, int codePage, int internationalCharacterSet, int line, Promise promise){
        try{
            //0 == success
            long ret = mPosControllerBCD.displayString(data, codePage, internationalCharacterSet, line);
            promise.resolve((int)ret);
        }catch(Exception e){
            promise.reject(e.getMessage());
        }
    }

    @ReactMethod
    protected void cleanScreen(Promise promise){
        try{
            //0 == success
            long ret = mPosControllerBCD.clearScreen();
            promise.resolve((int)ret);
        }catch(Exception e){
            promise.reject(e.getMessage());
        }
    }

    @ReactMethod
    protected void stroreImageFile(String filename, int width, int imageNumber, Promise promise){
        try{
            //0 == success
            long ret = mPosControllerBCD.storeImageFile(filename, width, imageNumber);
            promise.resolve((int)ret);
        }catch(Exception e){
            promise.reject(e.getMessage());
        }
    }

    @ReactMethod
    protected void displayImage(int imageNumber, int xPos, int yPos, Promise promise){
        try{
            //0 == success
            long ret = mPosControllerBCD.displayImage(imageNumber, xPos, yPos);
            promise.resolve((int)ret);
        }catch(Exception e){
            promise.reject(e.getMessage());
        }
    }

    @ReactMethod
    protected void clearImage(int imageNumber, Promise promise){
        try{
            //0 == success
            long ret = mPosControllerBCD.clearImage(false, imageNumber);
            promise.resolve((int)ret);
        }catch(Exception e){
            promise.reject(e.getMessage());
        }
    }
    
}

