package com.beloko.idtech;

import java.io.File;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Environment;

import com.beloko.idtech.GD.IDGame;

public class AppSettings {

	public static IDGame game;
	
	public static String belokoBaseDir;
	
	public static String musicBaseDir;
	
	public static boolean gamePadEnabled;
	public static String gamePadControlsFile;
	public static String graphicsDir;
	
	public static boolean vibrate;
	
	public static void setGame(IDGame g)
	{
		game = g;
	}
	
	public static void reloadSettings(Context ctx)
	{
		String base = getStringOption(ctx, "base_path", null);
		if (base == null)
		{
			base  =  Environment.getExternalStorageDirectory().toString() + "/Beloko";
			setStringOption(ctx, "base_path", base);
		}
		
		belokoBaseDir =  base;
		
		
		String music = getStringOption(ctx, "music_path", null);
		if (music == null)
		{
			music  =  belokoBaseDir + "/" + game + "/Music";
			setStringOption(ctx, "music_path", music);
		}
		
		musicBaseDir =  music;
		
		graphicsDir = ctx.getFilesDir().toString() + "/";
		
		gamePadEnabled = getBoolOption(ctx, "gamepad_enabled", false);
		AppSettings.gamePadControlsFile = ctx.getFilesDir().toString() + "/gamePadControls.dat";
		
		vibrate =  getBoolOption(ctx, "vibrate", true);
		
		CDAudioPlayer.initFiles(musicBaseDir);
		
	}

	public static String getQuakeDemoDir()
	{
		String quakeFilesDir = AppSettings.belokoBaseDir + "/" + game;
		return  quakeFilesDir + "/DEMO";
	}
	
	public static String getQuakeFullDir()
	{
		String quakeFilesDir = AppSettings.belokoBaseDir + "/" + game;
		return quakeFilesDir + "/FULL";
	}
	
	public static void createDirectories()
	{
		new File(getQuakeDemoDir()).mkdirs();
		
		String d = "";
		if (game == IDGame.Quake)
			d = "/id1/";
		else if (game == IDGame.Quake2)
			d = "/baseq2/";
		
		new File(getQuakeFullDir() + d).mkdirs();
		
	}
	
	public static boolean showAbout(Context ctx)
	{
		try {
			int versionCode = ctx.getPackageManager().getPackageInfo(ctx.getPackageName(), 0).versionCode;

			SharedPreferences settings = ctx.getSharedPreferences("OPTIONS", 0);
			int last_ver = settings.getInt("last_opened_version", -1);
			//Log.d("test"," ver = " +  versionCode + " las=" + last_ver);
			if (versionCode != last_ver)
			{
				SharedPreferences.Editor editor = settings.edit();
				editor.putInt("last_opened_version", versionCode);
				editor.commit();
				return true;
			}
			else
				return false;

		} catch (NameNotFoundException e) {
			return false;
		}
	}

	public static float getFloatOption(Context ctx,String name, float def)
	{
		SharedPreferences settings = ctx.getSharedPreferences("OPTIONS", 	Context.MODE_MULTI_PROCESS);
		return settings.getFloat(name, def);
	}

	public static void setFloatOption(Context ctx,String name, float value)
	{
		SharedPreferences settings = ctx.getSharedPreferences("OPTIONS", 	Context.MODE_MULTI_PROCESS);
		SharedPreferences.Editor editor = settings.edit();
		editor.putFloat(name, value);
		editor.commit();
	}

	public static boolean getBoolOption(Context ctx,String name, boolean def)
	{
		SharedPreferences settings = ctx.getSharedPreferences("OPTIONS", 	Context.MODE_MULTI_PROCESS);
		return settings.getBoolean(name, def);
	}

	public static void setBoolOption(Context ctx,String name, boolean value)
	{
		SharedPreferences settings = ctx.getSharedPreferences("OPTIONS", 	Context.MODE_MULTI_PROCESS);
		SharedPreferences.Editor editor = settings.edit();
		editor.putBoolean(name, value);
		editor.commit();
	}

	public static int getIntOption(Context ctx,String name, int def)
	{
		SharedPreferences settings = ctx.getSharedPreferences("OPTIONS", 	Context.MODE_MULTI_PROCESS);
		return settings.getInt(name, def);
	}

	public static void setIntOption(Context ctx,String name, int value)
	{
		SharedPreferences settings = ctx.getSharedPreferences("OPTIONS", 	Context.MODE_MULTI_PROCESS);
		SharedPreferences.Editor editor = settings.edit();
		editor.putInt(name, value);
		editor.commit();
	}

	public static String getStringOption(Context ctx,String name, String def)
	{
		SharedPreferences settings = ctx.getSharedPreferences("OPTIONS", 	Context.MODE_MULTI_PROCESS);
		return settings.getString(name, def);
	}

	public static void setStringOption(Context ctx,String name, String value)
	{
		SharedPreferences settings = ctx.getSharedPreferences("OPTIONS", 	Context.MODE_MULTI_PROCESS);
		SharedPreferences.Editor editor = settings.edit();
		editor.putString(name, value);
		editor.commit();
	}
}
