package cordova.plugin.netcontroll.integration;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Application;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.util.Printer;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaWebView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.lang.reflect.Method;
//import br.com.execucao.posmp_api.printer.Printer;

public class VeroPOS {
    //Cordova/Java Params
    private Activity mActivity;
    private Context mContext;
    private CordovaInterface mCordova;
    private CordovaWebView mWebView;
//    private br.com. printer;

    public void initialize(CordovaInterface cordova, CordovaWebView webView) {
        mActivity = cordova.getActivity();
        mContext = cordova.getActivity().getApplicationContext();
        mCordova = cordova;
        mWebView = webView;
    }

    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        String callbackContextMessageBase = "Result Vero POS - " + action + ": ";
        if (action.equals("Initialize")) {
            JSONObject params = args.getJSONObject(0);
            try {
                if (supportsPrinter()) {
//                    printer = Printer.getInstance(this);
//                    printer.open();
                } else {
                    callbackContext.error(callbackContextMessageBase + "Error Detail: " + "Printer not supported");
                }
                callbackContext.success(callbackContextMessageBase + "Success");
                return true;
            } catch (Exception error) {
                callbackContext.error(callbackContextMessageBase + "Error Detail: " + error);
            }
        } else if (action.equals("ImpressaoTextoNetPrint")) {
            JSONObject params = args.getJSONObject(0);
            try {

                String dados = params.getString("dados");
                Boolean negrito = false;
                try {
                    negrito = params.getBoolean("negrito");
                }catch (Exception ex){
                    String exStr = ex.toString();
                    String exStr2 = ex.toString();
                }
                String html = cordova.plugin.netcontroll.integration.util.NetControllPrinter.ConverterTextoImpressoToHtml(dados, negrito);
                cordova.plugin.netcontroll.integration.util.NetControllPrinter.ConverterHtmlToBitmap(html, mContext, bitmapResult -> {
                    Bitmap dadosImg = bitmapResult;

                    callbackContext.success(callbackContextMessageBase + "Success");
                });
            } catch (Exception e) {
                callbackContext.error("ImpressaoTextoNetPrintImg error: " + e);
            }
            return true;
        } else if (action.equals("ImpressaoBase64NetPrint")) {
            try{
                JSONObject params = args.getJSONObject(0);
                String base64String = params.getString("base64String");

                Bitmap dadosImg = cordova.plugin.netcontroll.integration.util.NetControllPrinter.ConverterBase64ToBitmap(base64String);
                callbackContext.success(callbackContextMessageBase + "Success");
            } catch (Exception e){
                callbackContext.error("PrintTesteBase64 error: " + e.toString());
            }
            return true;
        }
        return false;
    }


    private boolean supportsPrinter() {

        String[] models = new String[]{"GPOS700", "P2-B", "L3"};

        for (String model : models) {
            if (model.equals(Build.MODEL))
                return true;
        }
        return false;
    }

}