package com.usai.apex;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.MKGeneralListener;
import com.baidu.mapapi.map.MKEvent;
import com.baidu.mapapi.map.SupportMapFragment;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Toast;

public class InnerMapActivity extends FragmentActivity
{
//	public static boolean		m_bKeyRight				= true;
//	BMapManager					mBMapManager			= null;

//    public String strKey = getString(R.string.baidu_key);//"nqBQoSDbxrslhuzW91uViQX7";//release
	private static final String	LTAG					= "test";
	SupportMapFragment			map;

	boolean						m_bhasgoogleframework	= true;  

//	static class MyGeneralListener implements MKGeneralListener
//	{
//
//		@Override
//		public void onGetNetworkState(int iError)
//		{
//			if (iError == MKEvent.ERROR_NETWORK_CONNECT)
//			{
//				Toast.makeText(
//						ApexTrackingApplication.get_instance()
//								.getApplicationContext(),
//						"????????????????????????", Toast.LENGTH_LONG).show();
//			}
//			else if (iError == MKEvent.ERROR_NETWORK_DATA)
//			{
//				Toast.makeText(
//						ApexTrackingApplication.get_instance()
//								.getApplicationContext(),
//						"??????????????????????????????", Toast.LENGTH_LONG)
//						.show();
//			}
//			// ...
//		}
//
//		@Override
//		public void onGetPermissionState(int iError)
//		{
//			// ???????????????key???????????????
//			if (iError != 0)
//			{
//				// ??????Key?????????
//				Toast.makeText(
//						ApexTrackingApplication.get_instance()
//								.getApplicationContext(),
//						"?????? MyApplication.java???????????????????????????Key,??????????????????????????????????????????error: "
//								+ iError, Toast.LENGTH_LONG).show();
//				m_bKeyRight = false;
//			}
//			else
//			{
//				m_bKeyRight = true;
//				Toast.makeText(
//						ApexTrackingApplication.get_instance()
//								.getApplicationContext(), "key????????????",
//						Toast.LENGTH_LONG).show();
//			}
//		}
//	}

	@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 true;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		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("已经安装");
		}
		// setUpMapIfNeeded();

//		m_bhasgoogleframework = false;
		
		
		



		Fragment slFragment = null;
		if (m_bhasgoogleframework)
		{
			setContentView(R.layout.activity_inner_map);
			slFragment = new ServiceLocationFragment();

			FragmentTransaction ft = getSupportFragmentManager()
					.beginTransaction();
			ft.replace(R.id.inner_map, slFragment);
			ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
			ft.addToBackStack("Location");
			ft.commit();
		}
		else
		{
//			if (mBMapManager == null)
//			{
//				mBMapManager = new BMapManager(getApplicationContext());
//				/**
//				 * ??????BMapManager???????????????????????????BMapManager
//				 */
//				mBMapManager.init(strKey, new MyGeneralListener());
//			}
			
//			f.initEngineManager(this);
			setContentView(R.layout.activity_inner_map);
//			Log.d(LTAG, "onCreate");
//			// setContentView(R.layout.activity_main);
//			map = SupportMapFragment.newInstance();
			BaiduMapFragment f  = new BaiduMapFragment();
			FragmentManager manager = getSupportFragmentManager();
			manager.beginTransaction().add(R.id.inner_map, f, "map_fragment")
					.commit();
		}
	}

	// @Override
	// public boolean onCreateOptionsMenu(Menu menu)
	// {
	// // Inflate the menu; this adds items to the action bar if it is present.
	// getMenuInflater().inflate(R.menu.inner_map, menu);
	// return true;
	// }
	// private void setUpMapIfNeeded() {
	// // Do a null check to confirm that we have not already instantiated the
	// map.
	// if (mMap == null) {
	// // Try to obtain the map from the SupportMapFragment.
	// mMap = ((SupportMapFragment)
	// getSupportFragmentManager().findFragmentById(R.id.map))
	// .getMap();
	// // Check if we were successful in obtaining the map.
	// if (mMap != null) {
	// setUpMap();
	// }
	// }
	// }
}
