package zero1hd.rhythmbullet.ui.windows;

import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Slider;
import com.badlogic.gdx.scenes.scene2d.ui.Window;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;

import zero1hd.rhythmbullet.audio.MusicDataPack;

public class VolumeWindow extends Window {

	private Slider fxVolSlider;
	private Slider musicVolSlider;
	private Preferences prefs;
	
	private MusicDataPack music;
	public VolumeWindow(String title, Skin skin, Preferences prefs) {
		super(title, skin, "tinted");
		this.prefs = prefs;
		setSize(360f, 100f);
		
		Label musicVolSliderLabel = new Label("Music Volume: ", skin, "window-font", skin.getColor("default"));
		add(musicVolSliderLabel).left().padLeft(5f);
		musicVolSlider = new Slider(0, 100, 0.1f, false, skin);
		musicVolSlider.setValue(prefs.getFloat("music vol", 100f));
		add(musicVolSlider).width(200f);
		final Label musicVolPercentage = new Label(MathUtils.round(musicVolSlider.getValue()) + "%", skin, "window-font", skin.getColor("default"));
		musicVolSlider.addListener(new ChangeListener() {
			@Override
			public void changed(ChangeEvent event, Actor actor) {
				save();
				musicVolPercentage.setText(MathUtils.round(musicVolSlider.getValue()) + "%");
				if (music != null) {
					music.getPlaybackMusic().setVolume(musicVolSlider.getValue()/100f);
				}
			}
		});
		
		add(musicVolPercentage).spaceLeft(10f).expandX().left();
		
		row();
		
		Label fxVolSliderLabel = new Label("FX Volume: ", skin, "window-font", skin.getColor("default"));
		add(fxVolSliderLabel).left().padLeft(5f);
		fxVolSlider = new Slider(0, 100, 1, false, skin);
		fxVolSlider.setValue(prefs.getFloat("fx vol", 100f));
		add(fxVolSlider).width(200f);
		final Label fxVolPercentage = new Label(MathUtils.round(fxVolSlider.getValue()) + "%", skin, "window-font", skin.getColor("default"));
		fxVolSlider.addListener(new ChangeListener() {
			@Override
			public void changed(ChangeEvent event, Actor actor) {
				save();
				fxVolPercentage.setText(MathUtils.round(fxVolSlider.getValue()) + "%");
			}
		});
		
		add(fxVolPercentage).spaceLeft(10f).expandX().left();
	}
	
	public void save() {
		prefs.putFloat("music vol", musicVolSlider.getValue());
		prefs.putFloat("fx vol", fxVolSlider.getValue());
		prefs.flush();
	}
	
	public void setMusic(MusicDataPack music) {
		this.music = music;
		if (music != null) {
			music.getPlaybackMusic().setVolume(prefs.getFloat("music vol")/100f);
		}
	}
}
