package com.wasu.cs.widget.mediacontrol;

import java.util.List;

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

import com.wasu.cs.ui.ActivityPlayer;
import com.wasu.cs.widget.mediacontrol.SidePanelNavigator.INavigatorChildModel;
import com.wasu.cs.widget.videoview.PlayerParams;
import com.wasu.cs.widget.videoview.WasuPlayerView;

/**
 * 左边控制条
 */
public class LeftSidePanelController extends FrameLayout implements IMediaControllerChildView<MediaController> {

	/**
	 * 电影
	 */
	public static final int NAVIGATOR_TYPE_MOVIE = 0;
	/**
	 * 电视剧
	 */
	public static final int NAVIGATOR_TYPE_TV_SERIES = 1;
	/**
	 * 短视频
	 */
	public static final int NAVIGATOR_TYPE_VIDEO_CLIPS = 2;

	private MediaController mediaController;
	private SidePanelNavigator mNavigator;
	
	private int selectIndex;

	public int getSelectIndex() {
		return selectIndex;
	}

	public void setSelectIndex(int selectIndex) {
		this.selectIndex = selectIndex;
	}

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

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

	public LeftSidePanelController(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_leftsidepanel, this);

		mNavigator = (SidePanelNavigator) findViewById(R.id.side_panel_navigator);

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

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

		mNavigator.setSelectedViewIndex(selectIndex);
	}

	@Override
	public void onHide(MediaController mediaController) {

	}

	@Override
	public boolean onShield(KeyEvent event) {
		boolean ret = false;
		if (event.getAction() == KeyEvent.ACTION_DOWN) {

			switch (event.getKeyCode()) {
			case KeyEvent.KEYCODE_DPAD_UP:
				mNavigator.goPrev();
				ret = true;
				break;
			case KeyEvent.KEYCODE_DPAD_DOWN:
				mNavigator.goNext();
				ret = true;
				break;
			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_BACK:
				break;
			case KeyEvent.KEYCODE_DPAD_CENTER:
			case KeyEvent.KEYCODE_ENTER:
				mNavigator.click();
				ret = true;
				break;
			default:
				ret = true;
				break;
			}
		}
		return ret;
	}

	@Override
	public void clear() {
		selectIndex = 0;
	}

	public void setNavigator(List<INavigatorChildModel> navs) {
		if (navs == null) {
			return;
		}

		mNavigator.refresh();
		mNavigator.setNavigatorChildrenModel(navs);
	}

	/**
	 * 电视剧navigator
	 */
	public class TvSeires implements INavigatorChildModel {

		/**
		 * 集数
		 */
		int episode;

		public TvSeires(int episode) {
			this.episode = episode;
		}

		@Override
		public String getString() {
			return String.valueOf(episode + 1) + "集";
		}

		@Override
		public String getHintString() {
			return "正在播放";
		}

		@Override
		public void onClick() {
			if (episode == mediaController.getAssetInfo().getCurPlayIndex()) {
				return;
			}
			
			mediaController.getPlayer().stopPlayback();
			mediaController.setVisibility(false, getId());
			
			if (mediaController.getPlayer() instanceof WasuPlayerView) {
				PlayerParams playerParams = new PlayerParams();
				mediaController.getAssetInfo().setCurPlayIndex(episode);
				playerParams.setAssetInfo(mediaController.getAssetInfo());
				((WasuPlayerView) mediaController.getPlayer()).play(playerParams);
			} else if (mediaController.getPlayer() instanceof com.wasu.comp.videoview.WasuPlayerView) {
				Intent intent = new Intent(ActivityPlayer.ACTION);
				mediaController.getAssetInfo().setCurPlayIndex(episode);
				intent.putExtra(ActivityPlayer.ASSET_INFO, mediaController.getAssetInfo());
				mediaController.getContext().startActivity(intent);
			}

		}

		@Override
		public int getWidth() {
			return getContext().getResources().getDimensionPixelSize(R.dimen.d_300dp);
		}

		@Override
		public int getHeight() {
			return ViewGroup.LayoutParams.WRAP_CONTENT;
		}

		@Override
		public int getPaddingLeft() {
			return getContext().getResources().getDimensionPixelSize(R.dimen.d_0dp);
		}

		@Override
		public int getPaddingTop() {
			return getContext().getResources().getDimensionPixelSize(R.dimen.d_33dp);
		}

		@Override
		public int getPaddingRight() {
			return getContext().getResources().getDimensionPixelSize(R.dimen.d_0dp);
		}

		@Override
		public int getPaddingBottom() {
			return getContext().getResources().getDimensionPixelSize(R.dimen.d_33dp);
		}

		@Override
		public int getTextSize() {
			return getContext().getResources().getDimensionPixelSize(R.dimen.s_33sp);
		}

		@Override
		public int getHintTextSize() {
			return getContext().getResources().getDimensionPixelSize(R.dimen.s_24sp);
		}

	}
	
	/**
	 * 电影navigator
	 */
	public class MovieSeires implements INavigatorChildModel {
		
		int clipTime;

		public MovieSeires(int time) {
			clipTime = time;
		}

		@Override
		public String getString() {
			return clipTime / (60 * 1000) + "分钟";
		}

		@Override
		public void onClick() {
			mediaController.setVisibility(false, getId());
			mediaController.getPlayer().seekTo(clipTime);
		}

		@Override
		public int getWidth() {
			return getContext().getResources().getDimensionPixelSize(R.dimen.d_300dp);
		}

		@Override
		public int getHeight() {
			return ViewGroup.LayoutParams.WRAP_CONTENT;
		}

		@Override
		public int getPaddingLeft() {
			return getContext().getResources().getDimensionPixelSize(R.dimen.d_0dp);
		}

		@Override
		public int getPaddingTop() {
			return getContext().getResources().getDimensionPixelSize(R.dimen.d_33dp);
		}

		@Override
		public int getPaddingRight() {
			return getContext().getResources().getDimensionPixelSize(R.dimen.d_0dp);
		}

		@Override
		public int getPaddingBottom() {
			return getContext().getResources().getDimensionPixelSize(R.dimen.d_33dp);
		}

		@Override
		public int getTextSize() {
			return getContext().getResources().getDimensionPixelSize(R.dimen.s_33sp);
		}

		@Override
		public int getHintTextSize() {
			return getContext().getResources().getDimensionPixelSize(R.dimen.s_24sp);
		}

		@Override
		public String getHintString() {
			return "正在播放";
		}

	}

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

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

}
