package com.rn_bxl;

import android.app.PendingIntent;
import android.content.Intent;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.os.Build;
import android.widget.Toast;


import androidx.annotation.RequiresApi;

import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.Promise;
import com.bixolon.BixolonConst;

import com.bixolon.mpos.config.util.MPosControllerLookup;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;


public class  MPosLookupProxy extends ReactContextBaseJavaModule {

  public MPosLookupProxy(ReactApplicationContext reactContext) {
    super(reactContext);
  }

  @Override
  public String getName() {
    return "MPosLookupProxy";
  }

  @ReactMethod
  protected void getNetworkDevices(Promise promise){
    String noDevice = "[]";
    try{
      MPosControllerLookup.MPosDeviceInfo[] deviceInfos = null;
      int interfaceType = BixolonConst.MPOS_INTERFACE_WIFI;
      //int interfaceType = BixolonConst.MPOS_INTERFACE_ETHERNET;
      if (BixolonConst.MPOS_SUCCESS == MPosControllerLookup.refreshDeviceList(getReactApplicationContext(), interfaceType,3)){
        deviceInfos = MPosControllerLookup.getDeviceList(getReactApplicationContext(), interfaceType);
        if(deviceInfos == null || deviceInfos.length == 0) {
          promise.reject(noDevice);
        }else{
          List deviceList = new ArrayList();
          for(MPosControllerLookup.MPosDeviceInfo devInfo : deviceInfos) {
            if(devInfo.deviceName.isEmpty()){
              deviceList.add(new String("{\"interfacetype\":"  + "\""+ devInfo.interfaceType  + "\"" + ","+
                      "\"address\":" + "\"" + devInfo.ipAddress + "\","+
                      "\"devicename\":" + "\"" + devInfo.ipAddress + "\"}"));
            }else{
              deviceList.add(new String("{\"interfacetype\":"  + "\""+ devInfo.interfaceType  + "\"" + ","+
                      "\"address\":" + "\"" + devInfo.ipAddress + "\","+
                      "\"devicename\":" + "\"" + devInfo.deviceName + "\"}"));
            }
            
          }
          String deviceListJSon = deviceList.toString();
          promise.resolve(deviceListJSon);
        }
      }else{
        promise.resolve(noDevice);
      }
    }catch(Exception e){
      promise.reject(e.getMessage());
    }
  }

  @ReactMethod
  protected void getBluetoothDevices(Promise promise){
    try{
      String noDevice = "[]";
      MPosControllerLookup.MPosDeviceInfo[] deviceInfos = null;
      int interfaceType = BixolonConst.MPOS_INTERFACE_BLUETOOTH;
      deviceInfos = MPosControllerLookup.getDeviceList(getReactApplicationContext(), interfaceType);
      if(deviceInfos == null || deviceInfos.length == 0) {
        promise.reject(noDevice);
      }else{
        List deviceList = new ArrayList();
        for(MPosControllerLookup.MPosDeviceInfo devInfo : deviceInfos) {
          deviceList.add(new String("{\"interfacetype\":"  + "\""+ devInfo.interfaceType  + "\"" + ","+
                  "\"address\":" + "\"" + devInfo.macAddress + "\","+
                  "\"devicename\":" + "\"" + devInfo.deviceName + "\"}"));
        }
        String deviceListJSon = deviceList.toString();
        promise.resolve(deviceListJSon);
      }

    }catch(Exception e){
      promise.reject(e.getMessage());
    }
  }


  @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
  @ReactMethod
  protected void getUSBDevices(Promise promise){
    try{
      UsbManager manager = (UsbManager) getReactApplicationContext().getSystemService(getReactApplicationContext().USB_SERVICE);
      if(manager != null){
        HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
        Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
        List tmp = new ArrayList();
        while(deviceIterator.hasNext()){
          UsbDevice device = deviceIterator.next();
          tmp.add(new String("{\"interfacetype\":"  + "\""+ 16  + "\"" + ","+
                  "\"product\":" + "\"" + device.getProductName() + "\","+
                  "\"devicename\":" + "\"" + device.getDeviceName() + "\"}"));
        }
        String deviceListJSon = tmp.toString();
        promise.resolve(deviceListJSon);
      }
    }catch(Exception e){
      promise.reject(e.getMessage());
    }
  }

}