package id.kalanusa.flutter_zsdk.bluetoothdiscoveryhandler;

import android.app.Activity;
import android.content.Context;

import com.zebra.sdk.comm.ConnectionException;
import com.zebra.sdk.printer.discovery.BluetoothDiscoverer;
import com.zebra.sdk.printer.discovery.DiscoveredPrinter;
import com.zebra.sdk.printer.discovery.DiscoveryHandler;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import io.flutter.Log;
import io.flutter.plugin.common.EventChannel;

public class BluetoothDiscoveryHandler implements EventChannel.StreamHandler {

    public BluetoothDiscoveryHandler(Context context, Activity activity) {
        this.context = context;
        this.activity = activity;
    }

    private Context context;
    private Activity activity;
    private EventChannel.EventSink eventSink;
//    ArrayList<BluetoothPrinter> discoveredBluetoothPrinterList = new ArrayList<BluetoothPrinter>();
    List<Map<String, String>> discoveredBluetoothPrinters = new ArrayList<>();

    @Override
    public void onListen(Object arguments, EventChannel.EventSink events) {
        eventSink = events;
        eventSink.success("SOS");


        startDiscoverBluetoothPrinters();
    }

    @Override
    public void onCancel(Object arguments) {
        Log.w("From Native", "Canceling Discovery Bluetooth Subscription");
        discoveredBluetoothPrinters.clear();
        eventSink.success("EOS");
        eventSink = null;
//        context = null;
//        activity = null;
    }

    private void startDiscoverBluetoothPrinters() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                DiscoveryHandler discoveryHandler = new DiscoveryHandler() {
                    @Override
                    public void foundPrinter(DiscoveredPrinter discoveredPrinter) {
                        Map<String, String> discoveredPrinterDataMap = discoveredPrinter.getDiscoveryDataMap();
                        Log.w("From Native (Android)","Found printer " + discoveredPrinterDataMap.get("FRIENDLY_NAME"));

                        BluetoothPrinter discoveredBluetoothPrinter = new BluetoothPrinter(discoveredPrinterDataMap.get("FRIENDLY_NAME"), discoveredPrinterDataMap.get("MAC_ADDRESS"));
                        discoveredBluetoothPrinters.add(discoveredBluetoothPrinter.toMap());

//                        List<Map<String, String>> convertedDiscoveredPinterList = new ArrayList<>();
//
//                        for(BluetoothPrinter bluetoothPrinter : discoveredBluetoothPrinterList) {
//                            convertedDiscoveredPinterList.add(bluetoothPrinter.toMap());
//                        }

                        activity.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                eventSink.success(discoveredBluetoothPrinters);
                            }
                        });
                    }

                    @Override
                    public void discoveryFinished() {
                        Log.w("From Native (Android)", "Bluetooth Discovery Finished");

                        activity.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                eventSink.success(discoveredBluetoothPrinters);
                                eventSink.success("EOS");
                            }
                        });
                    }

                    @Override
                    public void discoveryError(String s) {
                        Log.w("From Native (Android)", "Bluetooth Discovery Error" + s);
                        activity.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                eventSink.error("DISCOVERY_ERROR", "Something went wrong, message" + s, s);
                            }
                        });
                    }
                };

                try {
                    BluetoothDiscoverer.findPrinters(context, discoveryHandler);
                    Log.w("From Native (Android)", "Bluetooth Discovery Started");
                } catch (ConnectionException e) {
                    Log.w("From Native (Android)", "Bluetooth Discovery Error <ConnectionException>");
                    activity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            eventSink.error("DISCOVERY_ERROR", "Something went wrong", e);
                        }
                    });
                }

            }
        }).start();
    }
}
