package com.wasu.cs.widget.mediacontrol;

import java.util.List;

import android.content.Context;
import android.media.MediaPlayer;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.widget.FrameLayout;
import android.widget.TextView;
import cn.com.wasu.main.R;

/**
 * 广告倒计时提示
 */
public class PromptADCountdown extends FrameLayout implements IMediaControllerChildView<MediaController> {
	
	private TextView promptCountDown;
	
	private MediaController mediaController;
	
	/**
	 * 倒数的task
	 */
	private Runnable countDownTask = new Runnable() {

		@Override
		public void run() {
			
			int count = (mediaController.getPlayer().getCurrentADDuration() - mediaController.getPlayer().getCurrentADPosition()) / 100;
			
			if (count == 0) {
				removeCallbacks(this);
			} else {
				promptCountDown.setText("广告剩余" + count + "秒");
				postDelayed(this, 1000);
			}
			
		}
		
	};

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

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

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

	private void init(Context context) {
		setId(MediaController.generateID());
		
		LayoutInflater.from(context).inflate(R.layout.media_controller_prompt_ad_countdown, this);

		promptCountDown = (TextView) findViewById(R.id.prompt_countdown);
	}

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

		post(countDownTask);
	}

	@Override
	public void onHide(MediaController mediaController) {
		removeCallbacks(countDownTask);
	}

	@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:
			case KeyEvent.KEYCODE_BACK:
				break;
			default:
				ret = true;
				break;
			}
		}
		return ret;
	}

	@Override
	public void clear() {
		removeCallbacks(countDownTask);
	}

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

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

}
