package com.usai.apex.mainframe;

import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.Intent.ShortcutIconResource;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.View;
import android.widget.ListView;

import com.usai.apex.AboutActivity;
import com.usai.apex.AnnouncementActivity;
import com.usai.apex.ApexHistoryActivity;
import com.usai.apex.ApexTrackingApplication;
import com.usai.apex.InnerMapActivity;
import com.usai.apex.R;

import org.json.JSONObject;

public class ToolsFragment extends StaticModelistFragment
{


	private static final int	REQUEST_LOGINACTIVITY			= 1;

	private void delShortcut()
	{
		Intent shortcut = new Intent(
				"com.android.launcher.action.UNINSTALL_SHORTCUT");

		// 快捷方式的名称
		shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,
				getString(R.string.app_name));

		// 指定当前的Activity为快捷方式启动的对象: 如 com.everest.video.VideoPlayer
		// 注意: ComponentName的第二个参数必须是完整的类名（包名+类名），否则无法删除快捷方式
		String appClass = getActivity().getPackageName() + "."
				+ getActivity().getLocalClassName();
		ComponentName comp = new ComponentName(getActivity().getPackageName(),
				appClass);
		shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(
				Intent.ACTION_MAIN).setComponent(comp));

		getActivity().sendBroadcast(shortcut);

	}

	private void addShortcut()
	{
		Intent shortcut = new Intent(
				"com.android.launcher.action.INSTALL_SHORTCUT");

		// 快捷方式的名称
		shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,
				getString(R.string.app_name));
		shortcut.putExtra("duplicate", false); // 不允许重复创建

		// 指定当前的Activity为快捷方式启动的对象: 如 com.everest.video.VideoPlayer
		// 注意: ComponentName的第二个参数必须加上点号(.)，否则快捷方式无法启动相应程序
		ComponentName comp = new ComponentName(getActivity().getPackageName(),
				"." + getActivity().getLocalClassName());
		shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(
				Intent.ACTION_MAIN).setComponent(comp));
		// 快捷方式的图标
		ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(
				getActivity(), R.drawable.ic_launcher);
		shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);

		getActivity().sendBroadcast(shortcut);
	}



	// @Override
	// public void onActivityResult(int requestCode, int resultCode, Intent
	// data)
	// {
	// switch (requestCode)
	// {
	// case REQUEST_LOGINACTIVITY:
	// if (resultCode == Activity.RESULT_CANCELED)
	// finish();
	// break;
	// case REQUEST_CHANGEPASSWORD_ACTIVITY:
	// if (resultCode == Activity.RESULT_OK)
	// {
	// 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, 1);
	// }
	// break;
	// default:
	// break;
	// }
	//
	// super.onActivityResult(requestCode, resultCode, data);
	// }
//	private List<Map<String, Object>> getData()
//	{
//		boolean login = false;
//		Bundle b = getArguments();
//
//		if(b!=null)
//		{
//		 login = b.getBoolean("Login");
//		}
//		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
//
//		Map<String, Object> map = new HashMap<String, Object>();
//		map.put("title", "Market news");
//		map.put("info", "google 1");
//		map.put("img", R.drawable.rect_market_news);
//		list.add(map);
//
//		map = new HashMap<String, Object>();
//		map.put("title", "Announcements");
//		map.put("info", "google 2");
//		map.put("img", R.drawable.rect_announcements);
//		list.add(map);
//
//		if (login)
//		{
//			map = new HashMap<String, Object>();
//			map.put("title", "Saved Detail/Search");
//			map.put("info", "google 2");
//			map.put("img", R.drawable.rect_search_history);
//			list.add(map);
//
//			map = new HashMap<String, Object>();
//			map.put("title", "Change Password");
//			map.put("img", R.drawable.rect_change_password);
//			list.add(map);
//			map = new HashMap<String, Object>();
//			map.put("title", "Setting");
//			map.put("img", R.drawable.rect_setting);
//			list.add(map);
//		}
//
//		map = new HashMap<String, Object>();
//		map.put("title", "Apex History");
//
//		map.put("img", R.drawable.rect_history);
//		list.add(map);
//
//		map = new HashMap<String, Object>();
//		map.put("title", "About this App");
//		map.put("info", "google 2");
//		map.put("img", R.drawable.rect_about);
//		list.add(map);
//
//		return list;
//	}

//	@Override
//	public void onActivityCreated(Bundle savedInstanceState)
//	{
//		super.onActivityCreated(savedInstanceState);
//		SimpleAdapter adapter = new SimpleAdapter(this.getActivity(),
//				getData(), R.layout.fragment_tools, new String[] { "title",
//						"img" }, new int[] { R.id.tv_toolname, R.id.iv_icon });
//		setListAdapter(adapter);
//		this.getListView().setBackgroundColor(Color.WHITE);
//	}


	@Override
	public void onActivityCreated(Bundle savedInstanceState)
	{
		super.onActivityCreated(savedInstanceState);

        setupAdapter(R.raw.tools);

//        adapter.getView()

		customize_style();
	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id)
	{

		JSONObject jsonobj=loadjson(R.raw.tools);
		Log.d("FragmentList", "Item clicked: " + id);

		String 	title = (String) getData(jsonobj).get(position-1).get("title");
		String type = (String) getData(jsonobj).get(position-1).get("type");
//		try {
////			title = jsonobj.getJSONArray("modelist").getJSONObject(position);
////
//
//
//
//			} catch (JSONException e) {
//			e.printStackTrace();
//		}

		if (title.equals("Market News"))
		{
			Intent intent = new Intent();
			intent.setClass(getActivity(), AnnouncementActivity.class);
			intent.putExtra("module_name", "Market News");
			startActivity(intent);
		}
		else if (title.equals("Announcements"))
		{
			Intent intent = new Intent();
			intent.setClass(getActivity(), AnnouncementActivity.class);
			intent.putExtra("module_name", "Announcements");
			startActivity(intent);
		}

//		else if (title.equals("Saved Detail/Search"))
//		{
//			intent.setClass(getActivity(), SearchHistoryActivity.class);
//		}
//		else if (title.equals("Change Password"))
//		{
//			intent.setClass(getActivity(), ChangePasswordActivity.class);
//
//			startActivityForResult(intent, REQUEST_CHANGEPASSWORD_ACTIVITY);
//			return;
//		}
		else if (title.equals("Apex History"))
		{
			Intent intent = new Intent();
			intent.setClass(getActivity(), ApexHistoryActivity.class);
			startActivity(intent);

//			startActivityForResult(intent, REQUEST_CHANGEPASSWORD_ACTIVITY);
//			return;
		}
		else if (title.equals("Service Location"))
		{
//			intent.setClass(getActivity(), ApexHistoryActivity.class);
//
//
//			Intent intent = new Intent();
			Intent intent = new Intent();
			intent.setClass(getActivity(), InnerMapActivity.class);
			startActivity(intent);

//			startActivityForResult(intent, REQUEST_CHANGEPASSWORD_ACTIVITY);
//			return;
		}
		else if (type.equals("clear_login"))
		{

			String msg = "Are you sure to clear saved login account and password?";
			if (ApexTrackingApplication.m_bauthorized) {
				msg = "Are you sure to logout current account and clear login information?";
			}

			new AlertDialog.Builder(getContext())
					.setTitle("Warning")
					.setMessage(msg)
					.setNegativeButton("No",null)
					.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialogInterface, int i) {

							SharedPreferences pref = ApexTrackingApplication.get_instance().getSharedPreferences("Apex", 0);
							SharedPreferences.Editor editor = pref.edit();
							editor.putString("user", null);
							editor.putString("password", null);
							editor.putBoolean("autologin", false);
							editor.commit();

							String UNIQUE_STRING = "com.usai.apex.push.cancel";
							Intent intent = new Intent(UNIQUE_STRING);
							getActivity().sendBroadcast(intent);

							Intent bintent = new Intent("Login");
							bintent.putExtra("state", false);
							LocalBroadcastManager.getInstance(getActivity()).sendBroadcast(bintent);
						}
					})
					.show();


		}
		else if (title.equals("About"))
		{
			Intent intent = new Intent();
			intent.setClass(getActivity(), AboutActivity.class);
			startActivity(intent);
		}
//		else if (title.equals("Setting"))
//		{
//			intent.setClass(getActivity(), SettingsActivity.class);
//
////			startActivityForResult(intent, REQUEST_CHANGEPASSWORD_ACTIVITY);
////			return;
//		}


	}
}
