package com.raphxyz.natif;

import android.R;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

 
public class RaphxyzNatif extends CordovaPlugin {
 
	public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
		final CallbackContext cbContext = callbackContext;
		
		PluginResult result = new PluginResult(PluginResult.Status.NO_RESULT);
		result.setKeepCallback(true);
		cbContext.sendPluginResult(result);
		
		if(this.cordova.getActivity().isFinishing()) return true;
		
		switch(action) {
			case "alert":
				alert(args.getString(0), args.getString(1), cbContext);
				return true;
				break;
			
			case "confirm":
				confirm(args.getString(0), args.getString(1), cbContext);
				return true;
				break;
			
			case "promt":
				promt(args.getString(0), args.getString(1), cbContext);
				return true;
				break;
			
			default:
				cbContext.sendPluginResult(new PluginResult(PluginResult.Status.INVALID_ACTION));
				return false;
				break;
		}
	}
 
	private synchronized void alert(final String title, final String content, final CallbackContext cbContext){
		AlertDialog.Builder alertDialog = new AlertDialog.Builder(this.cordova.getActivity(), AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
		
		alertDialog.setTitle(title)
		.setMessage(content)
		.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener(){
			public void onClick(DialogInterface dialog, int id){
				dialog.dismiss();
				cbContext.sendPluginResult(new PluginResult(PluginResult.Status.OK));
			}
		})
		.show();
	}
	
	private synchronized void confirm(final String title, final String message, final CallbackContext cbContext) {
		AlertDialog.Builder alertDialog = new AlertDialog.Builder(this.cordova.getActivity(), AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
		
		alertDialog.setTitle(title)
		.setMessage(message)
		.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener(){
			public void onClick(DialogInterface dialog, int id){
				dialog.dismiss();
				cbContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, true));
			}
		})
		.setNegativeButton(R.string.no, new DialogInterface.OnClickListener(){
			public void onClick(DialogInterface dialog, int id){
				dialog.dismiss();
				cbContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, false));
			}
		})
		.show();
    }
	
	private synchronized void promt(final String title, final String message, final CallbackContext cbContext) {
		final EditText promptInput =  new EditText(cordova.getActivity());
		promptInput.setHint("");
		AlertDialog.Builder alertDialog = new AlertDialog.Builder(this.cordova.getActivity(), AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
		
		final JSONObject result = new JSONObject();
		
		alertDialog.setTitle(title)
		.setMessage(message)
		.setCancelable(true)
		.setView(promptInput)
		.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener(){
			public void onClick(DialogInterface dialog, int id){
				dialog.dismiss();
				try {
					result.put("buttonIndex",1);
					result.put("input1", promptInput.getText().toString().trim().length()==0 ? defaultText : promptInput.getText());											
				} catch (JSONException e) { e.printStackTrace(); }
				cbContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, result));
			}
		})
		.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener(){
			public void onClick(DialogInterface dialog, int id){
				dialog.dismiss();
				try {
					result.put("buttonIndex",0);
					result.put("input1", promptInput.getText().toString().trim().length()==0 ? defaultText : promptInput.getText());
				} catch (JSONException e) { e.printStackTrace(); }
				cbContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, result));
			}
		})
		.show();
    }
}
@@ -884,22 +884,33 @@
 %09%0A%09%09
-switch
+if(%22alert%22.equals
 (action)
  %7B%0A%09
@@ -909,29 +909,11 @@
 ion)
- 
+)
 %7B%0A
-%09%09%09case %22alert%22:%0A%09
 %09%09%09a
@@ -962,33 +962,32 @@
  cbContext);%0A%09%09%09
-%09
 return true;%0A%09%09%09
@@ -989,41 +989,48 @@
 ;%0A%09%09
-%09%09break;%0A%09%09%09%0A%09%09%09case %22confirm%22:%0A%09
+%7D%0A%09%09else if(%22confirm%22.equals(action)) %7B%0A
 %09%09%09c
@@ -1081,33 +1081,32 @@
  cbContext);%0A%09%09%09
-%09
 return true;%0A%09%09%09
@@ -1108,39 +1108,46 @@
 ;%0A%09%09
-%09%09break;%0A%09%09%09%0A%09%09%09case %22promt%22:%0A%09
+%7D%0A%09%09else if(%22promt%22.equals(action)) %7B%0A
 %09%09%09p
@@ -1200,25 +1200,24 @@
 ontext);%0A%09%09%09
-%09
 return true;
@@ -1223,34 +1223,19 @@
 ;%0A%09%09
-%09%09break;%0A%09%09%09%0A%09%09%09default:%0A%09
+%7D%0A%09%09else %7B%0A
 %09%09%09c
@@ -1318,17 +1318,16 @@
 N));%0A%09%09%09
-%09
 return f
@@ -1336,19 +1336,8 @@
 se;%0A
-%09%09%09%09break;%0A
 %09%09%7D%0A

Change case to ifelse