package com.pokkt.plugin.papreact;

import android.app.Activity;

import androidx.annotation.NonNull;

import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.pokkt.PokktAds;
import com.pokkt.plugin.common.PokktAdsAdapter;

import org.json.JSONObject;


public class PokktNativeExtension extends ReactContextBaseJavaModule {

    private Activity reactActivity = null;
    private static ReactApplicationContext reactApplicationContext;

    PokktNativeExtension(ReactApplicationContext reactContext) {
        super(reactContext);
        reactApplicationContext = reactContext;
    }

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

    @ReactMethod
    public boolean execute(String operation, String param, Callback callbackContext) {
        try {
            if (operation.equals("getSDKVersion")) {
                if (callbackContext != null)
                    callbackContext.invoke(String.valueOf(getSDKVersionOnNative()));
                return true;
            }
            if (operation.equals("isAdCached")) {
                try {
                    JSONObject jsonObject = new JSONObject(param);
                    String screenId = jsonObject.optString("screenId", "");
                    if (callbackContext != null)
                        callbackContext.invoke(PokktAdsAdapter.isAdCached(screenId));
                } catch (Exception e) {
                    PokktAds.Debugging.printStackTrace("",e);
                }

                return true;
            }

            notifyNative(operation, param);
            if (callbackContext != null) {
                callbackContext.invoke();
            }
            return true;

        } catch (Exception e) {
            PokktAds.Debugging.logError(e.getMessage());
            if (callbackContext != null)
                callbackContext.invoke("Unknown operation requested : " + operation);
            return false;
        }
    }

    public static void notifyFramework(final String operation, final String param) {
        PokktAds.Debugging.log("[POKKT-JAVA] notifying framework of operation: " + operation + ", param: " + param);
        reactApplicationContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(operation, param);
    }

    private void notifyNative(String operation, String param) {
        PokktAds.Debugging.log("[POKKT-JAVA] received from framework, operation: " + operation + ", param: " + param);
        PokktAdsAdapter.process(operation, param, getReactActivity());
    }

    private static String getSDKVersionOnNative() {
        return PokktAdsAdapter.getSDKVersion();
    }

    private Activity getReactActivity() {

        if (getCurrentActivity() == null) {
            PokktAds.Debugging.logError("React Activity doesn't exist");
        } else {
            reactActivity = getCurrentActivity();
        }
        return reactActivity;
    }
}
