package com.backfire.popupwindow;

import net.yeeyo.doudizhu.feiyu.R;
import android.content.ContentResolver;
import android.content.Context;
import android.media.AudioManager;
import android.net.Uri;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import com.backfire.doudizhu.ActivityBase;
import com.backfire.doudizhu.common.DdzApplication;
import com.backfire.doudizhu.common.ShareData;
import com.backfire.room.RoomActivity;
import com.backfire.room.data.ConfigCenter;

public class SettingPopUpWindow  extends BasePopupWindow{
	private View view;
	private int width;
	private int height;
	private ImageView setting_close;
	private SeekBar seekbar_sound;//音效调节
	private SeekBar seekbar_music;//背景音乐调节
	private SeekBar seekbar_bright;//屏幕亮度调节
	private CheckBox checkbox_volume;//静音模式
	
	private WindowManager.LayoutParams lp;
	private final float MAX_LIANGDU = 205f;
	private final int MIN_LIANGDU = 50;
	/**
	 * @param activity
	 */
	public SettingPopUpWindow(ActivityBase activity) {
		super(activity);
		this.initSetting();
	}
	private void initSetting(){
		LayoutInflater mLayoutInflater = (LayoutInflater) mActivity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		int layoutRourse = R.layout.setting_pop;
		view = mLayoutInflater.inflate(layoutRourse, null);
		
		view.setFocusableInTouchMode(true);
		
		lp = mActivity.getWindow().getAttributes();  
		setting_close = (ImageView) view.findViewById(R.id.setting_close);
		seekbar_sound = (SeekBar) view.findViewById(R.id.seekbar_sound);
		seekbar_music = (SeekBar) view.findViewById(R.id.seekbar_music);
		checkbox_volume=(CheckBox) view.findViewById(R.id.checkbox_volume);
		
		((TextView) view.findViewById(R.id.seekbar_sound_txt)).getPaint().setFakeBoldText(true);
		((TextView) view.findViewById(R.id.seekbar_music_txt)).getPaint().setFakeBoldText(true);
		((TextView) view.findViewById(R.id.checkbox_volume_txt)).getPaint().setFakeBoldText(true);
		((TextView) view.findViewById(R.id.seekbar_bright_txt)).getPaint().setFakeBoldText(true);
		
		AudioManager mAudioManager = (AudioManager) mActivity.getSystemService(Context.AUDIO_SERVICE);
		float max = mAudioManager.getStreamMaxVolume( AudioManager.STREAM_MUSIC );
	    float current = mAudioManager.getStreamVolume( AudioManager.STREAM_MUSIC );
	    int bg_music = ShareData.getInstance().getIntValue("BG_MUSIC", (int)((current/max)*100f));
	    int music_voice = ShareData.getInstance().getIntValue("MUSIC_VOICE", (int)((current/max)*100f));
	    boolean volume_off=ConfigCenter.volume_off;
	    checkbox_volume.setChecked(volume_off);
	    if(volume_off){
	    	seekbar_sound.setEnabled(false);
			seekbar_music.setEnabled(false);
	    }
	    seekbar_sound.setProgress(music_voice);
	    seekbar_music.setProgress(bg_music);
		
		seekbar_bright = (SeekBar) view.findViewById(R.id.seekbar_bright);
		seekbar_bright.setMax((int)MAX_LIANGDU);
		seekbar_bright.setProgress((int)(getScreenBrightness()-MIN_LIANGDU));
		
		this.setFocusable(true);
		this.setConsumeKeyEvent(true);
		ImageView popupview = (ImageView) view.findViewById(R.id.select_bg);
		width = popupview.getLayoutParams().width;
		height = popupview.getLayoutParams().height;
		this.createPopupWindow(view, width, height);
	}
	
	/**
	 * 为各组件设置监听器
	 */
	public void setListens() {
		checkbox_volume.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked){
					ConfigCenter.volume_off=true;
					if(RoomActivity.class.isInstance(mActivity))DdzApplication.getApplication().getPlayMedia().stopBgMusic();
					seekbar_sound.setEnabled(false);
					seekbar_music.setEnabled(false);
					ShareData.getInstance().saveBooleanValue("VOLUME_OFF", ConfigCenter.volume_off);
				}else{
				    ConfigCenter.volume_off=false;
				    if(RoomActivity.class.isInstance(mActivity))DdzApplication.getApplication().getPlayMedia().startBgMusic();
					seekbar_sound.setEnabled(true);
					seekbar_music.setEnabled(true);
					ShareData.getInstance().saveBooleanValue("VOLUME_OFF", ConfigCenter.volume_off);
				}
				
				mActivity.playBtnPressMusic();
			}
		});
		setting_close.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				CloseWindow();
				
				mActivity.playBtnPressMusic();
			}
			
		});
		view.setOnKeyListener(new OnKeyListener() {
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				CloseWindow();
				return false;
			}
		});
		seekbar_bright.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				int current_yx = seekBar.getProgress();
				saveBrightness(mActivity.getContentResolver(),current_yx+MIN_LIANGDU);
			}

			@Override
			public void onProgressChanged(SeekBar seekBar,
					int progress, boolean fromUser) {
				int current_yx = seekBar.getProgress();
				lp.screenBrightness = (float)(current_yx+MIN_LIANGDU)/(MAX_LIANGDU+MIN_LIANGDU);     
				mActivity.getWindow().setAttributes(lp); 
			}

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub

			}
		});
		seekbar_sound.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				int current_yx = seekBar.getProgress();
				ShareData.getInstance().saveIntValue("MUSIC_VOICE", current_yx);
			}

			@Override
			public void onProgressChanged(SeekBar seekBar,
					int progress, boolean fromUser) {
			}

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub

			}
		});
		seekbar_music.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				int current_yx = seekBar.getProgress();
				ShareData.getInstance().saveIntValue("BG_MUSIC", current_yx);
				DdzApplication.getApplication().getPlayMedia().setVolume((float)current_yx/100f);
			}

			@Override
			public void onProgressChanged(SeekBar seekBar,
					int progress, boolean fromUser) {
			}

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub

			}
		});
	}
	/** 
     * 停止自动亮度调节 
     * 
     * @param activity 
     */ 
    public void stopAutoBrightness() { 
        Settings.System.putInt(mActivity.getContentResolver(), 
                Settings.System.SCREEN_BRIGHTNESS_MODE, 
                Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL); 
    } 

	/** 
     * 开启亮度自动调节
     * 
     */ 
    public  void startAutoBrightness() { 
        Settings.System.putInt(mActivity.getContentResolver(), 
                Settings.System.SCREEN_BRIGHTNESS_MODE, 
                Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC); 
    } 
	/** 
     * 获取屏幕的亮度
     * 
     * @return 
     */ 
    public int getScreenBrightness() { 
        int nowBrightnessValue = 0; 
        ContentResolver resolver = mActivity.getContentResolver(); 
        try {
			nowBrightnessValue = android.provider.Settings.System.getInt( 
			        resolver, Settings.System.SCREEN_BRIGHTNESS);
		} catch (SettingNotFoundException e) {
			e.printStackTrace();
		} 
        return nowBrightnessValue; 
    } 
	public void saveBrightness(ContentResolver resolver, int brightness) {
		startAutoBrightness();
        Uri uri = android.provider.Settings.System 
                .getUriFor("screen_brightness"); 
        android.provider.Settings.System.putInt(resolver, "screen_brightness", 
                brightness); 
        resolver.notifyChange(uri, null); 
		stopAutoBrightness();
    } 
	@Override
	protected boolean popupWindowWillShow() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	protected void popupWindowDidShow() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void popupWindowWillDissmiss() {
		// TODO Auto-generated method stub
		
	}
}