// Copyright 2015-present 650 Industries. All rights reserved.

package versioned.host.exp.exponent.modules.api;

import android.content.Context;
import android.content.res.Resources;
import android.os.Build;
import android.support.annotation.Nullable;
import android.util.DisplayMetrics;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import com.facebook.device.yearclass.YearClass;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.common.MapBuilder;

import org.json.JSONObject;

import javax.inject.Inject;

import host.exp.exponent.ExponentApplication;
import host.exp.exponent.ExponentManifest;
import host.exp.exponent.kernel.Kernel;
import host.exp.exponent.storage.ExponentSharedPreferences;

public class ConstantsModule extends ReactContextBaseJavaModule {

  @Inject
  ExponentSharedPreferences mExponentSharedPreferences;

  private int mStatusBarHeight = 0;
  private final Map<String, Object> mExperienceProperties;
  private String mSessionId = UUID.randomUUID().toString();
  private JSONObject mManifest;

  private static int convertPixelsToDp(float px, Context context) {
    Resources resources = context.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    float dp = px / (metrics.densityDpi / 160f);
    return (int) dp;
  }

  public ConstantsModule(
      ReactApplicationContext reactContext,
      ExponentApplication application,
      Map<String, Object> experienceProperties,
      JSONObject manifest) {
    super(reactContext);
    application.getAppComponent().inject(this);

    mManifest = manifest;

    if (!manifest.has(ExponentManifest.MANIFEST_STATUS_BAR_COLOR)) {
      int resourceId = application.getResources().getIdentifier("status_bar_height", "dimen", "android");
      if (resourceId > 0) {
        int statusBarHeightPixels = application.getResources().getDimensionPixelSize(resourceId);
        // Convert from pixels to dip
        mStatusBarHeight = convertPixelsToDp(statusBarHeightPixels, application);
      }
    } else {
      mStatusBarHeight = 0;
    }
    mExperienceProperties = experienceProperties;
  }

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

  @Nullable
  @Override
  public Map<String, Object> getConstants() {
    Map<String, Object> constants = new HashMap<>();
    constants.put("sessionId", mSessionId);
    constants.put("exponentVersion", Kernel.getVersionName());
    constants.put("statusBarHeight", mStatusBarHeight);
    constants.put("deviceYearClass", YearClass.get(getReactApplicationContext()));
    constants.put("deviceId", mExponentSharedPreferences.getOrCreateUUID());
    constants.put("deviceName", Build.MODEL);
    constants.put("manifest", mManifest.toString());
    constants.put("isDevice", !isRunningOnGenymotion() && !isRunningOnStockEmulator());
    if (mExperienceProperties != null) {
      constants.putAll(mExperienceProperties);
    }
    return constants;
  }

  private static boolean isRunningOnGenymotion() {
    return Build.FINGERPRINT.contains("vbox");
  }

  private static boolean isRunningOnStockEmulator() {
    return Build.FINGERPRINT.contains("generic");
  }
}

@@ -622,24 +622,60 @@
 ct.Inject;%0A%0A
+import host.exp.exponent.Constants;%0A
 import host.
@@ -2992,24 +2992,101 @@
  != null) %7B%0A
+      constants.put(%22appOwnership%22, getAppOwnership(mExperienceProperties));%0A
       consta
@@ -3363,11 +3363,509 @@
 c%22);%0A  %7D
+%0A%0A  private static String getAppOwnership(Map%3CString, Object%3E experienceProperties) %7B%0A    if (experienceProperties.containsKey(Kernel.MANIFEST_URL_KEY)) %7B%0A      String manifestUrl = (String) experienceProperties.get(Kernel.MANIFEST_URL_KEY);%0A%0A      if (Constants.INITIAL_URL == null) %7B%0A        return %22exponent%22;%0A      %7D else if (manifestUrl.equals(Constants.INITIAL_URL)) %7B%0A        return %22standalone%22;%0A      %7D else %7B%0A        return %22guest%22;%0A      %7D%0A    %7D else %7B%0A      return %22exponent%22;%0A    %7D%0A  %7D
 %0A%7D%0A

Add appOwnership constant