package io.scrollback.neighborhoods.modules.appvirality;

import android.app.Activity;
import android.support.annotation.Nullable;

import com.appvirality.AppviralityUI;
import com.appvirality.android.AppviralityAPI;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;

public class AppviralityModule extends ReactContextBaseJavaModule {

    private Activity mCurrentACtivity;

    public AppviralityModule(ReactApplicationContext reactContext, Activity activity) {
        super(reactContext);

        mCurrentACtivity = activity;

        AppviralityAPI.init(reactContext.getApplicationContext());

        AppviralityAPI.claimRewardOnSignUp(reactContext.getApplicationContext(), new AppviralityAPI.RewardClaimed() {
            @Override
            public void OnResponse(boolean isRewarded, String message) {
                WritableMap map = Arguments.createMap();

                map.putBoolean("isRewarded", isRewarded);

                sendEvent("appViralityClaimRewardsOnSignUp", map);
            }
        });
    }

    public String getName() {
        return "AppviralityModule";
    }

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

    @ReactMethod
    public void setUserDetails(final ReadableMap options, final Promise promise) {

        AppviralityAPI.UserDetails details = AppviralityAPI.UserDetails.setInstance(mCurrentACtivity.getApplicationContext());

        if (options.hasKey("email")) {
            details.setUserEmail(options.getString("email"));
        }

        if (options.hasKey("name")) {
            details.setCountry(options.getString("name"));
        }

        if (options.hasKey("storeId")) {
            details.setCountry(options.getString("storeId"));
        }

        if (options.hasKey("profileImage")) {
            details.setCountry(options.getString("profileImage"));
        }

        if (options.hasKey("country")) {
            details.setCountry(options.getString("country"));
        }

        if (options.hasKey("state")) {
            details.setState(options.getString("state"));
        }

        if (options.hasKey("city")) {
            details.setCity(options.getString("city"));
        }

        if (options.hasKey("isExisting")) {
            details.isExistingUser(options.getBoolean("isExisting"));
        }

        if (options.hasKey("pushRegId")) {
            details.setPushRegID(options.getString("pushRegId"));
        }

        details.Update(new AppviralityAPI.UpdateUserDetailsListner() {
            @Override
            public void onSuccess(boolean isSuccess) {
                promise.resolve(isSuccess);
            }
        });
    }

    @ReactMethod
    public void showWelcomeScreen() {
        AppviralityUI.showWelcomeScreen(mCurrentACtivity);
    }

    @ReactMethod
    public void showGrowthHack() {
        AppviralityUI.showGrowthHack(mCurrentACtivity, AppviralityUI.GH.Word_of_Mouth);
    }

    @ReactMethod
    public void showLaunchBar() {
        AppviralityUI.showLaunchBar(mCurrentACtivity, AppviralityUI.GH.Word_of_Mouth);
    }

    @ReactMethod
    public void showLaunchPopup() {
        AppviralityUI.showLaunchPopup(mCurrentACtivity, AppviralityUI.GH.Word_of_Mouth);
    }

    @ReactMethod
    public void saveConversionEvent(String event) {
        AppviralityAPI.saveConversionEvent(event, null, null);
    }
}

@@ -190,30 +190,118 @@
 ity.
-android.AppviralityAPI
+CampaignHandler;%0Aimport com.appvirality.android.AppviralityAPI;%0Aimport com.appvirality.android.CampaignDetails
 ;%0Aim
@@ -342,16 +342,59 @@
 uments;%0A
+import com.facebook.react.bridge.Callback;%0A
 import c
@@ -750,16 +750,45 @@
 odule;%0A%0A
+import java.util.ArrayList;%0A%0A
 public c
@@ -882,24 +882,94 @@
 entACtivity;
+%0A    private ArrayList%3CCallback%3E pendingCallbacks = new ArrayList%3C%3E();
 %0A%0A    public
@@ -1141,49 +1141,558 @@
 API.
-init(reactContext.getApplicationContext()
+setCampaignHandler(mCurrentACtivity, AppviralityAPI.GH.Word_of_Mouth, new AppviralityAPI.CampaignReadyListner() %7B%0A            @Override%0A            public void onCampaignReady(CampaignDetails campaignDetails) %7B%0A                if (campaignDetails != null) %7B%0A                    CampaignHandler.setCampaignDetails(campaignDetails);%0A%0A                    for (Callback callback : pendingCallbacks) %7B%0A                        callback.invoke();%0A                    %7D%0A%0A                    pendingCallbacks.clear();%0A                %7D%0A            %7D%0A        %7D
 );%0A%0A
@@ -2105,32 +2105,100 @@
    %7D%0A        %7D);
+%0A%0A        AppviralityAPI.init(reactContext.getApplicationContext());
 %0A    %7D%0A%0A    publ
@@ -3965,32 +3965,32 @@
 ;%0A            %7D%0A
-
         %7D);%0A    
@@ -3984,32 +3984,337 @@
      %7D);%0A    %7D%0A%0A
+    @ReactMethod%0A    public void onCampaignReady(final Callback callback) %7B%0A        CampaignDetails campaignDetails = CampaignHandler.getCampiagnDetails();%0A%0A        if (campaignDetails != null) %7B%0A            callback.invoke();%0A        %7D else %7B%0A            pendingCallbacks.add(callback);%0A        %7D%0A    %7D%0A%0A
     @ReactMethod

Add onCampaignReady method