package de.markusfisch.android.shadereditor.preference;

import android.content.Context;
import android.content.SharedPreferences;
import android.hardware.SensorManager;

import java.lang.NumberFormatException;

public class Preferences
{
	public static final String NAME =
		"de.markusfisch.android.preference.Preferences";

	public static final String WALLPAPER_SHADER = "shader";
	public static final String SAVE_BATTERY = "save_battery";
	public static final String RUN_MODE = "run_mode";
	public static final String UPDATE_DELAY = "update_delay";
	public static final String SENSOR_DELAY = "sensor_delay";
	public static final String TEXT_SIZE = "text_size";

	private static final int RUN_AUTO = 1;
	private static final int RUN_MANUALLY = 2;
	private static final int RUN_MANUALLY_EXTRA = 3;

	private SharedPreferences preferences;
	private long wallpaperShaderId = 1;
	private boolean saveBattery = true;
	private int runMode = RUN_AUTO;
	private int updateDelay = 1000;
	private int sensorDelay = SensorManager.SENSOR_DELAY_NORMAL;
	private int textSize = 12;

	public Preferences( Context context )
	{
		preferences = context.getSharedPreferences(
			NAME,
			Context.MODE_PRIVATE );

		update();
	}

	public SharedPreferences getSharedPreferences()
	{
		return preferences;
	}

	public void update()
	{
		wallpaperShaderId = parseLong(
			preferences.getString( WALLPAPER_SHADER, null ),
			wallpaperShaderId );
		saveBattery = preferences.getBoolean(
			SAVE_BATTERY,
			saveBattery );
		runMode = parseInt(
			preferences.getString( RUN_MODE, null ),
			runMode );
		updateDelay = parseInt(
			preferences.getString( UPDATE_DELAY, null ),
			updateDelay );
		sensorDelay = parseSensorDelay(
			preferences.getString( SENSOR_DELAY, null ),
			sensorDelay );
		textSize = parseInt(
			preferences.getString( TEXT_SIZE, null ),
			textSize );
	}

	public boolean saveBattery()
	{
		return saveBattery;
	}

	public boolean doesRunOnChange()
	{
		return runMode == RUN_AUTO;
	}

	public boolean doesRunInBackground()
	{
		return runMode != RUN_MANUALLY_EXTRA;
	}

	public int getUpdateDelay()
	{
		return updateDelay;
	}

	public int getSensorDelay()
	{
		return sensorDelay;
	}

	public int getTextSize()
	{
		return textSize;
	}

	public long getWallpaperShader()
	{
		return wallpaperShaderId;
	}

	public void setWallpaperShader( long id )
	{
		wallpaperShaderId = id;

		SharedPreferences.Editor editor = preferences.edit();
		editor.putString(
			WALLPAPER_SHADER,
			String.valueOf( wallpaperShaderId ) );
		editor.apply();
	}

	public static int parseInt( String s, int preset )
	{
		try
		{
			if( s != null &&
				s.length() > 0 )
				return Integer.parseInt( s );
		}
		catch( NumberFormatException e )
		{
		}

		return preset;
	}

	public static long parseLong( String s, long preset )
	{
		try
		{
			if( s != null &&
				s.length() > 0 )
				return Long.parseLong( s );
		}
		catch( NumberFormatException e )
		{
		}

		return preset;
	}

	private static int parseSensorDelay( String s, int preset )
	{
		if( s == null )
			return preset;

		if( s.equals( "Fastest" ) )
			return SensorManager.SENSOR_DELAY_FASTEST;
		else if( s.equals( "Game" ) )
			return SensorManager.SENSOR_DELAY_GAME;
		else if( s.equals( "Normal" ) )
			return SensorManager.SENSOR_DELAY_NORMAL;
		else if( s.equals( "UI" ) )
			return SensorManager.SENSOR_DELAY_UI;

		return preset;
	}
}

@@ -162,16 +162,61 @@
 Manager;
+%0Aimport android.preference.PreferenceManager;
 %0A%0Aimport
@@ -279,97 +279,10 @@
 ces%0A
+
 %7B%0A
-%09public static final String NAME =%0A%09%09%22de.markusfisch.android.preference.Preferences%22;%0A%0A
 %09pub
@@ -1084,70 +1084,66 @@
 s = 
-context.getSharedPreferences(%0A%09%09%09NAME,%0A%09%09%09Context.MODE_PRIVATE
+PreferenceManager%0A%09%09%09.getDefaultSharedPreferences( context
  );%0A
@@ -2666,32 +2666,49 @@
 ception e )%0A%09%09%7B%0A
+%09%09%09// use preset%0A
 %09%09%7D%0A%0A%09%09return pr
@@ -2901,16 +2901,33 @@
 e )%0A%09%09%7B%0A
+%09%09%09// use preset%0A
 %09%09%7D%0A%0A%09%09r

Use default shared preferences