package com.wasu.cs.widget.mediacontrol;

import android.content.Context;
import android.media.MediaPlayer;
import android.net.TrafficStats;
import android.text.Html;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.wasu.cs.model.DemandProgram;
import com.wasu.module.log.WLog;

import java.util.List;

import cn.com.wasu.main.R;

/**
 * 缓冲提示
 */
public class PromptBuffer extends FrameLayout implements IMediaControllerChildView<IMediaController> {
	private String TAG = "PromptBuffer";

	private IMediaController mediaController;

	private TextView bufferText;
	private TextView bufferRate;
	private TextView bufferNotice;

	private String rateString;
	private String textString;
	private boolean isNeedDestory = false;
	/**
	 * 获取总的接受字节数
	 */
	private long totalData;

	/**
	 * 获取周期，单位ms
	 */
	private static final int CYCLETIME = 1000;
	
	/**
	 * 更新缓冲速度task
	 */
	private Runnable updateRate = new Runnable() {

		@Override
		public void run() {
			bufferRate.setText(String.format(rateString, getNetSpeed() / 1024));
			if(!isNeedDestory){
				removeCallbacks(this);
				postDelayed(this, CYCLETIME);
			}else{
				removeCallbacks(this);
			}
		}

	};
	
	/**
	 * 记录播放器播放位置
	 */
	private int currDuration;
	private boolean isFullScreen;

	public int getCurrDuration() {
		return currDuration;
	}

	public void setCurrDuration(int currDuration) {
		this.currDuration = currDuration;
	}

	public PromptBuffer(Context context) {
		super(context);
		init(context);
	}

	public PromptBuffer(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public PromptBuffer(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}

	private void init(Context context) {

		setId(MediaController.generateID());

		LayoutInflater.from(context).inflate(R.layout.media_controller_buffer, this);

		bufferText = (TextView) findViewById(R.id.buffer_text);
		bufferRate = (TextView) findViewById(R.id.buffer_rate);
		bufferNotice = (TextView) findViewById(R.id.buffer_notice);
		bufferNotice.setText("");

		textString = getContext().getString(R.string.buffer_text);
		rateString = getContext().getString(R.string.buffer_rate);
		
		handleFullScreen(false);
	}

	/**
	 * 设置assetinfo，用于设置缓冲时的按键提示语
	 * */
	protected void setAssetInfo(DemandProgram assetInfo){
		if (bufferNotice != null && assetInfo != null){
			WLog.i(TAG,"assetType = "+assetInfo.getAssetType());
			if (assetInfo.getAssetType() == 0 || assetInfo.getAssetType() == 1 ){ // 0 = 短视频， 1 = 电影， 3 = 电视剧
				bufferNotice.setText(Html.fromHtml(getContext().getString(R.string.buffer_notice_single)));
			}else {
				bufferNotice.setText(Html.fromHtml(getContext().getString(R.string.buffer_notice)));
			}
		}
	}

	private int getNetSpeed() {
		long trafficData = TrafficStats.getTotalRxBytes() - totalData;
		totalData = TrafficStats.getTotalRxBytes();
		return (int) trafficData / (CYCLETIME / 1000);
	}
	
	public void handleFullScreen(boolean isFullScreen) {
		this.isFullScreen = isFullScreen;

		if (isFullScreen) {
			bufferText.setVisibility(VISIBLE);
			bufferRate.setVisibility(VISIBLE);
			bufferNotice.setVisibility(VISIBLE);
		} else {
			bufferText.setVisibility(GONE);
			bufferRate.setVisibility(GONE);
			bufferNotice.setVisibility(GONE);
		}
	}

	public void handleReady(boolean isReady) {
		if (!isFullScreen) {
			return;
		}

		bufferNotice.setVisibility(isReady ? VISIBLE : GONE);
	}

	@Override
	public boolean isAutoHide() {
		return false;
	}
	
	@Override
	public void onShow(IMediaController mediaController) {
		this.mediaController = mediaController;

		if (mediaController instanceof MediaController) {
			MediaController mc = (MediaController) mediaController;
			if (mc.getAssetInfo() != null && !TextUtils.isEmpty(mc.getAssetInfo().getTitle())) {
				bufferText.setText(String.format(textString, mc.getAssetInfo().getTitle(), 0));
			} else {
				bufferText.setText(String.format(textString, "", 0));
			}
		} else if (mediaController instanceof LiveMediaController) {
			bufferNotice.setVisibility(GONE);
			LiveMediaController lmc = (LiveMediaController) mediaController;
			if (lmc.getLiveInfo() != null) {
				//bufferText.setText(String.format(textString, lmc.getLiveInfo().getTitle(), 0));
			} else {
				bufferText.setText(String.format(textString, "", 0));
			}
		}
		
		bufferRate.setText(String.format(rateString, 0));
		
		totalData = TrafficStats.getTotalRxBytes();
		if(!isNeedDestory){
			postDelayed(updateRate, CYCLETIME);
		}else{
			removeCallbacks(updateRate);
		}
	}

	@Override
	public void onHide(IMediaController mediaController) {
		removeCallbacks(updateRate);
	}

	@Override
	public boolean onShield(KeyEvent event) {
		boolean ret = false;
		if (event.getAction() == KeyEvent.ACTION_DOWN) {
			
			switch (event.getKeyCode()) {
			case KeyEvent.KEYCODE_DPAD_UP:
			case KeyEvent.KEYCODE_DPAD_DOWN:
			case KeyEvent.KEYCODE_DPAD_LEFT:
			case KeyEvent.KEYCODE_DPAD_RIGHT:
			case KeyEvent.KEYCODE_VOLUME_UP:
			case KeyEvent.KEYCODE_VOLUME_DOWN:
			case KeyEvent.KEYCODE_VOLUME_MUTE:
			case KeyEvent.KEYCODE_MENU:
			case KeyEvent.KEYCODE_DPAD_CENTER:
			case KeyEvent.KEYCODE_ENTER:
				break;
			case KeyEvent.KEYCODE_BACK:
				ret = true;
				break;
			default:
				ret = true;
				break;
			}
		}
		return ret;
	}

	@Override
	public void clear() {
		removeCallbacks(updateRate);
		totalData = 0L;
		currDuration = 0;
		isFullScreen = false;
	}
	
	public void destory(){
		WLog.i(TAG,"destory");

		clear();
		isNeedDestory = true;
		clear();

		removeCallbacks(updateRate);
	}

	@Override
	protected void onDetachedFromWindow() {
		super.onDetachedFromWindow();

		WLog.i(TAG,"onDetachedFromWindow");
	}
	
	@Override
	public void onAdStatusChanged(int arg0, int arg1) {
		
	}

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

	}

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

	@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) {
		// TODO Auto-generated method stub
		
	}

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

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

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

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

	@Override
	public void setRelativeViews(IMediaControllerChildView<IMediaController>... views) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public List<IMediaControllerChildView<IMediaController>> getRelativeViews() {
		// TODO Auto-generated method stub
		return null;
	}

}