package com.wasu.cs.widget.mediacontrol;

import android.content.Context;
import android.media.MediaPlayer;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;

import com.wasu.cs.model.DemandProgram;
import com.wasu.cs.widget.videoview.WasuPlayerView;

import java.util.ArrayList;
import java.util.List;

import cn.com.wasu.main.R;

/**
 * 底部提示条
 */
public class PromptFooter extends RelativeLayout implements IMediaControllerChildView<MediaController> {

	private MediaController mediaController;

	private boolean isFullScreen;

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

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

	public PromptFooter(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_prompt_footer, this);
	}

	public void handleFullScreen(boolean isFullScreen) {
		this.isFullScreen = isFullScreen;
	}

	@Override
	public boolean isAutoHide() {
		return true;
	}

	@Override
	public void onShow(MediaController mediaController) {
		this.mediaController = mediaController;

		if (this.mediaController == null || this.mediaController.getAssetInfo() == null) {
			return;
		}

		removeAllViews();

		switch (this.mediaController.getAssetInfo().getAssetType()) {
		case DemandProgram.SHOWTYPE_MOVIE: {
//			RecommendMoviePanel p = new RecommendMoviePanel(getContext());
			RecommendMoviePanelR p = new RecommendMoviePanelR(getContext(), this.mediaController, DemandProgram.SHOWTYPE_MOVIE);
			addView(p);
//			p.attachMediaController(mediaController);
			break;
		}
		case DemandProgram.SHOWTYPE_TV_SERIES: { // 电视选集类型
			RecommendTVPanel p = new RecommendTVPanel(getContext());
			addView(p);
			p.attachMediaController(mediaController);
			break;
		}
		case DemandProgram.SHOWTYPE_NEITHER_MOVIE_NOR_TV:
		case DemandProgram.SHOWTYPE_UNKNOWN: {
//			RecommendVideoClipPanel p = new RecommendVideoClipPanel(getContext());
			RecommendMoviePanelR p = new RecommendMoviePanelR(getContext(), this.mediaController, DemandProgram.SHOWTYPE_NEITHER_MOVIE_NOR_TV);
			addView(p);
//			p.attachMediaController(this.mediaController);
			break;
		}
		}

		requestFocus();
	}

	@Override
	public void onHide(MediaController mediaController) {
		if (isFullScreen && mediaController.getPlayer() instanceof WasuPlayerView) {
			((WasuPlayerView) mediaController.getPlayer()).requestFocus();
		}
	}

	@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_DPAD_CENTER:
			case KeyEvent.KEYCODE_ENTER:
				View topView = getChildAt(0);
				if (topView != null) {
					topView.dispatchKeyEvent(event);
				}
				ret = true;
				break;
			case KeyEvent.KEYCODE_VOLUME_UP:
			case KeyEvent.KEYCODE_VOLUME_DOWN:
			case KeyEvent.KEYCODE_VOLUME_MUTE:
			case KeyEvent.KEYCODE_MENU:
				ret = true;
				break;
			case KeyEvent.KEYCODE_BACK:
				break;
			default:
				ret = true;
				break;
			}
		}
		return ret;
	}

	@Override
	public void clear() {
		isFullScreen = false;
	}

	@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

	}

	private List<IMediaControllerChildView<MediaController>> relativeViews = new ArrayList<IMediaControllerChildView<MediaController>>();
	
	@Override
	public void setRelativeViews(IMediaControllerChildView<MediaController>... views) {
		for (IMediaControllerChildView<MediaController> v : views) {
			relativeViews.add(v);
		}
	}

	@Override
	public List<IMediaControllerChildView<MediaController>> getRelativeViews() {
		return relativeViews;
	}

}
