package com.usai.apex;

import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.R.color;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.Intent.ShortcutIconResource;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class ToolsFragment extends ListFragment
{

	static final int PASSWORD_CHANGED = 1;
	private static final int	REQUEST_CHANGEPASSWORD_ACTIVITY	= 2;
	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)
//					getActivity().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);
//					getActivity().sendBroadcast(intent);
//					Intent i = new Intent();
//					i.setClass(getActivity(), ApexActivity.class);
//					startActivityForResult(i, REQUEST_LOGINACTIVITY);
					getActivity().setResult(PASSWORD_CHANGED, null);
					getActivity().finish();
				}
				break;
			default:
				break;
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	// @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 onListItemClick(ListView l, View v, int position, long id)
	{
		Log.d("FragmentList", "Item clicked: " + id);

		String title = (String) getData().get(position).get("title");
		Intent intent = new Intent();
		if (title.equals("Market news"))
		{
			intent.setClass(getActivity(), AnnouncementActivity.class);
			intent.putExtra("module_name", "Market News");
		}
		else if (title.equals("Announcements"))
		{
			intent.setClass(getActivity(), AnnouncementActivity.class);
			intent.putExtra("module_name", "Announcements");
		}
		else if (title.equals("About this App"))
		{
			intent.setClass(getActivity(), AboutActivity.class);
		}
		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.setClass(getActivity(), ApexHistoryActivity.class);

//			startActivityForResult(intent, REQUEST_CHANGEPASSWORD_ACTIVITY);
//			return;
		}
		else if (title.equals("Setting"))
		{
			intent.setClass(getActivity(), SettingsActivity.class);

//			startActivityForResult(intent, REQUEST_CHANGEPASSWORD_ACTIVITY);
//			return;
		}
		startActivity(intent);

	}
}
