package it.zenitlab.cordova.plugins.zbtprinter;

import java.io.IOException;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;
import android.util.Log;
import com.zebra.sdk.comm.BluetoothConnectionInsecure;
import com.zebra.sdk.comm.Connection;
import com.zebra.sdk.comm.ConnectionException;
import com.zebra.sdk.printer.PrinterStatus;
import com.zebra.sdk.printer.ZebraPrinter;
import com.zebra.sdk.printer.ZebraPrinterFactory;
import com.zebra.sdk.printer.ZebraPrinterLanguageUnknownException;

public class ZebraBluetoothPrinter extends CordovaPlugin {

    private static final String LOG_TAG = "ZebraBluetoothPrinter";
    String mac = "AC:3F:A4:1D:7A:5C";

    public ZebraBluetoothPrinter() {
    }

    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {

        if (action.equals("print")) {
            try {
                String msg = args.getString(0);
                sendData(callbackContext, msg);
            } catch (IOException e) {
                Log.e(LOG_TAG, e.getMessage());
                e.printStackTrace();
            }
            return true;
        }
        return false;
    }

    /*
     * This will send data to be printed by the bluetooth printer
     */
    void sendData(final CallbackContext callbackContext, final String msg) throws IOException {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    // Instantiate insecure connection for given Bluetooth MAC Address.
                    Connection thePrinterConn = new BluetoothConnectionInsecure(mac);

                    // Verify the printer is ready to print
                    if (isPrinterReady(thePrinterConn)) {

                        // Open the connection - physical connection is established here.
                        thePrinterConn.open();

                        // Send the data to printer as a byte array.
//                        thePrinterConn.write("^XA^FO0,20^FD^FS^XZ".getBytes());
                        thePrinterConn.write(msg.getBytes());


                        // Make sure the data got to the printer before closing the connection
                        Thread.sleep(500);

                        // Close the insecure connection to release resources.
                        thePrinterConn.close();
                        callbackContext.success("Stampa terminata");
                    }
                } catch (Exception e) {
                    // Handle communications error here.
                    callbackContext.error(e.getMessage());
                }
            }
        }).start();
    }

    private Boolean isPrinterReady(Connection connection) {
        Boolean isOK = false;
        try {
            connection.open();
            // Creates a ZebraPrinter object to use Zebra specific functionality like getCurrentStatus()
            ZebraPrinter printer = ZebraPrinterFactory.getInstance(connection);

            PrinterStatus printerStatus = printer.getCurrentStatus();
            if (printerStatus.isReadyToPrint) {
                isOK = true;
            } else if (printerStatus.isPaused) {
                System.out.println("Cannot Print because the printer is paused.");
            } else if (printerStatus.isHeadOpen) {
                System.out.println("Cannot Print because the printer media door is open.");
            } else if (printerStatus.isPaperOut) {
                System.out.println("Cannot Print because the paper is out.");
            } else {
                System.out.println("Cannot Print.");
            }
        } catch (ConnectionException e) {
            e.printStackTrace();
        } catch (ZebraPrinterLanguageUnknownException ex) {
            ex.printStackTrace();
        }
        return isOK;
    }
}


@@ -2559,32 +2559,99 @@
                %7D
+ else %7B%0A%09%09%09%09%09%09callbackContext.error(%22printer is not ready%22);%0A%09%09%09%09%09%7D
 %0A               
@@ -2907,16 +2907,81 @@
 nection)
+ throws ConnectionException, ZebraPrinterLanguageUnknownException
  %7B%0A     
@@ -3005,34 +3005,16 @@
  false;%0A
-        try %7B%0A    
         
@@ -3028,28 +3028,24 @@
 ion.open();%0A
-    
         // C
@@ -3141,20 +3141,16 @@
         
-    
 ZebraPri
@@ -3209,25 +3209,24 @@
 ction);%0A
-%0A
         
     Prin
@@ -3217,20 +3217,16 @@
         
-    
 PrinterS
@@ -3279,28 +3279,24 @@
 ();%0A        
-   
- 
 if (printerS
@@ -3331,20 +3331,16 @@
         
-    
 isOK = t
@@ -3344,36 +3344,32 @@
 = true;%0A        
- 
-   
 %7D else if (print
@@ -3401,37 +3401,44 @@
         
-    System.out.printl
+throw new ConnectionExceptio
 n(%22Canno
@@ -3479,36 +3479,32 @@
 sed.%22);%0A        
-  
-  
 %7D else if (print
@@ -3538,37 +3538,44 @@
         
-    System.out.printl
+throw new ConnectionExceptio
 n(%22Canno
@@ -3629,28 +3629,24 @@
 %22);%0A        
-  
-  
 %7D else if (p
@@ -3684,37 +3684,44 @@
         
-    System.out.printl
+throw new ConnectionExceptio
 n(%22Canno
@@ -3757,36 +3757,32 @@
 out.%22);%0A        
-    
 %7D else %7B%0A       
@@ -3790,229 +3790,53 @@
     
-    System.out.println(%22Cannot Print.%22);%0A            %7D%0A        %7D catch (ConnectionException e) %7B%0A            e.printStackTrace();%0A        %7D catch (ZebraPrinterLanguageUnknownException ex) %7B%0A            ex.printStackTrace(
+throw new ConnectionException(%22Cannot Print.%22
 );%0A 

manage connection exception