package com.segment.analytics;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import com.localytics.android.LocalyticsAmpSession;
import java.util.Map;

import static com.segment.analytics.Utils.hasPermission;
import static com.segment.analytics.Utils.isNullOrEmpty;

/**
 * Localytics is a general-purpose mobile analytics tool that measures customer acquisition, ad
 * attribution, retargeting campaigns and user actions in your mobile apps.
 *
 * @see <a href="http://www.localytics.com/">Localytics</a>
 * @see <a href="https://segment.io/docs/integrations/localytics/">Localytics Integration</a>
 * @see <a href="http://www.localytics.com/docs/android-integration/">Localytics Android SDK</a>
 */
class LocalyticsIntegrationAdapter extends AbstractIntegrationAdapter<LocalyticsAmpSession> {
  LocalyticsAmpSession session;

  @Override void initialize(Context context, JsonMap settings)
      throws InvalidConfigurationException {
    if (!hasPermission(context, Manifest.permission.WAKE_LOCK)) {
      throw new InvalidConfigurationException("localytics requires the wake lock permission");
    }
    session = new LocalyticsAmpSession(context, settings.getString("appKey"));
  }

  @Override LocalyticsAmpSession getUnderlyingInstance() {
    return session;
  }

  @Override String key() {
    return "Localytics";
  }

  @Override void onActivityCreated(Activity activity, Bundle savedInstanceState) {
    super.onActivityCreated(activity, savedInstanceState);
    session.open();
    session.upload();
  }

  @Override void onActivityResumed(Activity activity) {
    super.onActivityResumed(activity);
    session.open();
    session.upload();
    if (activity instanceof FragmentActivity) session.attach((FragmentActivity) activity);
  }

  @Override void onActivityPaused(Activity activity) {
    super.onActivityPaused(activity);
    if (activity instanceof FragmentActivity) session.detach();
    session.close();
    session.upload();
  }

  @Override void flush() {
    super.flush();
    session.upload();
  }

  @Override void optOut(boolean optOut) {
    super.optOut(optOut);
    session.setOptOut(optOut);
  }

  @Override void identify(IdentifyPayload identify) {
    super.identify(identify);
    Traits traits = identify.traits();

    session.setCustomerId(identify.userId());

    String email = traits.email();
    if (!isNullOrEmpty(email)) session.setCustomerEmail(email);

    String name = traits.name();
    if (!isNullOrEmpty(name)) session.setCustomerName(name);

    for (Map.Entry<String, Object> entry : traits.entrySet()) {
      session.setCustomerData(entry.getKey(), String.valueOf(entry.getValue()));
    }
  }

  @Override void screen(ScreenPayload screen) {
    super.screen(screen);
    session.tagScreen(screen.event());
  }

  @Override void track(TrackPayload track) {
    super.track(track);
    session.tagEvent(track.event(), track.properties().toStringMap());
  }
}

@@ -141,56 +141,8 @@
 le;%0A
-import android.support.v4.app.FragmentActivity;%0A
 impo
@@ -1734,32 +1734,55 @@
 vity instanceof 
+android.support.v4.app.
 FragmentActivity
@@ -1774,32 +1774,40 @@
 ragmentActivity)
+ %7B%0A     
  session.attach(
@@ -1807,16 +1807,39 @@
 attach((
+android.support.v4.app.
 Fragment
@@ -1859,16 +1859,22 @@
 ivity);%0A
+    %7D%0A
   %7D%0A%0A  @
@@ -1991,16 +1991,39 @@
 tanceof 
+android.support.v4.app.
 Fragment
@@ -2031,16 +2031,24 @@
 ctivity)
+ %7B%0A     
  session
@@ -2054,24 +2054,30 @@
 n.detach();%0A
+    %7D%0A
     session.

Use fully qualified names to avoid overloading logs