package com.pokkt.papreact;

import android.app.Activity;

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.papreact.common.PokktAdsAdapter;
import com.pokkt.sdk.debugging.Logger;

import org.json.JSONObject;


public class PokktNativeExtension extends ReactContextBaseJavaModule {

    // cordova activity
    private Activity reactActivity = null;
    private static ReactApplicationContext reactApplicationContext;

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

    @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("VideoAd_isAdCached")) {
                try {
                    JSONObject jsonObject = new JSONObject(param);
                    String screenName = jsonObject.optString("screenName", "");
                    boolean isRewarded = jsonObject.optBoolean("isRewarded");
                    if (callbackContext != null)
                        callbackContext.invoke(PokktAdsAdapter.isVideoAdCached(screenName, isRewarded));
                } catch (Exception e) {
                    Logger.printStackTrace(e);
                }

                return true;
            }
            if (operation.equals("Interstitial_isAdCached")) {
                try {
                    JSONObject jsonObject = new JSONObject(param);
                    String screenName = jsonObject.optString("screenName", "");
                    boolean isRewarded = jsonObject.optBoolean("isRewarded");
                    if (callbackContext != null)
                        callbackContext.invoke(PokktAdsAdapter.isInterstitialCached(screenName, isRewarded));
                } catch (Exception e) {
                    Logger.printStackTrace(e);
                }
                return true;
            }

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

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

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

    public void notifyNative(String operation, String param) {
        Logger.d("[POKKT-JAVA] received from framework, operation: " + operation + ", param: " + param);
        PokktAdsAdapter.process(operation, param, getReactActivity());
    }

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

    public Activity getReactActivity() {

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