package com.bladecoder.engine.util;

import com.badlogic.gdx.Gdx;

public class DPIUtils {
	public final static float BASE_DPI = 160.0f;
	
	/**
	 * Current DPI
	 */
	public final static float DPI = BASE_DPI * Gdx.graphics.getDensity();

	/**
	 * The Google recommendations are 48 dp -> 9mm for touchable elements
	 */
	public final static float TOUCH_MIN_SIZE = 48 * Gdx.graphics.getDensity();

	/**
	 * The Google recommendations of space between UI objects is 8 dp
	 */
	public final static float UI_SPACE = 8 * Gdx.graphics.getDensity();

	/**
	 * The Google recommendations of space from bottom or top is 16 dp
	 */
	public final static float MARGIN_SIZE = 16 * Gdx.graphics.getDensity();

	/**
	 * The Google recommendations are 56 dp for action buttons
	 */
	public final static float BUTTON_SIZE = 56 * Gdx.graphics.getDensity();
	
	/**
	 * The Google recommendations are 24 dp for icons inside action buttons
	 */
	public final static float ICON_SIZE = 24 * Gdx.graphics.getDensity();

	
	/**
	 * The Google recommendations are 8 dp for space between ui elements
	 */
	public final static float SPACING = 8 * Gdx.graphics.getDensity();

	/**
	 * The screen height in DP
	 */
	public final static float SCREEN_HEIGHT_DP = Gdx.graphics.getHeight()
			/ Gdx.graphics.getDensity();

	public final static float NORMAL_MULTIPLIER = 1.0f; // 3-5"
	public final static float LARGE_MULTIPLIER = 1.5f; // 5-7"
	public final static float XLARGE_MULTIPLIER = 2f; // 8-10"
	public final static float XXLARGE_MULTIPLIER = 2.5f; // > 10"

	/**
	 * Calcs the button size based in screen size
	 *
	 * @return The recommended size in pixels
	 */
	public static float getPrefButtonSize() {
		return getSizeMultiplier() * BUTTON_SIZE;
	}
	
	/**
	 * Calcs the minimum size based in screen size
	 *
	 * @return The recommended size in pixels
	 */
	public static float getTouchMinSize() {
		return getSizeMultiplier() * TOUCH_MIN_SIZE;
	}
	
	/**
	 * Calcs the margin size based in screen size
	 *
	 * @return The recommended size in pixels
	 */
	public static float getMarginSize() {
		return getSizeMultiplier() * MARGIN_SIZE;
	}
	
	/**
	 * Calcs the space between ui elements based in screen size
	 *
	 * @return The recommended size in pixels
	 */
	public static float getSpacing() {
		return getSizeMultiplier() * SPACING;
	}

//	public static float getSizeMultiplier() {
//		float inches = pixelsToInches(Gdx.graphics.getWidth());
//
//		if (inches > 15)
//			return XXLARGE_MULTIPLIER;
//
//		if (inches > 9)
//			return XLARGE_MULTIPLIER;
//
//		if (inches > 6)
//			return LARGE_MULTIPLIER;
//
//		return NORMAL_MULTIPLIER;
//
//	}
	
	public static float getSizeMultiplier() {
		float inches = pixelsToInches(Gdx.graphics.getWidth());
		float s = inches / 6f;

		return Math.max(1.0f, s);

	}

	public static int dpToPixels(int dp) {
		return (int) (dp * Gdx.graphics.getDensity());
	}
	
	public static int pixelsToDP(int pixels) {
		return (int) (pixels / Gdx.graphics.getDensity());
	}
	
	public static float pixelsToInches(int pixels) {
		return (float)pixels / DPI;
	}
	
	public static float ptToPixels(float pts) {
		return pts * 72 / DPI;
	}
}

@@ -130,17 +130,16 @@
 160.0f;%0A
-%09
 %0A%09/**%0A%09 
@@ -201,32 +201,26 @@
 E_DPI * 
-Gdx.graphics.get
+getLogical
 Density(
@@ -353,32 +353,26 @@
  = 48 * 
-Gdx.graphics.get
+getLogical
 Density(
@@ -494,32 +494,26 @@
 E = 8 * 
-Gdx.graphics.get
+getLogical
 Density(
@@ -640,32 +640,26 @@
  = 16 * 
-Gdx.graphics.get
+getLogical
 Density(
@@ -778,32 +778,26 @@
  = 56 * 
-Gdx.graphics.get
+getLogical
 Density(
@@ -799,17 +799,16 @@
 sity();%0A
-%09
 %0A%09/**%0A%09 
@@ -927,32 +927,26 @@
  = 24 * 
-Gdx.graphics.get
+getLogical
 Density(
@@ -949,18 +949,16 @@
 ity();%0A%0A
-%09%0A
 %09/**%0A%09 *
@@ -1070,32 +1070,26 @@
 G = 8 * 
-Gdx.graphics.get
+getLogical
 Density(
@@ -1204,30 +1204,21 @@
 ht()
-%0A%09%09%09/ Gdx.graphics.get
+ / getLogical
 Dens
@@ -1470,16 +1470,141 @@
  %3E 10%22%0A%0A
+%09public static final float getLogicalDensity() %7B%0A%09%09return Gdx.graphics.getDensity() / Gdx.graphics.getBackBufferScale();%0A%09%7D%0A%0A
 %09/**%0A%09 *
@@ -1781,33 +1781,32 @@
 BUTTON_SIZE;%0A%09%7D%0A
-%09
 %0A%09/**%0A%09 * Calcs 
@@ -1978,33 +1978,32 @@
 CH_MIN_SIZE;%0A%09%7D%0A
-%09
 %0A%09/**%0A%09 * Calcs 
@@ -2177,17 +2177,16 @@
 IZE;%0A%09%7D%0A
-%09
 %0A%09/**%0A%09 
@@ -2685,17 +2685,16 @@
 //%0A//%09%7D%0A
-%09
 %0A%09public
@@ -2907,32 +2907,26 @@
 ) (dp * 
-Gdx.graphics.get
+getLogical
 Density(
@@ -2924,33 +2924,32 @@
 lDensity());%0A%09%7D%0A
-%09
 %0A%09public static 
@@ -3002,32 +3002,26 @@
 ixels / 
-Gdx.graphics.get
+getLogical
 Density(
@@ -3023,25 +3023,24 @@
 sity());%0A%09%7D%0A
-%09
 %0A%09public sta
@@ -3091,15 +3091,8 @@
 urn 
-(float)
 pixe
@@ -3104,17 +3104,16 @@
 DPI;%0A%09%7D%0A
-%09
 %0A%09public

Update DPI retrieval acording latest Gdx changes.