package client.audio;

import client.Client;
import client.gui.element.Slider;
import client.gui.element.SliderCallback;
import client.vars.CVar;
import client.vars.CVarCategory;
import common.sound.EventType;
import common.util.Color;

public enum Volume implements CVar {
	MASTER("master", "Gesamt"),
	MUSIC("music", "Musik"),
	SFX("sfx", "Geräusche", EventType.SOUND_EFFECT),
	GUI("gui", "Oberfläche", EventType.UI_INTERFACE);
	
	private static final Volume[] LOOKUP = new Volume[EventType.values().length];
	
	public final String id;
	public final String name;
	public final EventType type;
	
	private int value = 100;
	
	private Volume(String id, String name, EventType type) {
		this.id = id;
		this.name = name;
		this.type = type;
	}
	
	private Volume(String id, String name) {
		this(id, name, null);
	}
	
	static {
		for(Volume volume : values()) {
			if(volume.type != null)
				LOOKUP[volume.type.ordinal()] = volume;
		}
	}
	
	public static Volume getByType(EventType type) {
		return LOOKUP[type.ordinal()];
	}

	public String getDisplay() {
		return this.name;
	}
	
	public String getCVarName() {
		return "snd_volume_" + this.id;
	}
	
	public String getType() {
		return Color.DARK_GREEN + "volume";
	}
	
	public CVarCategory getCategory() {
		return CVarCategory.SOUND;
	}
	
	public boolean parse(String str) {
		int value;
		try {
			value = Integer.parseInt(str);
		}
		catch(NumberFormatException e) {
			return false;
		}
		if(value < 0 || value > 100)
			return false;
		this.value = value;
		this.apply();
		return true;
	}
	
	public String format() {
		return "" + this.value;
	}
	
	public String getDefault() {
		return "100";
	}
	
	public void setDefault() {
		this.value = 100;
		this.apply();
	}
	
	public String getValues() {
		return "0..100";
	}
	
	public void apply() {
		if(Client.CLIENT.getAudioInterface() != null)
			Client.CLIENT.getAudioInterface().alSetVolume(this, (short)(((float)this.value) / 100.0f * 32767.0f));
	}
	
	public Slider selector(int x, int y, int w, int h) {
		return new Slider(x, y, w, h, 0, 0, 100, 100, this.value, new SliderCallback() {
			public void use(Slider elem, int value) {
				Volume.this.value = value;
				Volume.this.apply();
			}
		}, this.name, "%");
	}
}
