package org.apache.cordova.estat;

import fr.mediametrie.mesure.library.android.*;
import com.squareup.tape.*;
import org.json.JSONArray;
import org.json.JSONException;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CallbackContext;
import android.R;
import android.content.Context;
import android.content.DialogInterface;
import android.app.AlertDialog;
 
public class estat extends CordovaPlugin {
 
  public Boolean trackerStarted = false;
  
  public String serial = "";
  
  public EstatAudienceTagger audienceTagger = null;
  
  public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    if("initEstat".equals(action)){
      String id = args.getString(0);
	  if(!trackerStarted) { this.startTracker(id, callbackContext); }
      callbackContext.success();
      return true;
    }else if("sendHitEstat".equals(action)){
		if(args.length() == 1)
		{
			this.sendHit(args.getString(0), callbackContext);
			callbackContext.success();
		} else if (args.length() == 2)
		{
			this.sendHit(args.getString(0), args.getString(1), callbackContext);
			callbackContext.success();
		} else if (args.length() == 3)
		{
			this.sendHit(args.getString(0), args.getString(1), args.getString(2), callbackContext);
			callbackContext.success();
		} else if (args.length() == 4)
		{
			this.sendHit(args.getString(0), args.getString(1), args.getString(2), args.getString(3), callbackContext);
			callbackContext.success();
		}
      return true;
    } else {
      callbackContext.error("AlertPlugin."+action+" not found !");
      return false;
    }
  }
 
  private void showAlert(String content){
    // see http://developer.android.com/guide/topics/ui/dialogs.html
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(this.cordova.getActivity(), AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
    alertDialog.setTitle("Alert");
    alertDialog.setMessage(content);
    alertDialog.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener(){
      public void onClick(DialogInterface dialog, int id){
        // User clicked OK button
      }
    });
    alertDialog.show();
  }
  
   private void startTracker(String id, CallbackContext callbackContext) {
        if (null != id && id.length() > 0) {
            //GoogleAnalytics.getInstance(this.cordova.getActivity()).getTracker(id);
			Estat.init(this.cordova.getActivity());
			Estat.setLog(BuildConfig.DEBUG);
			serial = id;
			audienceTagger = Estat.getAudience(serial, "");
			trackerStarted = true;
            callbackContext.success("estat started");
			AlertDialog.Builder alertDialog = new AlertDialog.Builder(this.cordova.getActivity(), AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
			alertDialog.setTitle("eStat started !");
			alertDialog.setMessage(id + " V." + Estat.getVersion());
			alertDialog.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener(){
			  public void onClick(DialogInterface dialog, int id){
				// User clicked OK button
			  }
			});
			alertDialog.show();
            
            //GAServiceManager.getInstance().setLocalDispatchPeriod(30); //deprecated but whatcha gonna do? set dispatch period to 30 sec
        } else {
            callbackContext.error("serial is not valid");
        }
    }
	
	private void sendHit(String level1, CallbackContext callbackContext) {
        if (trackerStarted) {
            audienceTagger.sendHit(level1);
			callbackContext.success("estat hit sent");
        } else {
            callbackContext.error("eStat not initialized");
        }
    }
	
	private void sendHit(String level1, String level2, CallbackContext callbackContext) {
        if (trackerStarted) {
            audienceTagger.sendHit(level1, level2);
			callbackContext.success("estat hit sent");
        } else {
            callbackContext.error("eStat not initialized");
        }
    }
	
	private void sendHit(String level1, String level2, String level3, CallbackContext callbackContext) {
        if (trackerStarted) {
            audienceTagger.sendHit(level1, level2, level3);
			callbackContext.success("estat hit sent");
        } else {
            callbackContext.error("eStat not initialized");
        }
    }
	
	private void sendHit(String level1, String level2, String level3, String level4, CallbackContext callbackContext) {
        if (trackerStarted) {
            audienceTagger.sendHit(level1, level2, level3, level4);
			callbackContext.success("estat hit sent");
        } else {
            callbackContext.error("eStat not initialized");
        }
    }
}

@@ -1669,564 +1669,12 @@
  %7D%0D%0A
- %0D%0A  private void showAlert(String content)%7B%0D%0A    // see http://developer.android.com/guide/topics/ui/dialogs.html%0D%0A    AlertDialog.Builder alertDialog = new AlertDialog.Builder(this.cordova.getActivity(), AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);%0D%0A    alertDialog.setTitle(%22Alert%22);%0D%0A    alertDialog.setMessage(content);%0D%0A    alertDialog.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener()%7B%0D%0A      public void onClick(DialogInterface dialog, int id)%7B%0D%0A        // User clicked OK button%0D%0A      %7D%0D%0A    %7D);%0D%0A    alertDialog.show();%0D%0A  %7D%0D%0A
   %0D%0A
+
    p
@@ -1795,95 +1795,8 @@
  %7B%0D%0A
-            //GoogleAnalytics.getInstance(this.cordova.getActivity()).getTracker(id);%0D%0A
 %09%09%09E
@@ -1964,24 +1964,24 @@
 ed = true;%0D%0A
+
             
@@ -2027,614 +2027,8 @@
 );%0D%0A
-%09%09%09AlertDialog.Builder alertDialog = new AlertDialog.Builder(this.cordova.getActivity(), AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);%0D%0A%09%09%09alertDialog.setTitle(%22eStat started !%22);%0D%0A%09%09%09alertDialog.setMessage(id + %22 V.%22 + Estat.getVersion());%0D%0A%09%09%09alertDialog.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener()%7B%0D%0A%09%09%09  public void onClick(DialogInterface dialog, int id)%7B%0D%0A%09%09%09%09// User clicked OK button%0D%0A%09%09%09  %7D%0D%0A%09%09%09%7D);%0D%0A%09%09%09alertDialog.show();%0D%0A            %0D%0A            //GAServiceManager.getInstance().setLocalDispatchPeriod(30); //deprecated but whatcha gonna do? set dispatch period to 30 sec%0D%0A
     

Update estat.java