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.ViewGroup;
import android.widget.FrameLayout;

import com.wasu.cs.model.ILiveAssets;
import com.wasu.cs.widget.mediacontrol.SidePanelNavigator.INavigatorChildModel;
import com.wasu.cs.widget.videoview.WasuLivePlayerView;

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

import cn.com.wasu.main.R;

/**
 * 频道控制条
 */
public class ChannelPanelController extends FrameLayout implements IMediaControllerChildView<LiveMediaController> {

	/**
	 * 电影
	 */
	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 LiveMediaController mediaController;
	private SidePanelNavigator mNavigator;
	
	private int selectIndex;

	public int getSelectIndex() {
		return selectIndex;
	}

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

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

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

	public ChannelPanelController(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_channel_panel, this);

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

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

	@Override
	public void onShow(LiveMediaController mediaController) {
		this.mediaController = mediaController;
		
		ILiveAssets liveInfo = this.mediaController.getLiveInfo();
		if (liveInfo == null) {
			return;
		}
		
		List<INavigatorChildModel> navs = new ArrayList<INavigatorChildModel>();

		int size = liveInfo.getChannelSize();
		for (int i = 0; i < size; i++) {
            String channelId = liveInfo.getChannelId(i);
			ChannelClip channelClip = new ChannelClip(liveInfo.getChannelName(channelId), channelId);
			navs.add(channelClip);
		}
		setNavigator(navs);
		
		mNavigator.setSelectedViewIndex(liveInfo.getIndexByChanenlId(mediaController.getChannelId()));
	}

	@Override
	public void onHide(LiveMediaController 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:
				ret = true;
				break;
			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);
	}

	/**
	 * 频道
	 */
	public class ChannelClip implements INavigatorChildModel {
		
		private String channelName;
		private String channelId;

		public ChannelClip(String channelName, String channelId) {
			this.channelName = channelName;
			this.channelId = channelId;
		}

		@Override
		public String getString() {
			return channelName;
		}

		@Override
		public String getHintString() {
			return "";
		}

		@Override
		public void onClick() {
			if (mediaController == null) {
				return;
			}

            if (this.channelId.equals(mediaController.getChannelId())) {
                return;
            }
			
			mediaController.post(new Runnable() {

				@Override
				public void run() {
					mediaController.hideCurrentView();
				}

			});

            if (mediaController.getPlayer() != null && mediaController.getPlayer() instanceof WasuLivePlayerView) {
                mediaController.getPlayer().stopPlayback();
				getContext().getSharedPreferences(WasuLivePlayerView.CONFIG_FILE, Context.MODE_PRIVATE).edit().putString(WasuLivePlayerView.CHANNEL_KEY, this.channelId).commit();
				((WasuLivePlayerView) mediaController.getPlayer()).play(mediaController.getPlayType(), this.channelId);
			}
		}

		@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_50dp);
		}

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

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

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

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

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

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

}
