package com.sdk.example;

import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.wole56.sdk.Uploader;
import com.wole56.sdk.Uploader.UploadCallback;
import com.wole56.sdk.Video;

/**
 * 上传本地视频到服务器，支持断点
 * 
 * @author susie
 * 
 */
public class UploadVideo extends Activity {
	private static final int LIB = 1; // load video from lib
	protected static final String TAG = "UploadVideo";

	private String videoPath;
	private TextView videoPathTxtV;
	private EditText tagEt;
	private EditText titleEt;
	private EditText descEt;
	// category用户自定义视频类别，只能为数字
	private EditText categoryEt;
	private Button uploadBtn;
	private TextView resultTxtV;
	private Button getVideoBtn;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.video_upload);

		initUI();
		regEvent();

	}

	private void regEvent() {
		uploadBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if (videoPath == null || videoPath.equals("")) {
					Toast.makeText(getApplicationContext(),
							"please get video path", Toast.LENGTH_SHORT).show();
				} else {
					upload();
				}
			}
		});

		getVideoBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
				intent.setType("video/*");
				startActivityForResult(intent, LIB);
			}

		});
	}

	protected void upload() {
		// upload video
		JSONObject videoInfo = new JSONObject();
		String tag = tagEt.getText().toString().trim();
		String title = titleEt.getText().toString().trim();
		String desc = descEt.getText().toString().trim();
		String category = categoryEt.getText().toString().trim();
		try {
			videoInfo.put("tag", tag);// 视频标签
			videoInfo.put("title", title);// 视频标题
			videoInfo.put("desc", desc);// 视频描述
			videoInfo.put("category", category);// 视频描述
			String time = System.currentTimeMillis() + "";
			videoInfo.put("time", time);
			String sid = "qq123456";// 第三方用户id
			videoInfo.put("sid", sid);
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		Log.i(TAG, "video info " + videoInfo);
		Uploader.uploadVideo(getApplicationContext(), videoPath, videoInfo,
				new UploadCallback() {

					@Override
					public void onFail(final String info) {
						// TODO Auto-generated method stub
						UploadVideo.this.runOnUiThread(new Runnable() {

							@Override
							public void run() {
								// TODO Auto-generated method stub
								resultTxtV.setText(info);
							}

						});
					}

					@Override
					public void onProgress(final int progress) {
						UploadVideo.this.runOnUiThread(new Runnable() {

							@Override
							public void run() {
								// TODO Auto-generated method stub
								resultTxtV.setText("progress: " + progress);
							}

						});
					}

					@Override
					public void onSuccess(final String info) {
						Log.i(TAG, "success " + info);
						UploadVideo.this.runOnUiThread(new Runnable() {

							@Override
							public void run() {
								// TODO Auto-generated method stub
								resultTxtV.setText(info);
							}

						});
					}
				});

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (data == null) {
			return;
		}
		if (resultCode == Activity.RESULT_OK) {
			Uri videoUri = data.getData();

			switch (requestCode) {
			case LIB: {
				videoPath = Video.getFilePath(UploadVideo.this, videoUri);
				videoPathTxtV.setText(videoPath);
				break;
			}
			default:
				break;
			}
		}
	}

	private void initUI() {
		videoPathTxtV = (TextView) findViewById(R.id.video_path_txt);
		tagEt = (EditText) findViewById(R.id.tag_et);
		titleEt = (EditText) findViewById(R.id.title_et);
		descEt = (EditText) findViewById(R.id.desc_et);
		categoryEt = (EditText) findViewById(R.id.category_et);
		uploadBtn = (Button) findViewById(R.id.upload_btn);
		resultTxtV = (TextView) findViewById(R.id.result_txt);
		getVideoBtn = (Button) findViewById(R.id.select_video_btn);
	}
}
