package com.ctpodp;

import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.common.MapBuilder;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.facebook.react.uimanager.SimpleViewManager;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.ViewManager;
import com.facebook.react.uimanager.events.RCTEventEmitter;
import com.facebook.react.uimanager.events.RCTModernEventEmitter;
import com.facebook.react.views.image.ReactImageView;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.bridge.WritableMap;
//import com.inpixon.odp.Extend;
//import com.inpixon.odp.TDNavOutput;
//import com.inpixon.odp.aiding.FloorPlan;
//import com.inpixon.odp.aiding.Location;
//import com.inpixon.odp.aiding.Region;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class OdpViewManager extends SimpleViewManager<FrameLayout> {

  public static final String REACT_CLASS = "OdpViewManager";
  public final int COMMAND_START = 1;
  public final int COMMAND_UPLOAD_LOGS = 2;
  ReactApplicationContext _reactContext;
  OdpView odpView;

  public OdpViewManager(ReactApplicationContext reactContext) {
    _reactContext = reactContext;
  }

  @Override
  public String getName() {
    return REACT_CLASS;
  }

  @NonNull
  @Override
  protected FrameLayout createViewInstance(@NonNull ThemedReactContext themedReactContext) {
    odpView = new OdpView(themedReactContext);
    return odpView;
  }

  /**
   * Map the "create" command to an integer
   */
  @Nullable
  @Override
  public Map<String, Integer> getCommandsMap() {
    return MapBuilder.of("start", COMMAND_START, "upload_logs", COMMAND_UPLOAD_LOGS);
  }

  /**
   * Handle "create" command (called from JS) and call createFragment method
   */
  @Override
  public void receiveCommand(
      @NonNull FrameLayout root,
      String commandId,
      @Nullable ReadableArray args) {
    super.receiveCommand(root, commandId, args);
    int commandIdInt = Integer.parseInt(commandId);

    switch (commandIdInt) {
      case COMMAND_START:
        String key = args.getString(0);
        String host = args.getString(1);
        Double delay = args.getDouble(2);
        odpView.start(_reactContext, key, host, delay);
        break;
      case COMMAND_UPLOAD_LOGS:
        odpView.uploadLogs();
        break;
      default: {
      }
    }
  }

}
