package com.mattprecious.telescope;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.util.DisplayMetrics;
import android.util.Log;

/**
 * A basic {@link Lens} implementation that composes an email with the provided addresses and
 * subject (optional). The body will be pre-populated with app and device info:
 *
 * <ul>
 *   <li>App version</li>
 *   <li>App version code</li>
 *   <li>Device manufacturer</li>
 *   <li>Device model</li>
 *   <li>Screen resolution</li>
 *   <li>Screen density</li>
 *   <li>Android version</li>
 *   <li>Android API level</li>
 * </ul>
 */
public class EmailDeviceInfoLens extends EmailLens {
  private static final String TAG = "EmailDeviceInfoLens";

  private final Context context;
  private final String version;
  private final int versionCode;
  private final String body;

  /** @deprecated Use {@link #EmailDeviceInfoLens(Context, String, String...)}. */
  @Deprecated
  public EmailDeviceInfoLens(Context context, String[] addresses, String subject) {
    this(context, subject, addresses);
  }

  public EmailDeviceInfoLens(Context context, String subject, String... addresses) {
    super(context, subject, addresses);
    this.context = context;

    PackageInfo packageInfo = null;
    try {
      packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
    } catch (PackageManager.NameNotFoundException e) {
      Log.e(TAG, "Unable to get app info");
    }

    if (packageInfo == null) {
      version = "0";
      versionCode = 0;
    } else {
      version = packageInfo.versionName;
      versionCode = packageInfo.versionCode;
    }

    this.body = makeBody();
  }

  @Override protected String getBody() {
    return body;
  }

  private String makeBody() {
    DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
    String densityBucket = getDensityString(displayMetrics);

    return "Version: " + version + '\n' //
        + "Version code: " + versionCode + '\n' //
        + "Make: " + Build.MANUFACTURER + '\n' //
        + "Model: " + Build.MODEL + '\n' //
        + "Resolution: " + displayMetrics.heightPixels + 'x' + displayMetrics.widthPixels + '\n' //
        + "Density: " + displayMetrics.densityDpi + "dpi (" + densityBucket + ")\n" //
        + "Release: " + Build.VERSION.RELEASE + '\n' //
        + "API: " + Build.VERSION.SDK_INT + '\n'
        + "-------------------\n\n"; //
  }

  public static String getDensityString(DisplayMetrics displayMetrics) {
    switch (displayMetrics.densityDpi) {
      case DisplayMetrics.DENSITY_LOW:
        return "ldpi";
      case DisplayMetrics.DENSITY_MEDIUM:
        return "mdpi";
      case DisplayMetrics.DENSITY_HIGH:
        return "hdpi";
      case DisplayMetrics.DENSITY_XHIGH:
        return "xhdpi";
      case DisplayMetrics.DENSITY_XXHIGH:
        return "xxhdpi";
      case DisplayMetrics.DENSITY_XXXHIGH:
        return "xxxhdpi";
      case DisplayMetrics.DENSITY_TV:
        return "tvdpi";
      default:
        return "unknown";
    }
  }
}

@@ -919,16 +919,21 @@
 ;%0A%0A  /**
+%0A   *
  @deprec
@@ -997,17 +997,98 @@
 ing...)%7D
-.
+ or %7B@link%0A   * #EmailDeviceInfoLens(Context, String, String, int, String...)%7D.%0A  
  */%0A  @D
@@ -1177,16 +1177,55 @@
  subject
+, String version,%0A      int versionCode
 ) %7B%0A    
@@ -1238,32 +1238,54 @@
 ontext, subject,
+ version, versionCode,
  addresses);%0A  %7D
@@ -1899,16 +1899,305 @@
 );%0A  %7D%0A%0A
+  public EmailDeviceInfoLens(Context context, String subject, String version, int versionCode,%0A      String... addresses) %7B%0A    super(context, subject, addresses);%0A    this.context = context;%0A    this.version = version;%0A    this.versionCode = versionCode;%0A    this.body = makeBody();%0A  %7D%0A%0A
   @Overr

Bring back constructor that takes version and version code