package io.securiteam.plugins;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;

import android.content.Context;
import android.app.Activity;
import android.graphics.Point;
import android.os.Build;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.util.DisplayMetrics;

public class DeviceScreen extends CordovaPlugin
{
	public static final String ACTION_GET_WIDTH = "getWidth";
	public static final String ACTION_GET_HEIGHT = "getHeight";
	public static final String ACTION_GET_SCALE = "getScale";
	
	private CallbackContext context;
	private Activity activity;
	private Window window;
	private View decorView;
	
	@Override
	public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException
	{
		context = callbackContext;
		activity = cordova.getActivity();
		window = activity.getWindow();
		decorView = window.getDecorView();
		
		if (ACTION_GET_WIDTH.equals(action))
			return getWidth();
		else if (ACTION_GET_HEIGHT.equals(action))
			return getHeight();
		else if (ACTION_GET_SCALE.equals(action))
			return getScale();
		
		return false;
	}
	
	/**
	 * The width of the screen in immersive mode
	 */
	protected boolean getWidth()
	{
		activity.runOnUiThread(new Runnable()
		{
			@Override
			public void run() 
			{
				try
				{
					Point outSize = new Point();
					
					decorView.getDisplay().getRealSize(outSize);
					
			        PluginResult res = new PluginResult(PluginResult.Status.OK, outSize.x);
			        context.sendPluginResult(res);
				}
				catch (Exception e)
				{
					context.error(e.getMessage());
				}
			}
		});
		
		return true;
	}
	
	/**
	 * The height of the screen in immersive mode
	 */	
	protected boolean getHeight()
	{
		activity.runOnUiThread(new Runnable()
		{
			@Override
			public void run() 
			{
				try
				{
					Point outSize = new Point();
					
					decorView.getDisplay().getRealSize(outSize);
					
			        PluginResult res = new PluginResult(PluginResult.Status.OK, outSize.y);
			        context.sendPluginResult(res);
				}
				catch (Exception e)
				{
					context.error(e.getMessage());
				}
			}
		});
        
		return true;
	}
	
	/**
	 * The scale of the screen
	 */	
	protected boolean getScale()
	{
		activity.runOnUiThread(new Runnable()
		{
			@Override
			public void run() 
			{
				try
				{
					float density = activity.getApplicationContext().getResources().getDisplayMetrics().density;
			        PluginResult res = new PluginResult(PluginResult.Status.OK, density);
			        context.sendPluginResult(res);
				}
				catch (Exception e)
				{
					context.error(e.getMessage());
				}
			}
		});
        
		return true;
	}
	
}
@@ -1307,34 +1307,16 @@
 e screen
- in immersive mode
 %0A%09 */%0A%09p
@@ -1436,32 +1436,95 @@
 %7B%0A%09%09%09%09try%0A%09%09%09%09%7B%0A
+%09%09%09%09%09int width;%0A%09%09%09%09%09%0A%09%09%09%09%09if (Build.VERSION.SDK_INT %3E= 17) %7B%0A%09
 %09%09%09%09%09Point outSi
@@ -1538,37 +1538,32 @@
 w Point();%0A%09%09%09%09%09
-%0A%09%09%09%09
 %09decorView.getDi
@@ -1584,32 +1584,173 @@
 lSize(outSize);%0A
+%09%09%09%09%09%09width = outSize.x;%0A%09%09%09%09%09%7D else %7B%0A%09%09%09%09%09%09width = activity.getApplicationContext().getResources().getDisplayMetrics().widthPixels;%0A%09%09%09%09%09%7D%0A
 %09%09%09%09%09%0A%09%09%09       
@@ -1810,25 +1810,21 @@
 tus.OK, 
-outSize.x
+width
 );%0A%09%09%09  
@@ -2009,26 +2009,8 @@
 reen
- in immersive mode
 %0A%09 *
@@ -2140,24 +2140,88 @@
 %09%09try%0A%09%09%09%09%7B%0A
+%09%09%09%09%09int height;%0A%09%09%09%09%09%0A%09%09%09%09%09if (Build.VERSION.SDK_INT %3E= 17) %7B%0A%09
 %09%09%09%09%09Point o
@@ -2247,21 +2247,16 @@
 );%0A%09%09%09%09%09
-%0A%09%09%09%09
 %09decorVi
@@ -2289,24 +2289,168 @@
 e(outSize);%0A
+%09%09%09%09%09%09height = outSize.y;%0A%09%09%09%09%09%7D else %7B%0A%09%09%09%09%09%09height = activity.getApplicationContext().getResources().getDisplayMetrics().heightPixels;%0A%09%09%09%09%09%7D%0A
 %09%09%09%09%09%0A%09%09%09   
@@ -2514,25 +2514,22 @@
 tus.OK, 
-outSize.y
+height
 );%0A%09%09%09  

support older api