package id.kalanusa.flutter_zsdk;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Handler;

import androidx.annotation.NonNull;

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.Map;
import java.util.Objects;

import id.kalanusa.flutter_zsdk.bluetoothconnectionhandler.BluetoothConnectionHandler;
import id.kalanusa.flutter_zsdk.bluetoothconnectionhandler.BluetoothConnectionHolder;
import id.kalanusa.flutter_zsdk.bluetoothdiscoveryhandler.BluetoothDiscoveryHandler;
import id.kalanusa.flutter_zsdk.bluetoothdiscoveryhandler.BluetoothPrinter;
import id.kalanusa.flutter_zsdk.zplprinthandler.ZplPrintHandler;
import io.flutter.Log;
import io.flutter.embedding.engine.dart.DartExecutor;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.EventChannel;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry;

/** FlutterZsdkPlugin */
public class FlutterZsdkPlugin implements FlutterPlugin, MethodCallHandler, ActivityAware {
  /// The MethodChannel that will the communication between Flutter and native Android
  ///
  /// This local reference serves to register the plugin with the Flutter Engine and unregister it
  /// when the Flutter Engine is detached from the Activity
  private MethodChannel channel;
  private Context context;
  private Activity activity;
  public static final String bluetoothDiscoveryEventChannel = "id.kalanusa.flutter_zsdk.channel_events/bluetooth_discovery";
  private DartExecutor dartExecutor;
  private BinaryMessenger binaryMessenger;

  public BluetoothConnectionHolder bluetoothConnectionHolder;

  public BluetoothConnectionHandler bluetoothConnectionHandler;
  public ZplPrintHandler zplPrintHandler;


  @Override
  public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
    channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "flutter_zsdk");
    channel.setMethodCallHandler(this);
    context = flutterPluginBinding.getApplicationContext();
    dartExecutor = Objects.requireNonNull(flutterPluginBinding.getFlutterEngine()).getDartExecutor();
    binaryMessenger = dartExecutor.getBinaryMessenger();


    bluetoothConnectionHolder = new BluetoothConnectionHolder(null);
  }

  @Override
  public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) {
    activity = binding.getActivity();

    new EventChannel(binaryMessenger, bluetoothDiscoveryEventChannel).setStreamHandler(new BluetoothDiscoveryHandler(context, activity));
  }

  @Override
  public void onDetachedFromActivityForConfigChanges() {

  }

  @Override
  public void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBinding binding) {

  }

  @Override
  public void onDetachedFromActivity() {
    activity = null;
  }

  @Override
  public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
    Log.w("From Native (Android)", "ini di FlutterZsdkPlugin");

    switch(call.method) {
      case "bluetoothOpenConnection":
      case "isConnected":
      case "bluetoothCloseConnection":
        if (bluetoothConnectionHandler == null) {
          bluetoothConnectionHandler = new BluetoothConnectionHandler();
        }

        bluetoothConnectionHandler.handle(call, result);
        break;

      case "printZplOverBluetooth":
        if (zplPrintHandler == null) {
          zplPrintHandler = new ZplPrintHandler();
        }

        zplPrintHandler.handle(call, result);
        break;
    }
  }

  @Override
  public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
    channel.setMethodCallHandler(null);
  }
}
