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

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

/**
 * 测试 Video.getVideoInfo(Context c)接口,将返回数据绑定到相应控件上
 * 
 */
public class GetVideoInfo extends Activity {
	protected static final String TAG = "GetVideoInfo";
	private Button videoInfoBtn;
	private EditText videoInfoEt;
	private TextView resultTxtV;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.video_info);

		initUI();
		regEvent();
	}

	private void regEvent() {
		/*
		 * get video info
		 */
		videoInfoBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				final String videoId = videoInfoEt.getText().toString().trim();// "72613970";

				if ("".equals(videoId)) {
					// input null
					String msg = "请输入video id";
					Toast.makeText(getApplicationContext(), msg,
							Toast.LENGTH_SHORT).show();
				} else {
					// get data
					JSONObject result = Video.getVideoInfo(GetVideoInfo.this,
							videoId);
					Log.i(TAG, "get video info " + result);

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

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

					resultTxtV.setText(spanStr);
				}
			}
		});
	}

	private void initUI() {
		resultTxtV = (TextView) findViewById(R.id.result_txt);
		videoInfoBtn = (Button) findViewById(R.id.video_info_btn);
		videoInfoEt = (EditText) findViewById(R.id.video_id_et);
	}
}
