package com.sdk.example;

import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.text.Html;
import android.text.SpannableString;
import android.text.Spanned;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

import com.sdk.util.Parser;
import com.wole56.sdk.APP;

/**
 * 获取应用下的所有视频，支持分页
 * 
 */
public class GetAppVideoNew extends Activity {

	private String TAG = "NewGetAppVideo";
	private EditText keywordEdt;
	private TextView resultTxtV;
	private String VIEWS_SORT = "views";
	private String TIME_SORT = "time";
	private String sortBy = "";
	private String keyword = "";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.new_app_video);
		// init ui
		resultTxtV = (TextView) findViewById(R.id.appvideo_result_txt);
		RadioGroup sortRadioGroup = (RadioGroup) findViewById(R.id.sort_radio_group);
		keywordEdt = (EditText) findViewById(R.id.keyword_et);

		sortRadioGroup
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(RadioGroup group, int checkedId) {
						// TODO Auto-generated method stub
						if (checkedId == R.id.views_btn) {
							sortBy = VIEWS_SORT; // 根据热度“最热”排序
						} else if (checkedId == R.id.time_btn) {
							sortBy = TIME_SORT; // 根据时间“最新”排序
						}
					}
				});

	}

	public void onClick(View view) {
		String num = "3";// 返回数据 每页的数量
		String page = "1";// 返回数据，第几页
		keyword = keywordEdt.getText().toString();

		// 获取应用下的所有视频
		JSONObject result = APP.getVideo(getApplicationContext(), num, page,
				sortBy, keyword);
		Log.i(TAG, result.toString());

		// 数据简单解析与呈现
		String parseStr = Parser.simpleParser(result);

		// 富文本形式展现
		Spanned span = Html.fromHtml(parseStr);
		SpannableString spanStr = new SpannableString(span);
		// show result
		resultTxtV.setText(spanStr);
	}
}
