package id.kalanusa.flutter_zsdk.bluetoothconnectionhandler;

import android.os.Looper;

import androidx.annotation.NonNull;

import com.zebra.sdk.comm.BluetoothConnectionInsecure;
import com.zebra.sdk.comm.Connection;
import com.zebra.sdk.comm.ConnectionException;

import io.flutter.Log;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;

public class BluetoothConnectionHandler {
    String macAddress;

    public void handle(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {

        switch (call.method) {
            case "bluetoothOpenConnection":
                String macAddressArgument = call.argument("macAddress");

                if (BluetoothConnectionHolder.connection != null) {
                    String connectionInfo = BluetoothConnectionHolder.connection.getSimpleConnectionName();
                    String currentConnectionMacAddress = connectionInfo.substring(0, 17);

                    if (currentConnectionMacAddress != macAddressArgument) {
                        closeAndReopenConnection(result, macAddressArgument);

                    }
                    break;
                }

                if (BluetoothConnectionHolder.connection == null) {
                    BluetoothConnectionHolder.updateConnection(new BluetoothConnectionInsecure(macAddressArgument));

                    openConnection(result);

                }
                break;

            case "isConnected":
                checkConnection(result);
                break;

            case "bluetoothCloseConnection":
                closeConnection(result);
                break;
        }
    }

    public void openConnection(@NonNull MethodChannel.Result result) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Looper.prepare();
                    BluetoothConnectionHolder.connection.open();
                    Looper.myLooper().quit();
                    result.success(null);
                    Log.w("From Native (Android)", "open connection finished");
                } catch (ConnectionException e) {
                    Log.w("From Native (Android)", "Connection Exception threw " + e);
                    result.error("OPEN_CONNECTION_ERROR", "Cant Connect to printers", e.getMessage());
                }
            }
        }).start();
    }

    public void closeAndReopenConnection(@NonNull MethodChannel.Result result, String newMacAddress) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Looper.prepare();
                    BluetoothConnectionHolder.connection.close();
                    BluetoothConnectionHolder.updateConnection(new BluetoothConnectionInsecure(newMacAddress));
                    BluetoothConnectionHolder.connection.open();
                    Looper.myLooper().quit();
                    result.success(null);
                    Log.w("From Native (Android)", "close and reopen connection finished");
                } catch (ConnectionException e) {
                    Log.w("From Native (Android)", "Connection Exception threw " + e);
                    result.error("CLOSE_AND_REOPEN_CONNECTION_ERROR", "Cant close and connect to new printers", e.getMessage());
                }
            }
        }).start();
    }

    public void checkConnection(@NonNull MethodChannel.Result result) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                BluetoothConnectionHolder.getSimpleConnectionName();
                boolean isConnected = BluetoothConnectionHolder.connection.isConnected();

                result.success(isConnected);

                Log.w("From Native (Android)", "isConnected: " + isConnected);
            }
        }).start();
    }

    public void closeConnection(@NonNull MethodChannel.Result result) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    BluetoothConnectionHolder.connection.close();
                    result.success(null);
                } catch (ConnectionException e) {
                    Log.w("From Native (Android)", "something went wrong when closing connection");
                    result.error("CLOSE_CONNECTION_ERROR", "Something went wrong while closing the connection", e.getMessage());
                }
            }
        }).start();
    }


}
