package com.volcengine.reactnative.veplayer;

import androidx.annotation.Nullable;
import android.util.Log;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.module.annotations.ReactModule;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.ss.ttvideoengine.TTVideoEngine;
import com.volcengine.reactnative.veplayer.pictureInpicture.PictureInPictureManager;

import com.volcengine.VolcApiEngine.*;
import com.volcengine.reactnative.veplayer.events.ClassHelper;
import com.volcengine.VolcApiEngine.IEventReceiver;

@ReactModule(name = VeplayerModuleSpec.NAME)
public class VeplayerModule extends VeplayerModuleSpec implements IEventReceiver {
  private static final String TAG = "VeplayerModule";
  public static ReactApplicationContext reactApplicationContext = null;

  private final VeplayerImpl impl;
  private int listenerCount = 0;

  VeplayerModule(ReactApplicationContext context) {
    super(context);
    VeplayerModule.reactApplicationContext = context;
    // Provide ReactContext to PiP manager as an optional Activity resolution path.
    PictureInPictureManager.getInstance().setReactContext(context);
    this.impl = new VeplayerImpl(context, this);
  }

  @Override
  public String getName() {
    return VeplayerModuleSpec.NAME;
  }

  @ReactMethod(isBlockingSynchronousMethod = true)
  public boolean newApiEngine() {
    return impl.newApiEngine();
  }

  @ReactMethod(isBlockingSynchronousMethod = true)
  public String callApiSync(ReadableMap arg) {
    try {
      String params = arg.getString("params");
      return impl.callApiSync(params);
    } catch (Exception e) {
      Log.e(TAG, "callApiSync failed", e);
      throw new RuntimeException(e);
    }
  }

  @ReactMethod(isBlockingSynchronousMethod = false)
  public void callApi(ReadableMap arg, Callback callback) {
    try {
      String params = arg.getString("params");
      impl.callApi(params, callback);
    } catch (Exception e) {
      Log.e(TAG, "callApi failed", e);
    }
  }

  @ReactMethod(isBlockingSynchronousMethod = true)
  public void destroyIns() {
    impl.destroyApiEngine();
  }

  @ReactMethod(isBlockingSynchronousMethod = true)
  public void test(ReadableMap arg) {
    TTVideoEngine engine = new TTVideoEngine(null);
    // Placeholder for test usage
  }

  @ReactMethod
  public void addListener(String eventName) {
    if (listenerCount == 0) {
      // Set up any upstream listeners or background tasks as necessary
    }
    listenerCount += 1;
  }

  @ReactMethod
  public void removeListeners(Integer count) {
    listenerCount -= count;
    if (listenerCount == 0) {
      // Remove upstream listeners, stop unnecessary background tasks
    }
  }

  private void sendEvent(ReactContext reactContext, String eventName, @Nullable WritableMap params) {
    reactContext
      .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
      .emit(eventName, params);
  }

  @Override
  public void OnEvent(String event, String data) {
    final WritableMap map = Arguments.createMap();
    map.putString("event", event);
    map.putString("data", data);

    super.context
      .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
      .emit(VeplayerModuleSpec.EVENT, map);
  }
}
