package fsenablers.android.utilities;

import fsenablers.android.BuildConfig;
import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;

public class DeviceHelper {
	
	
	public static final String PHONE_FORMAT = "phone";
	
	public static final String TABLET_FORMAT = "tablet";

	
	public static String getPlatform( Context context ) {
		return "android";
	}

	public static String getFormat( Context context) {
		if( isTablet( context ) && isTabletDevice( context )) {
			return TABLET_FORMAT;
		} else {
			return PHONE_FORMAT;
		}
	}
	
	
	public static String getName( Context context ) {
	  
	  StringBuffer buffer = new StringBuffer();
	  buffer.append( Build.MANUFACTURER );
	  buffer.append( "," );
	  buffer.append( Build.MODEL );
	  

	  return buffer.toString();
	  
	}
	
	public static boolean isTablet(Context context) {
		
	    TelephonyManager manager = 
	        (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
	    if (manager.getPhoneType() == TelephonyManager.PHONE_TYPE_NONE) {
	        //Tablet
	        return true;
	    } else {
	        //Mobile
	        return false; 
	    }
	}

	public static boolean isTabletDevice(Context activityContext) {
	    // Verifies if the Generalized Size of the device is XLARGE to be
	    // considered a Tablet
	    boolean xlarge = 
	         ((activityContext.getResources().getConfiguration().screenLayout & 
	           Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE);

	    // If XLarge, checks if the Generalized Density is at least MDPI (160dpi)
	    if (xlarge) {
	        DisplayMetrics metrics = new DisplayMetrics();
	        Activity activity = (Activity) activityContext;
	        activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);

	        // MDPI=160, DEFAULT=160, DENSITY_HIGH=240, DENSITY_MEDIUM=160,
	        // DENSITY_TV=213, DENSITY_XHIGH=320
	        if (metrics.densityDpi == DisplayMetrics.DENSITY_DEFAULT
	                  || metrics.densityDpi == DisplayMetrics.DENSITY_HIGH
	                  || metrics.densityDpi == DisplayMetrics.DENSITY_MEDIUM   
	                  || metrics.densityDpi == DisplayMetrics.DENSITY_XHIGH) {

	             // Yes, this is a tablet!
	             return true;
	        }
	    }

	    // No, this is not a tablet!
	    return false;
	}
	
	public static String getUserAgentSuffix( Context context ) {
		
		StringBuffer agent = new StringBuffer();
		
		agent.append( ";FSE:native=true" )
		     .append( ";FSE:platform=" + getPlatform( context ))
		     .append( ";FSE:format=" + getFormat( context ))
		     .append( ";FSE:device=" + getName( context ))
		     .append( ";FSE:caps=phone,camera,speech" )
		     .append( ";FSE:variant=" + getFSEVariant())
		     .append( ";FSE:version=" + getFSEVersion())
		     .append( ";FSE:build=" + getFSEBuild() );
		
		return agent.toString();

	}
	
	public static String getFSEVariant() {
		// for dev
		// return "FSCRM-T";
		// for prod
		return "FSCRM";
	}
	
	public static String getFSEBuild() {
		if( BuildConfig.DEBUG  ) {
			return "6.16.7.2015.D";
		} else {
			return "6.16.7.2015";
		}
	}
	
	public static String getFSEVersion() {
		return "0.1.0";
	}
	
}
