package id.kalanusa.flutter_zsdk.zplprinthandler;

import androidx.annotation.NonNull;

import com.zebra.sdk.comm.ConnectionException;

import id.kalanusa.flutter_zsdk.bluetoothconnectionhandler.BluetoothConnectionHandler;
import id.kalanusa.flutter_zsdk.bluetoothconnectionhandler.BluetoothConnectionHolder;
import io.flutter.Log;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;

public class ZplPrintHandler {

    public void handle(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
        printZplOverBluetooth(result, call.argument("zplData"));
    }


    public void printZplOverBluetooth(@NonNull MethodChannel.Result result, String zplData) {
        new Thread(new Runnable() {
            @Override
            public void run() {
//                Log.w("From Native (Android)", "isConnected from zplPrinterHandler " + BluetoothConnectionHolder.connection.isConnected() );
                try {
//                    String zplData = "^XA^PW624^LL800^FO20,20^A0N,25,25^FDThis is a ZPL test.^FS^FO20,60^BQ , 60,10 , , , A ^FD 1234ABC.^FS^XZ";
                    BluetoothConnectionHolder.connection.write(zplData.getBytes());
                } catch (ConnectionException e) {
                    Log.w("From Native (Android)", "something went wrong when sending data to printer");
                    result.error("WRITE_ERROR", "Something went wrong when sending data to printer", e.getMessage());
                }
            }
        }).start();
    }
}
