package com.wasu.cs.widget;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;

import com.wasu.comp.videoview.IMediaListener;
import com.wasu.cs.model.HomeColumnModel;
import com.wasu.cs.model.SpecialColumnModel.ChannelListModel;
import com.wasu.cs.model.SpecialColumnModel.ChannelModel;
import com.wasu.cs.widget.mediacontrol.MediaController;
import com.wasu.cs.widget.videoview.WasuLivePlayerView;
import com.wasu.module.msgqueue.MsgHandler;
import com.wasu.module.msgqueue.MsgIdConstant;
import com.wasu.module.msgqueue.MsgQueueModule;

import java.util.ArrayList;

import cn.com.wasu.main.R;

//import android.widget.VideoView;

/**
 *
 * @ClassName: MainHPageItem22
 * @Description: 内容区视频播放小窗口的Item
 * @author 海迪
 * @date 2015年7月8日 下午6:26:55
 *
 */
public class MainHPageItemVideoLive extends MainHPageItemBase implements Runnable, IMediaListener, MsgHandler {

	private static final int ACTION_IDLE = 1 << 0;
	private static final int ACTION_PLAY = 1 << 1;
	private static final int ACTION_PREPARE = 1 << 2;
	private static final int ACTION_PAUSE = 1 << 3;
	private static final int ACTION_RESUME = 1 << 4;
	private static final int ACTION_STOP = 1 << 5;
	private String layout;
	private String jsonUrl;

	private String firstImgUrl;

	private RelativeLayout mContainer;
	// private ImageView mIconPlay = null;
	// private ImageView contFirstImgView;
	private WasuLivePlayerView mVideoView;

	private ArrayList<ChannelModel> channelList = null;

	private ChannelListModel channel = null;
	// 当前状态
	private int mCurrentStatus = ACTION_IDLE;
	// 目标状态
	private int mTargetStatus = ACTION_IDLE;
	private Activity context;
	private int linkType;

	public MainHPageItemVideoLive(Activity context) {
		super(context);
		this.context = context;
		init(context);
	}

	private void initVideo() {
		mVideoView = new WasuLivePlayerView(context);
		mVideoView.setExcludeOption(MediaController.DISPLAY_PROMPT_EXIT_AD);
		mVideoView.addObserver(this);
		mVideoView.setAnchorView(this, this.context);

	}

	@SuppressLint("NewApi")
	private void init(Context context) {
		LayoutInflater.from(context).inflate(R.layout.main_hpage_item_video, this);
		int w = getResources().getDimensionPixelSize(R.dimen.d_422dp);
		int h = getResources().getDimensionPixelSize(R.dimen.d_270dp);
		setLayoutParams(new LayoutParams(w, h));

		mContainer = (RelativeLayout) findViewById(R.id.item_box);
		mContainer.setBackgroundColor(Color.TRANSPARENT);
		// contFirstImgView = (ImageView) findViewById(R.id.cont_first_img);
		// mIconPlay = (ImageView) findViewById(R.id.cont_status_icon);

		initVideo();

		MsgQueueModule.getInstance().addMsgHandler(MsgIdConstant.MSGID_APP_MIN + 1, this);
	}

	@Override
	public void onClick(View v) {
		mVideoView.toggleFullScreen();

	}

	@Override
	protected void onWindowVisibilityChanged(int visibility) {
		super.onWindowVisibilityChanged(visibility);
		if (ACTION_IDLE == mCurrentStatus) {
			return;
		}
		if (View.VISIBLE == visibility) {
			if (ACTION_STOP == mCurrentStatus) {
				mTargetStatus = ACTION_PLAY;
				removeCallbacks(this);
				post(this);
			}
		} else {
			mTargetStatus = ACTION_STOP;
			removeCallbacks(this);
			post(this);
		}
	}

	/**
	 * 改变播放状态
	 *
	 * @param isChange
	 */
	public void changeStatus(boolean isChange) {
		if (ACTION_IDLE == mCurrentStatus) {
			if(mVideoView!=null && !isCover() && !mVideoView.isPlaying() && !mVideoView.isPreparing()){
				mTargetStatus = ACTION_PLAY;
				removeCallbacks(this);
				post(this);
			}
			return;
		}
		if (isChange) {// 可见继续播放
			// handler1.postDelayed(runnable1, 1000);
			if(mVideoView!=null && !isCover())
				mVideoView.setVisibility(VISIBLE);
			resumePlay();
		} else {
			if(mVideoView!=null)
				mVideoView.setVisibility(INVISIBLE);
			mTargetStatus = ACTION_STOP;
			removeCallbacks(this);
			post(this);
		}
	}

	/**
	 * 对视频小窗口被覆盖后做处理
	 * @param notCover
	 */
	private void converHandle(boolean notCover){
		if (ACTION_IDLE == mCurrentStatus) {
			return;
		}

		if (notCover) {// 可见继续播放
			// handler1.postDelayed(runnable1, 1000);
			if(mVideoView.getVisibility() != VISIBLE){
				mVideoView.setVisibility(VISIBLE);
			}
			resumePlay();
		} else {
			mVideoView.setVisibility(INVISIBLE);
			mTargetStatus = ACTION_STOP;
			post(this);
		}
	}

	Handler handler1 = new Handler();
	Runnable runnable1 = new Runnable() {

		@Override
		public void run() {

			resumePlay();
		}
	};

	private void resumePlay() {
		if(isCover()){
			return;
		}
		if (mCurrentStatus == ACTION_PAUSE) {
			mTargetStatus = ACTION_RESUME;
			removeCallbacks(this);
			post(this);
		}
		if (mCurrentStatus == ACTION_STOP) {
			mTargetStatus = ACTION_PLAY;
			removeCallbacks(this);
			post(this);
		}

	}

	/**
	 * 检测视频窗口被覆盖并处理
	 */
	public void detect() {
//		changeStatus(!isCover());
		converHandle(!isCover());
	}

	@Override
	public void focusChange(View v, boolean hasFocus) {
		if (hasFocus) {
			setBackgroundResource(R.drawable.home_select_focus);
			if (ACTION_IDLE == mCurrentStatus) {
				mTargetStatus = ACTION_PLAY;
				post(this);
			}
		} else {
			setBackgroundResource(0);
			if (ACTION_IDLE == mCurrentStatus) {
				mTargetStatus = ACTION_IDLE;
			} else if (ACTION_PREPARE == mCurrentStatus) {
				mTargetStatus = ACTION_PAUSE;
			}
		}
	}

	@Override
	public String getLayout() {
		return layout;
	}

	@Override
	public String getJsonUrl() {
		return jsonUrl;
	}

	@Override
	public int getRowspan() { // 行
		return 2;
	}

	@Override
	public int getColspan() { // 列
		return 2;
	}

	@Override
	public boolean isFrontItem() {
		return false;
	}

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

	public HomeColumnModel cm;

	@Override
	public HomeColumnModel getData() {
		return cm;
	}

	@Override
	public void initData(HomeColumnModel cm) {
		this.cm = cm;
		this.linkType = cm.getLinkType();

		layout = cm.getLayout();
		jsonUrl = cm.getJsonUrl();
		if (cm.getPicUrlList().size() > 0) {
			firstImgUrl = cm.getPicUrlList().get(0).getBottomPic();
		}

		// if (contFirstImgView != null && !StringUtils.isBlank(firstImgUrl)) {
		// contFirstImgView.setVisibility(View.VISIBLE);
		// ImageFetcherModule.getInstance().attachImage(firstImgUrl,
		// contFirstImgView,
		// getResources().getDimensionPixelSize(R.dimen.d_8dp));
		// }

		channel = cm.getChannel();
		// mVideoView.play(cm.getJsonUrl(),
		// channel.getChannelList().get(0).getChannelId());
	}

	@Override
	public void run() {
		if (mVideoView == null) {
			return;
		}

		if (ACTION_PLAY == mTargetStatus) {
			mTargetStatus = ACTION_IDLE;
			if (!isCover()) {
				mCurrentStatus = ACTION_PREPARE;
				mVideoView.play(cm.getJsonUrl(), channel.getChannelList().get(0).getChannelId());
				Log.d("danxx", "播放成功");
				mVideoView.start();
			}

		} else if (ACTION_PAUSE == mTargetStatus) {
			if (mVideoView.isPlaying()) {
				mTargetStatus = ACTION_IDLE;
				mCurrentStatus = ACTION_PAUSE;
				mVideoView.pause();
			}
		} else if (ACTION_RESUME == mTargetStatus) {
			mTargetStatus = ACTION_IDLE;
			if (!isCover()) {
				mVideoView.start();
				mCurrentStatus = ACTION_PLAY;
			}
		} else if (ACTION_STOP == mTargetStatus) {
			mCurrentStatus = ACTION_STOP;
			mTargetStatus = ACTION_IDLE;
			mVideoView.stopPlayback();
		}

	}

	@Override
	public void onWasuPlayLimit(int arg0, String arg1) {
	}

	@Override
	public void onWasuError(int arg0, String arg1) {
	}

	@Override
	public void onStop(MediaPlayer arg0) {
	}

	@Override
	public void onStatusChanged(MediaPlayer arg0, int arg1) {
		if (IMediaListener.STATE_PLAYING == arg1) {
		}

	}

	@Override
	public void onStart(MediaPlayer arg0) {
	}

	@Override
	public void onSeeking(MediaPlayer arg0) {
	}

	@Override
	public void onSeekComplete(MediaPlayer arg0) {
	}

	@Override
	public void onResume(MediaPlayer arg0) {
	}

	@Override
	public void onProgress(int arg0, int arg1, int arg2) {
		// mCurrentPostion = arg0;
	}

	@Override
	public void onPreparing(MediaPlayer arg0) {
	}

	@Override
	public void onPrepareComplete(MediaPlayer arg0) {
//		if (ACTION_PAUSE == mTargetStatus) {
//			post(this);
//			return;
//		}
//
//		if (ACTION_PREPARE == mCurrentStatus) {
//			mCurrentStatus = ACTION_PLAY;
//			mVideoView.start();
//			return;
//		}
	}

	@Override
	public void onPause(MediaPlayer arg0) {
	}

	@Override
	public void onInfo(MediaPlayer arg0, int arg1, int arg2) {
	}

	@Override
	public void onError(MediaPlayer arg0, int arg1, int arg2) {
	}

	@Override
	public void onCompletion(MediaPlayer arg0) {

		// mCurrentPostion = 0;
		mTargetStatus = ACTION_PLAY;
		removeCallbacks(this);
		post(MainHPageItemVideoLive.this);
	}

	@Override
	public void onAdStatusChanged(int arg0, int arg1) {
	}

	@Override
	public boolean handleMsg(int arg0, int arg1, String arg2, Object arg3) {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean isAliRecItem() {
		// TODO Auto-generated method stub
		return false;
	}

	/**
	 * 控制播放器的暂停和播放
	 * @param isToPlay
	 */
	@Override
	public void notifyPlay(boolean isToPlay) {
		if (isToPlay) {
			mTargetStatus = ACTION_PLAY;
			removeCallbacks(this);
			post(this);
		} else {
			mTargetStatus = ACTION_STOP;
			removeCallbacks(this);
			post(this);
		}

	}

}