package com.usai.apex;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TabHost;

import com.usai.apex.saved.SavedDetailFragment;
import com.usai.apex.saved.SavedSearchFragment;
import com.usai.util.dbUtil;

public class SearchHistoryActivity extends FragmentActivity
{
	TabHost	mTabHost;
	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.checksaved, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		// Intent intent = new Intent();
		switch (item.getItemId())
		{
			case R.id.action_deleteall:
			{
				AlertDialog.Builder builder = new Builder(this);
				builder.setMessage(getString(R.string.str_deletemessage));

				builder.setTitle(getString(R.string.str_confirmdelete));

				builder.setPositiveButton(getString(R.string.str_del),
						new Dialog.OnClickListener()
						{

							@Override
							public void onClick(DialogInterface dialog,
									int which)
							{
								SQLiteDatabase db = dbUtil.OpenDB(
										SearchHistoryActivity.this, null, false);
								int a=mTabHost.getCurrentTab();
								if(mTabHost.getCurrentTab()==0)
								{
									
									db.execSQL("delete from history");
									SavedSearchFragment f = (SavedSearchFragment)getSupportFragmentManager().findFragmentById(R.id.tab1);
//									f.searchresult.getData().clear();
//									f.adapter.notifyDataSetChanged();
								}
								else
								{
									db.execSQL("delete from favorites");
									SavedDetailFragment f = (SavedDetailFragment)getSupportFragmentManager().findFragmentById(R.id.tab2);
//									f.searchresult.getData().clear();
//									f.adapter.notifyDataSetChanged();
								}	


								dbUtil.CloseDB(db);
								dialog.dismiss();

							}
						});

				builder.setNegativeButton(getString(R.string.str_cancel),
						new Dialog.OnClickListener()
						{

							@Override
							public void onClick(DialogInterface dialog,
									int which)
							{
								dialog.dismiss();
							}
						});

				builder.create().show();

				break;
			}
			default:
				break;
		}
		return super.onOptionsItemSelected(item);
	}
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		Log.d("SearchHistoryActivity", "onCreate");

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_search_history);
		// ActionBar bar = getActionBar();
		// bar.setTitle("");
		setupTabs();

	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
		if (keyCode == KeyEvent.KEYCODE_BACK)
		{
			// Intent myIntent = new Intent();
			// myIntent = new Intent(EditActivity.this, tabActivity.class);
			// startActivity(myIntent);
			finish();
			
			return false;
		}
		return super.onKeyDown(keyCode, event);
	}
	private void setupTabs()
	{
		mTabHost = (TabHost) this.findViewById(R.id.tabhost);

		mTabHost.setup();

		mTabHost.addTab(mTabHost
				.newTabSpec(getString(R.string.action_saved_search))
				.setIndicator(getString(R.string.action_saved_search))
				.setContent(R.id.tab1));

		mTabHost.addTab(mTabHost.newTabSpec(getString(R.string.action_saved_detail))
				.setIndicator(getString(R.string.action_saved_detail))
				.setContent(R.id.tab2));


		Fragment favorites = new SavedDetailFragment();
		Fragment history = new SavedSearchFragment();

		// Add the fragment to the activity, pushing this transaction
		// on to the back stack.

		FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
		ft.replace(R.id.tab1, history);
		ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
		ft.addToBackStack("search");

		ft.replace(R.id.tab2, favorites);
		ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
		ft.addToBackStack("detail");

		ft.commit();

	}
}
