package com.usai.apex;

import com.usai.util.dbUtil;

import android.app.ActionBar;
import android.app.Activity;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

public class FunctionSelectActivity extends FragmentActivity implements
		OnClickListener, OnCheckedChangeListener
{

	static final int PASSWORD_CHANGED = 1;
	private static final int	REQUEST_LOGINACTIVITY			= 1;
	private static final int	REQUEST_CHANGEPASSWORD_ACTIVITY	= 2;
	private static final int	REQUEST_TOOLS_ACTIVITY			= 3;
	ArrayAdapter<String>		adapter							= null;
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		Intent intent = new Intent();
		switch (item.getItemId())
		{
			case R.id.action_settings:
				intent.setClass(this, SettingsActivity.class);
				// intent.putExtra("user", user);
				// intent.putExtra("password", password);
//				intent.putExtra("function_name", function_name);
//				intent.putExtra("behavior", Network.BEHAVIOR_SEARCH);
				startActivity(intent);
				break;

			default:
				break;
		}
		return super.onOptionsItemSelected(item);
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.function_select, menu);
		return true;
	}
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// user = ApexTrackingApplication.get_user();
		// password= getIntent().getStringExtra("password");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.new_function_select);

		ActionBar bar = getActionBar();
		bar.setTitle("");
		//
		TextView tv_booking = (TextView) findViewById(R.id.tv_booking);
		tv_booking.setOnClickListener(this);
		TextView tv_info = (TextView) findViewById(R.id.tv_info);
		tv_info.setOnClickListener(this);
		TextView tv_detail = (TextView) findViewById(R.id.tv_detail);
		tv_detail.setOnClickListener(this);
		// TextView tv_cargo = (TextView)findViewById(R.id.tv_cargo);
		// tv_cargo.setOnClickListener(this);
		TextView tv_doc = (TextView) findViewById(R.id.tv_doc);
		tv_doc.setOnClickListener(this);
		TextView tv_message = (TextView) findViewById(R.id.tv_message);
		tv_message.setOnClickListener(this);
		TextView tv_exit = (TextView) findViewById(R.id.tv_exit);
		tv_exit.setOnClickListener(this);
		TextView tv_loc = (TextView) findViewById(R.id.tv_location);
		tv_loc.setOnClickListener(this);
		TextView tv_tools = (TextView) findViewById(R.id.tv_tools);
		tv_tools.setOnClickListener(this);
		Button btn_search = (Button) findViewById(R.id.btn_search);
		btn_search.setOnClickListener(this);
		Button btn_clear = (Button) findViewById(R.id.btn_clear);
		btn_clear.setOnClickListener(this);
		RadioGroup rg = (RadioGroup) findViewById(R.id.radioGroup1);
		rg.setOnCheckedChangeListener(this);
		// AutoCompleteTextView et = (AutoCompleteTextView)
		// findViewById(R.id.atv_criterion);
		// if (rg.getCheckedRadioButtonId() == R.id.radio0)
		// adapter = new ArrayAdapter<String>(this,
		// android.R.layout.simple_dropdown_item_1line,
		// dbUtil.get_searchhistory("h_bol"));
		// else
		// adapter = new ArrayAdapter<String>(this,
		// android.R.layout.simple_dropdown_item_1line,
		// dbUtil.get_searchhistory("ctnr"));
		// et.setAdapter(adapter);
		SharedPreferences pref = ApexTrackingApplication.get_instance()
				.getSharedPreferences("Apex", 0);
		boolean autologin = pref.getBoolean("autologin", false);

		if (!autologin)
		{
			Intent i = new Intent();
			i.setClass(this, ApexActivity.class);
			startActivityForResult(i, REQUEST_LOGINACTIVITY);
		}

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		switch (requestCode)
		{
			case REQUEST_LOGINACTIVITY:
				if (resultCode == Activity.RESULT_CANCELED)
					finish();
				break;
			case REQUEST_TOOLS_ACTIVITY:
				if (resultCode == PASSWORD_CHANGED)
				{
					String UNIQUE_STRING = "com.usai.apex.push.cancel";
					Intent intent = new Intent(UNIQUE_STRING);
					sendBroadcast(intent);
					Intent i = new Intent();
					i.setClass(this, ApexActivity.class);
					startActivityForResult(i, REQUEST_LOGINACTIVITY);
				}
				break;
			default:
				break;
		}

		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	protected void onDestroy()
	{
		// dbUtil.CloseDB(m_db);
		super.onDestroy();
	}

	@Override
	public void onClick(View v)
	{
		String TAG = "onClick@FunctionSelectActivity";
		Log.d(TAG, "ID=" + v.getId());
		switch (v.getId())
		{
			case R.id.btn_clear:
			{

				AutoCompleteTextView et = (AutoCompleteTextView) findViewById(R.id.atv_criterion);
				et.setText("");
				break;

			}
			case R.id.btn_search:
			{

				AutoCompleteTextView et = (AutoCompleteTextView) findViewById(R.id.atv_criterion);
				// et.clearFocus();

				InputMethodManager inputMethodManager = (InputMethodManager) getApplicationContext()
						.getSystemService(Context.INPUT_METHOD_SERVICE);

				inputMethodManager.hideSoftInputFromWindow(et.getWindowToken(),
						0);
				inputMethodManager.hideStatusIcon(et.getWindowToken());
				String cargo_criterion = et.getText().toString();
				// if (TextUtils.isEmpty(cargo_criterion))
				// {
				// et.setError(getString(R.string.error_field_required));
				// et.requestFocus();
				// }
				// else
				// {
				Intent intent = new Intent();
				intent.setClass(this, DetailActivity.class);

				intent.putExtra("action0", "Tracking");
				intent.putExtra("function_name", "Cargo Tracking");

				intent.putExtra("cargo_criterion", cargo_criterion);
				intent.putExtra("actions_count", 1);
				intent.putExtra("_id", "dumb");
				String h_field;
				RadioGroup rg = (RadioGroup) findViewById(R.id.radioGroup1);
				if (rg.getCheckedRadioButtonId() == R.id.radio0)
				{
					intent.putExtra("criterion_type", 0);
					h_field = "h_bol";
				}
				else
				{
					intent.putExtra("criterion_type", 1);
					h_field = "ctnr";
				}

				dbUtil.savehistory(h_field, cargo_criterion);

				startActivity(intent);

				break;
			}
			case R.id.tv_booking:
			{
				Intent intent = new Intent();
				intent.setClass(this, SearchListActivity.class);
				// intent.putExtra("user", ApexTrackingApplication.get_user());
				// intent.putExtra("password", password);
				intent.putExtra("function_name", "Ocean Booking");
				intent.putExtra("title", "Booking Search");
				startActivity(intent);
				break;
			}
			case R.id.tv_info:
			{
				Intent intent = new Intent();
				intent.setClass(this, SearchListActivity.class);
				// intent.putExtra("user", user);
				// intent.putExtra("password", password);
				intent.putExtra("function_name", "Ocean B/L info.");
				intent.putExtra("title", "B/L info. Search");
				startActivity(intent);
				break;
			}
			case R.id.tv_detail:
			{
				Intent intent = new Intent();
				intent.setClass(this, SearchListActivity.class);
				// intent.putExtra("user", user);
				// intent.putExtra("password", password);
				intent.putExtra("function_name", "Container detail");
				intent.putExtra("title", "Container Search");
				startActivity(intent);
				break;
			}

			case R.id.tv_doc:
			{
				Intent intent = new Intent();
				intent.setClass(this, DocumentsActivity.class);
				// // intent.putExtra("user", user);
				// // intent.putExtra("password", password);
				// intent.putExtra("function_name", "Download Document");
				// intent.putExtra("title", "Document Search");
				startActivity(intent);
				break;
			}
			case R.id.tv_message:
			{

				Intent intent = new Intent();
				intent.setClass(this, MessageActivity.class);
				startActivity(intent);
				//
				// Intent intent = new Intent();
				// intent.setClass(this, ChangePasswordActivity.class);
				// // intent.putExtra("function_name", "Container detail");
				// startActivityForResult(intent,
				// REQUEST_CHANGEPASSWORD_ACTIVITY);
				break;
			}
			case R.id.tv_exit:
			{
				String UNIQUE_STRING = "com.usai.apex.push.cancel";
				Intent intent = new Intent(UNIQUE_STRING);
				// intent.putExtra("key1", "value1");
				// intent.putExtra("key2", "value2");
				sendBroadcast(intent);

				// ApexTrackingApplication.cancelalarm();
				// ApexTrackingApplication.put_password("");
				// ApexTrackingApplication.put_sessionid("");
				// ApexTrackingApplication.put_user("");

				Intent i = new Intent();
				i.setClass(this, ApexActivity.class);
				startActivityForResult(i, 1);

				// finish();
				// System.exit(0);
				break;
			}
			case R.id.tv_tools:
			{
				Intent intent = new Intent();
				intent.setClass(this, InnerToolsActivity.class);
				intent.putExtra("launcher", "inner");
				startActivityForResult(intent,REQUEST_TOOLS_ACTIVITY);
				break;
			}
			case R.id.tv_location:
			{
				Log.d("==============", "start location activity");
				Intent intent = new Intent();
				intent.setClass(this, InnerMapActivity.class);
				// intent.putExtra("user", user);
				// intent.putExtra("password", password);
				// intent.putExtra("function_name", "Container detail");
				startActivity(intent);
				break;
			}

			default:
				break;
		}
		// TODO Auto-generated method stub

	}

	// class HistoryAdapter extends SimpleCursorAdapter{
	//
	// public HistoryAdapter(Context context, int layout, Cursor c,
	// String[] from, int[] to)
	// {
	// super(context, layout, c, from, to,FLAG_AUTO_REQUERY);
	// // TODO Auto-generated constructor stub
	// }
	//
	// }

	@Override
	protected void onResume()
	{
		SQLiteDatabase db = dbUtil.OpenDB(this, null, false);
		int count = dbUtil.get_count(db, "push_message", "read = 0 and user = '"+ApexTrackingApplication.get_user()+"'");

		dbUtil.CloseDB(db);
		NotificationManager nManager = (NotificationManager) this
				.getSystemService(Context.NOTIFICATION_SERVICE);
		nManager.cancel(R.layout.activity_apex);
		ApexTrackingApplication.ncount=1;

		// Intent intent = new Intent();
		// intent.setClass(ApexTrackingApplication
		// .get_instance(), FunctionSelectActivity.class);
		// Intent addShortcut = new Intent(
		// "com.android.launcher.action.INSTALL_SHORTCUT");
		// Parcelable icon = Intent.ShortcutIconResource.fromContext(this,
		// R.drawable.ic_launcher);
		// addShortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,
		// getString(R.string.app_name));
		// addShortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
		// addShortcut.putExtra("duplicate", 0);
		// addShortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
		// Log.e("uuuuuuuuuuuuuuuurrrrrrrrrrrrrriiiiiiiii",
		// intent.toUri(MODE_WORLD_WRITEABLE));
		// sendBroadcast(addShortcut);
		if (count > 0)
		{
			TextView tv_message = (TextView) findViewById(R.id.tv_message);
			tv_message.setCompoundDrawablesWithIntrinsicBounds(0,
					R.drawable.ic_message_new, 0, 0);

		}
		else
		{
			TextView tv_message = (TextView) findViewById(R.id.tv_message);
			tv_message.setCompoundDrawablesWithIntrinsicBounds(0,
					R.drawable.ic_message, 0, 0);
		}

		RadioGroup rg = (RadioGroup) findViewById(R.id.radioGroup1);
		AutoCompleteTextView et = (AutoCompleteTextView) findViewById(R.id.atv_criterion);
		if (rg.getCheckedRadioButtonId() == R.id.radio0)
			adapter = new ArrayAdapter<String>(this,
					android.R.layout.simple_dropdown_item_1line,
					dbUtil.get_searchhistory("h_bol"));
		else
			adapter = new ArrayAdapter<String>(this,
					android.R.layout.simple_dropdown_item_1line,
					dbUtil.get_searchhistory("ctnr"));
		et.setAdapter(adapter);
		super.onResume();
	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId)
	{

		AutoCompleteTextView et = (AutoCompleteTextView) findViewById(R.id.atv_criterion);
		if (checkedId == R.id.radio0)
			adapter = new ArrayAdapter<String>(this,
					android.R.layout.simple_dropdown_item_1line,
					dbUtil.get_searchhistory("h_bol"));
		else
			adapter = new ArrayAdapter<String>(this,
					android.R.layout.simple_dropdown_item_1line,
					dbUtil.get_searchhistory("ctnr"));
		et.setAdapter(adapter);

	}

}
