package hh.dejsem;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import hh.lib.DA;

public class Act extends DA implements Handler.Callback {

	@Override
	public void onCreate(Bundle b) {
		super.onCreate(b);
		new Prefs().refreshPrefs(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {     // volá se jen jednou
		d.l(4, "onCreateOptionsMenu");
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if(d.ll(4)) d.l(String.format("options menu item selected=%s", item.getTitle()));
		if(onItemSelected(item)) return true;
		else return super.onOptionsItemSelected(item);
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
		d.l(2, "onCreateContextMenu");
		getMenuInflater().inflate(R.menu.main, menu);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		if(d.ll(4)) d.l(String.format("context menu item selected=%s", item.getTitle()));
		if(onItemSelected(item)) return true;
		else return super.onContextItemSelected(item);
	}

	@Override
	public boolean handleMessage(Message msg) {
		if(d.ll(4)) d.l("handleMessage: what=" + msg.what);
		return false;
	}

	public boolean onItemSelected(MenuItem item) {
		switch(item.getItemId()) {
			case R.id.menu_settings:
				getSupportFragmentManager().beginTransaction().replace(android.R.id.content, new Prefs()).addToBackStack("Prefs").commit();
				return true;
			case R.id.menu_uninstall:
				Uri packageURI = Uri.parse("package:" + getComponentName().getPackageName().toString());
				Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI);
				startActivity(uninstallIntent);
				return true;
			case R.id.menu_activity_hack:
				hack();
				return true;
			case R.id.menu_del_passwd:
				PassW.clearPW();
				return true;
			case R.id.menu_del_sd_card_perms:
				SdUri.clearPermissions();
				return true;
			case R.id.menu_clean_caches:
				cleanCache();
				return true;
			case R.id.menu_reset_prefs:
				new Prefs().resetPrefs(this);
				return true;
		}
		return false;
	}

	public void cleanCache() {
		Prefs.sp.edit().remove(Prefs.HOST_CACHE_KEY).apply();
		Prefs.sp.edit().remove(Prefs.HOME_CACHE_KEY).apply();
	}

	@Override
	public void hack() {
		if(d.ll(4)) d.l("hack");
	}

}

