package com.wasu.cs.ui;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.ViewGroup;

import com.wasu.comp.videoview.IMediaListener;
import com.wasu.cs.widget.mediacontrol.LiveMediaController;
import com.wasu.cs.widget.videoview.WasuLivePlayerView;
import com.wasu.module.log.WLog;
import com.wasu.util.NetUtils;

import cn.com.wasu.main.AppUtil;
import cn.com.wasu.main.IntentConstant;

public class ActivityLivePlayer extends ActivityBase implements IMediaListener {
	private static final String TAG = "ActivityLivePlayer";
    public static final String ACTION = "com.wasu.action.live.play";

	public static final String ASSET_PLAY_INFO = IntentConstant.PLAY_INFO.value();
	public static final String ASSET_CHANNEL_ID = "channelId";

	///////////////////////////////////////////////////////////////////////////////////////////



	///////////////////////////////////////////////////////////////////////////////////////////

	/**
	 * 播放器
	 */
	private WasuLivePlayerView player;

	////////////////////////////////////////////////////////////////////////////////////////////

	private String jsonInfo;
    private String channelId;

	////////////////////////////////////////////////////////////////////////////////////////////

	@Override
	protected void doCreate(Bundle savedInstanceState) {
		WLog.i(TAG,"doCreate()");

		player = new WasuLivePlayerView(this);
		player.addObserver(this);

		addContentView(player, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

		configurePlayer();
	}
	
	@Override
	protected void onPostCreate(Bundle savedInstanceState) {

		if (parseIntent()) {
			play();
		}

		super.onPostCreate(savedInstanceState);
	}
	
	@Override
	protected void onNewIntent(Intent intent) {
		
		setIntent(intent);
		
		player.stopPlayback();
		
		if (parseIntent()) {
			play();
		}
		
		super.onNewIntent(intent);
	}
	
	@Override
	public void finish() {
		
		if (player != null) {
			player = null;
		}
		
		super.finish();
	}

	@Override
	public void onStateChanged(int state) {
		if (AppUtil.ETHERNET_DISCONNECTED == state || AppUtil.WIFI_DISCONNECTED == state) {
			showErrorExitDlg("网络出错,请检查您的网络设置");
		} else {
			hideErrorExitDlg();
		}
	}

	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		if (player != null) {
            return player.dispatchKeyEvent(event);
        }
		return super.dispatchKeyEvent(event);
	}
	
	/**
	 * 配置播放器
	 */
	private void configurePlayer() {
        player.setDisplayOption(LiveMediaController.DISPLAY_CHANNEL_PANEL_CONTROLLER |
                LiveMediaController.DISPLAY_CHANNEL_HEADER |
                LiveMediaController.DISPLAY_PROMPT_BUFFER |
                LiveMediaController.DISPLAY_VOLUME_CONTROLLER |
                LiveMediaController.DISPLAY_VOLUME_MUTE_CONTROLLER |
                LiveMediaController.DISPLAY_PROMPT_EXIT_AD);
	}

	/**
	 * 解析传入数据
	 * 
	 * @return
	 */
	public boolean parseIntent() {

        jsonInfo = getIntent().getStringExtra(ASSET_PLAY_INFO);
        channelId = getIntent().getStringExtra(ASSET_CHANNEL_ID);

		if (TextUtils.isEmpty(jsonInfo)) {
			showErrorExitDlg("传入数据为空");
			return false;
		}
		
		return true;
	}

	/**
	 * 开始播放
	 */
	public void play() {
        player.play(jsonInfo, channelId);
	}

	@Override
	public void onAdStatusChanged(int arg0, int arg1) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onCompletion(MediaPlayer arg0) {
        // TODO Auto-generated method stub
	}

	@Override
	public void onError(MediaPlayer arg0, int arg1, int arg2) {
		showErrorExitDlg("播放器错误:" + arg1 + "," + arg2);
	}

	@Override
	public void onInfo(MediaPlayer arg0, int arg1, int arg2) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onPause(MediaPlayer arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onPrepareComplete(MediaPlayer arg0) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onPreparing(MediaPlayer arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onProgress(int arg0, int arg1, int arg2) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onResume(MediaPlayer arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onSeekComplete(MediaPlayer arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onSeeking(MediaPlayer arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onStart(MediaPlayer arg0) {
        player.getMediaController().handleFullScreen(true);
	}

	@Override
	public void onStatusChanged(MediaPlayer arg0, int arg1) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onStop(MediaPlayer arg0) {
	}

	/**
	 * @param arg0
	 * 		WASU_ERROR_AUTH			 	= 设备授权注册失败
	 *		WASU_ERROR_QUERY_PRICE 		= 资产询价失败
	 *		WASU_ERROR_PAY				= 资产支付失败
	 *		WASU_ERROR_PLAYER			= 播放器异常
	 *		WASU_ERROR_UPM_TOKEN_EXPIRE	= token严重超期
	 * @param arg1
	 */
	@Override
	public void onWasuError(int arg0, String arg1) {
		if (NetUtils.isNetConnected(this)) {
            String msg = arg1;
			switch (arg0) {
                case WASU_ERROR_AUTH:
                    msg = "设备授权注册失败";
                    break;
                case WASU_ERROR_QUERY_PRICE:
                    msg = "资产询价失败";
                    break;
                case WASU_ERROR_PAY:
                    msg = "资产支付失败";
                    break;
                case WASU_ERROR_PLAYER:
                    msg = "播放器异常:" + arg1;
                    break;
                case WASU_ERROR_UPM_TOKEN_EXPIRE:
                    msg = "token严重超期";
                    break;
            }
            showErrorExitDlg(msg);
		}else{
			showErrorExitDlg("网络出错,请检查您的网络设置");
		}
	}

	@Override
	public void onWasuPlayLimit(int arg0, String arg1) {
		// TODO Auto-generated method stub
		
	}

}
