package com.wasu.cs.widget.mediacontrol;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.widget.FrameLayout;

import java.util.List;

import basic.app.TvAppLike;
import cn.com.wasu.main.R;

/**
 * 静音控制条
 */
public class VolumeMuteController extends FrameLayout implements IMediaControllerChildView<IMediaController> {

	private IMediaController mediaController;
	
	private AudioManager audioManager;
	
	private int currentVolume;

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

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

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

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

		LayoutInflater.from(context).inflate(R.layout.media_controller_volume_mute, this);
		
		audioManager = getAudioManger();
		
		currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
	}
	
	private AudioManager getAudioManger() {
		return (AudioManager) TvAppLike.getContext().getSystemService(Context.AUDIO_SERVICE);
	}

	public void volumeChanged(int volume) {
		currentVolume = volume;
	}
	
	@Override
	public boolean isAutoHide() {
		return false;
	}

	@Override
	public void onShow(IMediaController mediaController) {
		this.mediaController = mediaController;
		
		if (audioManager == null) {
			audioManager = getAudioManger();
		}
		audioManager.setStreamMute(AudioManager.STREAM_MUSIC, true);
	}

	@Override
	public void onHide(IMediaController mediaController) {
		audioManager.setStreamMute(AudioManager.STREAM_MUSIC, false);
		audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, currentVolume, 0);
	}

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

			switch (event.getKeyCode()) {
				case KeyEvent.KEYCODE_DPAD_UP:
				case KeyEvent.KEYCODE_DPAD_DOWN:
				case KeyEvent.KEYCODE_DPAD_LEFT:
				case KeyEvent.KEYCODE_DPAD_RIGHT:
				case KeyEvent.KEYCODE_VOLUME_UP:
				case KeyEvent.KEYCODE_VOLUME_DOWN:
				case KeyEvent.KEYCODE_MENU:
				case KeyEvent.KEYCODE_DPAD_CENTER:
				case KeyEvent.KEYCODE_ENTER:
				case KeyEvent.KEYCODE_BACK:
					break;
				case KeyEvent.KEYCODE_VOLUME_MUTE:
					if (audioManager == null) {
						audioManager = getAudioManger();
					}
					this.mediaController.setVisibility(false, getId());
					ret = true;
					break;
				default:
					ret = true;
					break;
				}
		}
		return ret;
	}

	@Override
	public void clear() {
		audioManager = null;
	}

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

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

}
