package com.wasu.cs.widget.mediacontrol;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

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 cn.com.wasu.main.Common;
import cn.com.wasu.main.R;

import com.wasu.authsdk.AuthSDK;
import com.wasu.comp.ad.AdView;
import com.wasu.comp.ad.WasuAdEngine;
import com.wasu.cs.model.DemandPlayinfo;
import com.wasu.cs.model.DemandProgram;
import com.wasu.cs.widget.videoview.WasuLivePlayerView;
import com.wasu.cs.widget.videoview.WasuPlayerView;
import com.wasu.util.VersionUtils;

/**
 * 暂停提示
 */
public class PromptPauseAD extends FrameLayout implements IMediaControllerChildView<MediaController> {

	private MediaController mediaController;

	private FrameLayout container;
	
	private boolean hideFlag;

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

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

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

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

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

		container = (FrameLayout) findViewById(R.id.container);
	}
	
	private String getPlayTag() {
		if (mediaController == null || mediaController.getAssetInfo() == null) {
			return null;
		}
		
		Set<Entry<Long, DemandPlayinfo>> entrySet = mediaController.getAssetInfo().getPlayinfoList().entrySet();
		long fitRate = getContext().getSharedPreferences(MediaController.SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE).getLong(MediaController.SHARED_PREFERENCES_KEY_CURRENT_RATE, WasuPlayerView.DEFAULT_PLAY_RATE);
		for (Entry<Long, DemandPlayinfo> entry : entrySet) {
			long rate = entry.getKey();
			String tag = entry.getValue().getTag();
			
			if (rate == fitRate) {
				return tag;
			}
		}
		
		return null;
	}

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

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

		mediaController.getPlayer().pause();

		container.removeAllViews();
		
		Map<String, String> params = new HashMap<String, String>();
		
		if (this.mediaController != null && this.mediaController.getAssetInfo() != null) {
			params.put("prol", Common.APP_NAME + "_" + VersionUtils.getVersionName(getContext()));
			params.put("ccid", mediaController.getAssetInfo().getCatId());
			params.put("cid", mediaController.getAssetInfo().getId());
			params.put("pay", mediaController.getAssetInfo().getIsFree() == 1 ? "0" : "1");
			params.put("strrate", getPlayTag());
			params.put("vd", "");
			params.put("ccName", mediaController.getAssetInfo().getCatName());
			params.put("cName", mediaController.getAssetInfo().getTitle());
			
			params.put("userid", AuthSDK.getInstance().getValue(AuthSDK.KEY_TVID));
			params.put("columnid", mediaController.getAssetInfo().getCatId());
			params.put("programid", mediaController.getAssetInfo().getId());
			params.put("featureid", mediaController.getAssetInfo().getIsFree() == 1 ? "true" : "false");
		}
		AdView view = WasuAdEngine.getInstance().getImageAd(76, R.drawable.ad_wasu_logo, false, params, null);
		
		container.addView(view, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
	}
	
	@Override
	public void onHide(MediaController mediaController) {
		hideFlag = false;
		mediaController.getPlayer().start();
	}

	@Override
	public boolean onShield(KeyEvent event) {
		boolean ret = false;
		if (event.getAction() == KeyEvent.ACTION_DOWN) {
			switch (event.getKeyCode()) {
			case KeyEvent.KEYCODE_DPAD_LEFT:
			case KeyEvent.KEYCODE_DPAD_RIGHT:
				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:
			case KeyEvent.KEYCODE_BACK:
				hideFlag = true;
				
				if (mediaController.getPlayer() instanceof WasuPlayerView || mediaController.getPlayer() instanceof WasuLivePlayerView) {
					((WasuPlayerView) mediaController.getPlayer()).requestFocus();
				}
				break;
			default:
				ret = true;
				break;
			}
		}
		return ret;
	}

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

	}

	@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) {
        if (mediaController == null || mediaController.getPlayer() == null) {
            return;
        }

        DemandProgram assetInfo = mediaController.getAssetInfo();

        if (assetInfo == null) {
            return;
        }

        if (assetInfo.getAssetFrom() == 91) {
            mediaController.getPlayer().pause();
        }
	}

	@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) {
			if (!relativeViews.contains(v)) {
				relativeViews.add(v);
			}
		}
	}

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

}
