package com.mobile.constellations.ui;

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.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

import com.mobile.constellations.R;

public class EarnFragment extends Fragment implements OnClickListener{

	private TextView pointsTextView;
	private String displayPointsText;
	private final Handler mHandler = new Handler();
	private View layout;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		if (layout == null) {
			layout = inflater.inflate(R.layout.fragment_tab_earn, null);
			resetTitlebar();
			initUI();
			initwanpu();
		}
		ViewGroup parent = (ViewGroup) layout.getParent();
		if (parent != null) {
			parent.removeView(layout);
		}
		return layout;
	}
	
	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 initwanpu() {

	}

	private void initUI() {
		TextView tv_wanpu = (TextView) layout.findViewById(R.id.tv_wanpu);
		pointsTextView = (TextView) layout.findViewById(R.id.tv_score);
		tv_wanpu.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tv_wanpu:
			break;

		default:
			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;
		mHandler.post(mUpdateResults);
	}

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

	@Override
	public void onResume() {
		// 从服务器端获取当前用户的虚拟货币.
		// 返回结果在回调函数getUpdatePoints(...)中处理
		super.onResume();
	}
	
}
