package com.wasu.cs.widget.mediacontrol;

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 android.widget.TextView;

import com.wasu.cs.model.DemandPlayinfo;
import com.wasu.cs.model.DemandProgram;
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;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;

import cn.com.wasu.main.R;

/**
 * 码率选择控制条
 */
public class BitrateController extends FrameLayout implements IMediaControllerChildView<MediaController> {
	
	private MediaController mediaController;
	
	private TextView settingTitle;
	private SidePanelNavigator settingValue;
	
	private List<INavigatorChildModel> values;

	public List<INavigatorChildModel> getValues() {
		return values;
	}

	public void setValues(List<INavigatorChildModel> values) {
		if (values != null) {
			this.values = values;
			settingValue.setNavigatorChildrenModel(values);
		}
	}
	
	/**
	 * 码率选择是否显示过，即用户是否按过菜单键
	 */
	private boolean isShowed;

	public boolean isShowed() {
		return isShowed;
	}

	public void setShowed(boolean isShowed) {
		this.isShowed = isShowed;
	}
	
	public BitrateController(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}

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

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

	private void init(Context context) {
		setId(MediaController.generateID());
		
		LayoutInflater.from(context).inflate(R.layout.media_controller_bitratepanel, this);
		
		settingTitle = (TextView) findViewById(R.id.setting_title);
		settingValue = (SidePanelNavigator) findViewById(R.id.setting_value);
		
		settingTitle.setText("视频码率");
	}
	
	@Override
	protected void onAttachedToWindow() {
		
		settingValue.requestFocus();
		
		super.onAttachedToWindow();
	}

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

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

		values = new ArrayList<INavigatorChildModel>();
		
		DemandProgram assetInfo = mediaController.getAssetInfo();
		if (assetInfo == null) {
			return;
		}
		
		Set<Entry<Long, DemandPlayinfo>> entrySet = assetInfo.getPlayinfoList().entrySet();
		
		long fitRate = getContext().getSharedPreferences(MediaController.SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE).getLong(MediaController.SHARED_PREFERENCES_KEY_CURRENT_RATE, WasuPlayerView.DEFAULT_PLAY_RATE);

		int index = 0;
		int fitIndex = 0;
		for (Entry<Long, DemandPlayinfo> entry : entrySet) {
			long rate = entry.getKey();
			String tag = entry.getValue().getTag();
			
			values.add(new SettingItemClip(tag, rate));
			

			if (rate == fitRate) {
				fitIndex = index;
			} else {
				index++;
			}
		}
			
		settingValue.setNavigatorChildrenModel(values);
		settingValue.setSelectedViewIndex(fitIndex);
	}

	@Override
	public void onHide(MediaController mediaController) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onShield(KeyEvent event) {
		boolean ret = false;
		if (event.getAction() == KeyEvent.ACTION_DOWN) {
			
			switch (event.getKeyCode()) {
			case KeyEvent.KEYCODE_DPAD_UP:
				settingValue.goPrev();
				ret = true;
				break;
			case KeyEvent.KEYCODE_DPAD_DOWN:
				settingValue.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:
				ret = true;
				break;
			case KeyEvent.KEYCODE_BACK:
				break;
			case KeyEvent.KEYCODE_MENU:
				ret = true;
				break;
			case KeyEvent.KEYCODE_DPAD_CENTER:
			case KeyEvent.KEYCODE_ENTER:
				settingValue.click();
				ret = true;
				break;
			default:
				ret = true;
				break;
			}
		}
		return ret;
	}
	
	@Override
	public void clear() {
		
	}

	public class SettingItemClip implements INavigatorChildModel {

		/**
		 * 名称
		 */
		String name;
		/**
		 * 值
		 */
		Object value;

		public SettingItemClip(String name, Object value) {
			this.name = name;
			this.value = value;
		}

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

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

		@Override
		public void onClick() {
			long fitRate = getContext().getSharedPreferences(MediaController.SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE).getLong(MediaController.SHARED_PREFERENCES_KEY_CURRENT_RATE, WasuPlayerView.DEFAULT_PLAY_RATE);
			if (fitRate == (Long) this.value) {
				return;
			}
			
			mediaController.post(new Runnable() {

				@Override
				public void run() {
					mediaController.hideCurrentView();
				}
				
			});
			
			getContext().getSharedPreferences(MediaController.SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE).edit().putLong(MediaController.SHARED_PREFERENCES_KEY_CURRENT_RATE, (Long) this.value).commit();
			
			mediaController.getPlayer().stopPlayback(false);
			((WasuPlayerView) mediaController.getPlayer()).ignoreHeadAd();

			if (getContext() instanceof ActivityPlayer) {
				Intent intent = new Intent(ActivityPlayer.ACTION);
				intent.putExtra(ActivityPlayer.ASSET_INFO, mediaController.getAssetInfo());
				intent.putExtra(ActivityPlayer.ASSET_URL, mediaController.getAssetUrl());
				intent.putExtra(ActivityPlayer.PLAY_TYPE, mediaController.getPlayType());
				intent.putExtra(ActivityPlayer.PLAY_INDEX, mediaController.getPlayIndex());
				intent.putExtra(ActivityPlayer.ASSET_LIST, (Serializable) mediaController.getAssetList());
				intent.putExtra(ActivityPlayer.TRACEID, mediaController.getTraceid());
				intent.putExtra(ActivityPlayer.ASSET_BITRATE, (Long) this.value);
				getContext().startActivity(intent);
			} else {
				PlayerParams playerParams = mediaController.getPlayerParams();
				playerParams.setBitrate((Long) this.value);
				((WasuPlayerView) mediaController.getPlayer()).play(playerParams);
			}
		}
		
		@Override
		public int getWidth() {
			return ViewGroup.LayoutParams.MATCH_PARENT;
		}

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

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

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

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

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

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

		@Override
		public int getHintTextSize() {
			return 0;
		}

	}

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

}
