package com.mobile.constellations.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;


import com.mobile.constellations.R;
import com.umeng.fb.FeedbackAgent;
import com.umeng.fb.fragment.FeedbackFragment;

public class MyFragment extends Fragment implements OnItemClickListener{

	private TextView pointsTextView;
	private MenudAdapter menudAdapter;
	private String[] menudItems;
	private String displayPointsText;
	private final Handler mHandler = new Handler();
	private View layout;
	private int curNumb ;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		if (layout == null) {
			layout = inflater.inflate(R.layout.fragment_tab_my, null);
			resetTitlebar();
			initUI();
			initwanpu();
		}
		ViewGroup parent = (ViewGroup) layout.getParent();
		if (parent != null) {
			parent.removeView(layout);
		}
		return layout;
	}

	private void initwanpu() {

	}

	private void resetTitlebar() {
		View include_tilebar = layout.findViewById(R.id.include_titlebar);
		TextView title = (TextView) include_tilebar.findViewById(R.id.tv_title);
		title.setText("个人");
	}

	private void initUI() {
		menudItems = getResources().getStringArray(R.array.menud_items);
		ListView listMenud = (ListView) layout.findViewById(R.id.listView1);
		View headMenud = getActivity().getLayoutInflater().inflate(R.layout.header_listview_menud, null);
		pointsTextView = (TextView) headMenud.findViewById(R.id.login);
		listMenud.addHeaderView(headMenud);
		menudAdapter = new MenudAdapter();
		listMenud.setAdapter(menudAdapter);
		listMenud.setOnItemClickListener(this);
	}

	private class MenudAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return menudItems.length;
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// 侧滑菜单左边对应的数据显示
			View layout = getActivity().getLayoutInflater().inflate(R.layout.item_listview_menud, null);
			TextView textView = (TextView) layout.findViewById(R.id.textView1);
			textView.setText(menudItems[position]);
			return layout;
		}

	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		switch (position) {
		// 下载赚积分
		case 1:

			
		// 星座漫画
		case 3:
			startActivity(new Intent(getActivity(), SoccerImageActivity.class));
			break;

		// 星座配对
		case 4:
			startActivity(new Intent(getActivity(), PairActivity.class));
			break;

		// 跳转到点此合作
		case 5:
			startActivity(new Intent(getActivity(), CooperateActivity.class));
			break;

		// 跳转到意见反馈（采用第三方umeng的意见反馈功能）
		case 6:
			Intent intent = new Intent();
			intent.setClass(getActivity(), ConversationDetailActivity.class);
			String ids = new FeedbackAgent(getActivity()).getDefaultConversation().getId();
			intent.putExtra(FeedbackFragment.BUNDLE_KEY_CONVERSATION_ID, ids);
			startActivity(intent);
			break;

		}

	}

	// 创建一个线程
	final Runnable mUpdateResults = new Runnable() {
		public void run() {
			if (pointsTextView != null) {
				pointsTextView.setText(displayPointsText);
			}
		}
	};

	/**
	 * AppConnect.getPoints()方法的实现，必须实现
	 * 
	 * @param currencyName
	 *            虚拟货币名称.
	 * @param pointTotal
	 *            虚拟货币余额.
	 */
	public void getUpdatePoints(String currencyName, int pointTotal) {
		displayPointsText = "当前" + currencyName + ": " + pointTotal;
		curNumb = pointTotal;
		mHandler.post(mUpdateResults);
	}

	/**
	 * AppConnect.getPoints() 方法的实现，必须实现
	 * 
	 * @param error
	 *            请求失败的错误信息
	 */
	public void getUpdatePointsFailed(String error) {
		displayPointsText = error;
		mHandler.post(mUpdateResults);
	}

	@Override
	public void onResume() {

		super.onResume();
	}

}
