package com.usai.apex;

//import com.google.android.gms.maps.SupportMapFragment;

import android.app.ActionBar;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
//import android.app.Fragment;
//import android.app.FragmentTransaction;
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.widget.TabHost;

public class ApexActivity extends FragmentActivity /*
													 * implements
													 * OnTabChangeListener
													 */
{
	boolean	m_bhasgoogleframework	= false;

	@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);
	}

	TabHost	mTabHost;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		Log.d("ApexActivity", "onCreate");

		// ApexTrackingApplication.get_instance()
		// .getPackageManager().
		PackageInfo packageInfo;
		try
		{
			packageInfo = this.getPackageManager().getPackageInfo(
					"com.google.android.gms", 0);

		}
		catch (NameNotFoundException e)
		{
			packageInfo = null;
			e.printStackTrace();
		}
		if (packageInfo == null)
		{
			m_bhasgoogleframework = false;
			System.out.println("没有安装");
		}
		else
		{
			m_bhasgoogleframework = true;
			System.out.println("已经安装");
		}

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

	}

	// @Override
	// public boolean onCreateOptionsMenu(Menu menu) {
	// // Inflate the menu; this adds items to the action bar if it is present.
	// getMenuInflater().inflate(R.menu.apex, menu);
	// return true;
	// }
	//
	// @Override
	// public boolean onOptionsItemSelected(MenuItem item) {
	// switch (item.getItemId()) {
	// case R.id.forget_password:
	// Intent intent = new Intent();
	// intent.setClass(this, RetrievePasswordActivity.class);
	// startActivity(intent);
	//
	// break;
	// default:
	// break;
	//
	// }
	// return super.onOptionsItemSelected(item);
	// }

	private void setupTabs()
	{
		mTabHost = (TabHost) this.findViewById(R.id.tabhost);

		mTabHost.setup();
		// // ��ɵײ��Զ�����ʽ�İ�ť
		// String[] title = new String[] { "Login", "Not login" };
		// int[] tabIds = new int[] { R.id.tab1, R.id.tab2 };
		mTabHost.addTab(mTabHost.newTabSpec(getString(R.string.action_sign_in_short)).setIndicator(getString(R.string.action_sign_in_short))
				.setContent(R.id.tab1));
		// mTabHost.addTab(mTabHost.newTabSpec("test").setIndicator("test").setContent(R.id.tabtest));

		mTabHost.addTab(mTabHost.newTabSpec(getString(R.string.str_tool)).setIndicator(getString(R.string.str_tool))
				.setContent(R.id.tab2));
		mTabHost.addTab(mTabHost.newTabSpec(getString(R.string.str_location))
				.setIndicator(getString(R.string.str_location)).setContent(R.id.tab3));

		// Instantiate a new fragment.
		Fragment loginFragment = new LoginFragment();
		Fragment toolsFragment = new ToolsFragment();
		Fragment slFragment;
//		 m_bhasgoogleframework=false;
		if (m_bhasgoogleframework)
			slFragment = new ServiceLocationFragment();
		else
			slFragment = new BaiduMapFragment();
		// Add the fragment to the activity, pushing this transaction
		// on to the back stack.

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

		ft.replace(R.id.tab2, toolsFragment);
		ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
		ft.addToBackStack("Tools");		
		
		
		// ft.replace(R.id.tab2, dtFragement);
		// ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
		// ft.addToBackStack("Tracking");
		// Fragment test = new ServiceLocationFragment();
		// ft.replace(R.id.tabtest, (Fragment) test);
		// ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
		// ft.addToBackStack("test");

		ft.replace(R.id.tab3, (Fragment) slFragment);
		ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
		ft.addToBackStack("Location");

		ft.commit();

		// if (manager.findFragmentByTag(tabId) == null)
		// {
		// FragmentTransaction trans = manager.beginTransaction();
		// trans.replace(contentViewID, frag, tabId);
		// trans.commit();
		// }
		// for (int i = 0; i < title.length; i++) {
		// Button button = new Button(this);
		// button.setText(title[i]);
		// button.setBackgroundDrawable(this.getResources().getDrawable(
		// R.drawable.tab_lable)); //�Զ��尴ť��ʽ
		// mTabHost.addTab(mTabHost.newTabSpec(title[i]).setIndicator(button)
		// .setContent(tabIds[i]));
		// }

		// mTabHost.setOnTabChangedListener(this);
	}

	// @Override
	// public void onTabChanged(String tabId)
	// {
	// TODO Auto-generated method stub
	// Fragment frag = null;
	// int contentViewID = 0;
	// if (tabId.equals("Login")) {
	// frag = new LoginFragment();
	// //�Զ���̳�Fragment��UI������һ���򵥵���ʾ�ı�����Ŀؼ���
	// contentViewID = R.id.tab1;
	// } else if (tabId.equals("Not login")) {
	// frag = new DirectTrackingFragment();
	// contentViewID = R.id.tab2;
	// }
	// if (frag == null)
	// return;
	//
	// FragmentManager manager = this.getFragmentManager();
	//
	// if (manager.findFragmentByTag(tabId) == null) {
	// FragmentTransaction trans = manager.beginTransaction();
	// trans.replace(contentViewID, frag, tabId);
	// trans.commit();
	// }
	// }
	// public class LoginFragment extends Fragment implements OnClickListener{
	// @Override
	// public View onCreateView(LayoutInflater inflater, ViewGroup container,
	// Bundle savedInstanceState) {
	// View view = inflater.inflate(R.layout.activity_login, null);
	// Button btn = (Button)view.findViewById(R.id.sign_in_button);
	// btn.setOnClickListener(this);
	// return view;
	// }
	//
	// @Override
	// public void onClick(View v)
	// {
	// // TODO Auto-generated method stub
	//
	// }
	// }
}
