package net.indivia.hacklabbo.statusreader;

import java.io.File;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Build;
import android.os.Environment;
import android.util.Log;

public class SUtility {
	public static final String LOG_TAG = "SUtility";
	
	@SuppressLint("NewApi")
	public static void saveState(Context ctx, Boolean status, String last_refresh, String status_change){
		Resources res = ctx.getResources();
	    //storing
	    SharedPreferences states = ctx.getApplicationContext().getSharedPreferences(res.getString(R.string.key_states), 0);
	    SharedPreferences.Editor editor = states.edit();
	    
	    editor.putBoolean(res.getString(R.string.key_status), status);
	    editor.putString(res.getString(R.string.key_last_refresh), last_refresh);
	    editor.putString(res.getString(R.string.key_status_changed), status_change);
	    
	    if(Build.VERSION.SDK_INT > Build.VERSION_CODES.FROYO)
	    	editor.apply();
	    else
	    	commitShared(editor);
	}
	
	@TargetApi(Build.VERSION_CODES.FROYO)
	private static void commitShared(SharedPreferences.Editor editor){
		editor.commit();
	}

	public static File getFile(String url, String reldir) {
		final String ADIR = "/StatusReader/" ;
		File file, apath;

		String[] fileUrlParts = url.split("/");
		apath = new File(Environment.getExternalStorageDirectory()+ ADIR + reldir);
		if (! apath.exists()){
			apath.mkdirs();
		}

		file = new File(apath.toString()+ "/" + fileUrlParts[fileUrlParts.length-1]);
		Log.d(LOG_TAG,"external storage state:"+Environment.getExternalStorageState());
    
    	return file;
	}
}