package com.wasu.cs.widget.mediacontrol;

import android.annotation.SuppressLint;
import android.app.Activity;
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.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.wasu.comp.ad.AdView;
import com.wasu.comp.ad.WasuAdEngine;
import com.wasu.cs.widget.videoview.WasuLivePlayerView;
import com.wasu.cs.widget.videoview.WasuPlayerView;

import java.util.List;

import cn.com.wasu.main.R;

/**
 * 退出提示
 */
public class PromptExitAD extends FrameLayout implements IMediaControllerChildView<IMediaController>, OnFocusChangeListener {

	private IMediaController mediaController;

	private TextView cancel;
	private TextView submit;
	private FrameLayout container;
	
	private boolean hideFlag;

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

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

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

	private void init(Context context) {
		setId(MediaController.generateID());

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

		cancel = (TextView) findViewById(R.id.cancel);
		submit = (TextView) findViewById(R.id.submit);
		container = (FrameLayout) findViewById(R.id.container);

		cancel.setOnFocusChangeListener(this);
		submit.setOnFocusChangeListener(this);
		
	}
	
	@Override
	public boolean isAutoHide() {
		return hideFlag;
	}

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

		container.removeAllViews();
		AdView view = WasuAdEngine.getInstance().getImageAd(80, R.drawable.ad_wasu_logo, false, null, null);
		container.addView(view, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
		
		submit.requestFocus();
	}

	@Override
	public void onHide(IMediaController mediaController) {
		hideFlag = false;
	}

	@Override
	public boolean onShield(KeyEvent event) {
		boolean ret = false;
		if (event.getAction() == KeyEvent.ACTION_DOWN) {
			switch (event.getKeyCode()) {
			case KeyEvent.KEYCODE_DPAD_LEFT:
				if (submit.isFocused()) {
					cancel.requestFocus();
				}
				ret = true;
				break;
			case KeyEvent.KEYCODE_DPAD_RIGHT:
				if (cancel.isFocused()) {
					submit.requestFocus();
				}
				ret = true;
				break;
			case KeyEvent.KEYCODE_DPAD_UP:
			case KeyEvent.KEYCODE_DPAD_DOWN:
			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_DPAD_CENTER:
			case KeyEvent.KEYCODE_ENTER:
				hideFlag = true;
				
				if (cancel.isFocused() && getVisibility() == VISIBLE) {
					if (mediaController.getPlayer() instanceof WasuPlayerView || mediaController.getPlayer() instanceof WasuLivePlayerView) {
                        ((View) mediaController).post(new Runnable() {

							@Override
							public void run() {
								mediaController.hideCurrentView();
							}
							
						});
						((View) mediaController.getPlayer()).requestFocus();
					}
				}
				if (submit.isFocused() && getVisibility() == VISIBLE) {
					mediaController.getPlayer().stopPlayback();
					mediaController.clear();
                    ((ViewGroup) mediaController).removeAllViews();
					
					((Activity) getContext()).finish();
				}
				break;
			case KeyEvent.KEYCODE_BACK:
				hideFlag = true;
				
				if (mediaController.getPlayer() instanceof WasuPlayerView || mediaController.getPlayer() instanceof WasuLivePlayerView) {
					((View) mediaController.getPlayer()).requestFocus();
				}
				break;
			default:
				ret = true;
				break;
			}
		}
		return ret;
	}

	@Override
	public void clear() {
		// TODO Auto-generated method stub
		
	}

	@SuppressLint("NewApi")
	@Override
	public void onFocusChange(View v, boolean hasFocus) {
		if (hasFocus) {
			v.animate().setDuration(300).scaleX(1.4f).scaleY(1.4f);
		} else {
			v.animate().setDuration(300).scaleX(1.0f).scaleY(1.0f);
		}
	}

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

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

}
