
package com.reactlibrary;

import static com.facebook.react.bridge.UiThreadUtil.runOnUiThread;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import android.content.IntentFilter;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;

import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.WritableNativeMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.facebook.react.uimanager.IllegalViewOperationException;

public class RNZebraBarcodeModule extends ReactContextBaseJavaModule {

  private final ReactApplicationContext reactContext;
  private static final String DEFAULT_DATA_STRING_EXTRA = "com.symbol.datawedge.data_string";
  private String mIntentAction = "";
  private String mDataStringExtra = DEFAULT_DATA_STRING_EXTRA;
  private Boolean mBolBroadcast = false;

  public RNZebraBarcodeModule(ReactApplicationContext reactContext) {
    super(reactContext);
    this.reactContext = reactContext;
  }

  @Override
  public String getName() {
    return "RNZebraBarcode";
  }
  private BroadcastReceiver myBroadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
      String action = intent.getAction();
      Bundle b = intent.getExtras();

      if (action.equals(mIntentAction)) {
        //  Received a barcode scan
        try {
          String decodedData = intent.getStringExtra(mDataStringExtra);

          Toast.makeText(reactContext, decodedData,
                  Toast.LENGTH_LONG).show();
          WritableMap params = new WritableNativeMap();
          params.putString("BarcodeValue", decodedData);
          reactContext
                  .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
                  .emit("BarcodeReceived", params);
        } catch (Exception e) {
          Toast.makeText(reactContext, e.getMessage(),
                  Toast.LENGTH_SHORT).show();
        }
      }
    }
  };

  @ReactMethod
  public void startUp(String intentAction, String dataStringExtra) {
    if (!mBolBroadcast) {
      try {
        mIntentAction = intentAction;
        if (dataStringExtra != null && !dataStringExtra.trim().isEmpty()) {
          mDataStringExtra = dataStringExtra;
        } else {
          mDataStringExtra = DEFAULT_DATA_STRING_EXTRA;
        }

        IntentFilter filter = new IntentFilter();
        filter.addCategory(Intent.CATEGORY_DEFAULT);
        filter.addAction(intentAction);
        getReactApplicationContext().registerReceiver(myBroadcastReceiver, filter);

        mBolBroadcast = true;
      } catch (Throwable e) {
        Toast.makeText(reactContext, e.getMessage(),
                Toast.LENGTH_SHORT).show();
      }
    }
  }

  @ReactMethod
  public void stopListening() {
    if (mBolBroadcast) {
      try {
        reactContext.unregisterReceiver(myBroadcastReceiver);
        mBolBroadcast = false;
      } catch (Exception e) {
        Toast.makeText(reactContext, e.getMessage(), Toast.LENGTH_SHORT).show();
      }
    }
  }

  @ReactMethod
  public void hideNavigationBar() {
    try {
      try {
        runOnUiThread(new Runnable() {
          @Override
          public void run() {
            if (getCurrentActivity() != null) {
              int UI_FLAG_HIDE_NAV_BAR = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                      | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                      | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
                      | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;

              View decorView = getCurrentActivity().getWindow().getDecorView();
              decorView.setSystemUiVisibility(UI_FLAG_HIDE_NAV_BAR);
            }
          }
        });
      } catch (IllegalViewOperationException e) {
        WritableMap map = Arguments.createMap();
        map.putBoolean("success", false);
      }
    } catch (Throwable e) {
      Toast.makeText(reactContext, e.getMessage(), Toast.LENGTH_SHORT).show();
    }
  }

  @ReactMethod
  public void showNavigationBar() {
    try {
      runOnUiThread(new Runnable() {
        @Override
        public void run() {
          if (getCurrentActivity() != null) {
            View decorView = getCurrentActivity().getWindow().getDecorView();
            int uiOptions = View.SYSTEM_UI_FLAG_VISIBLE;
            decorView.setSystemUiVisibility(uiOptions);
          }
        }
      });
    } catch (IllegalViewOperationException e) {
      Toast.makeText(reactContext, e.getMessage(), Toast.LENGTH_SHORT).show();
    }
  }
}
