package zero1hd.polyjet.ui.windows;

import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Window;
import com.badlogic.gdx.utils.Disposable;

public class LoadingWindow extends Window implements Disposable {

	private Label status;
	private Sound closeSound;
	private Sound openSound;
	private float vol;
	public LoadingWindow(Skin skin, boolean progress, AssetManager assets, float vol) {
		super("loading...", skin, "tinted");
		
		this.openSound = assets.get("pop_open.ogg", Sound.class);
		this.closeSound = assets.get("pop_close.ogg", Sound.class);
		this.vol = vol;
		Image loading = new Image(skin, "loading");
		loading.addAction(Actions.forever(Actions.rotateBy(-360f, 2f)));
		add(loading).left();
		System.out.println(loading.getHeight());
		
		setSize(loading.getWidth()+20f, loading.getHeight()+40f);
		
		loading.setOrigin(loading.getWidth()/2, loading.getHeight()/2);

		
		
		if (progress) {
			status = new Label("[   ]", skin, "sub-font", skin.getColor("default"));
			add(status).spaceLeft(10f).expandX().right();
			setSize(getWidth() + status.getWidth() + 45f, (loading.getHeight() >= status.getHeight() ? loading.getHeight() : status.getHeight()) + 74);
		}
		
	}
	
	public LoadingWindow(Skin skin, String styleName, boolean progress, AssetManager assets, float vol) {
		super("loading...", skin, styleName);
		
		this.openSound = assets.get("pop_open.ogg", Sound.class);
		this.closeSound = assets.get("pop_close.ogg", Sound.class);
		this.vol = vol;
		Image loading = new Image(skin, "loading");
		loading.addAction(Actions.forever(Actions.rotateBy(-360f, 2f)));
		add(loading).left();
		
		setSize(loading.getWidth()+20f, loading.getHeight()+40f);
		
		loading.setOrigin(loading.getWidth()/2, loading.getHeight()/2);

		
		
		if (progress) {
			status = new Label("[   ]", skin, "sub-font", skin.getColor("default"));
			add(status).spaceLeft(10f).expandX().right();
			setSize(getWidth() + status.getWidth() + 45f, (loading.getHeight() >= status.getHeight() ? loading.getHeight() : status.getHeight()) + 74);
		}
		
	}
	
	public void playOpenSound() {
		openSound.play(vol/100f);
	}
	
	@Override
	public boolean remove() {
		closeSound.play(vol/100f);
		return super.remove();
	}
	public void setProgress(float progress) {
		status.setText(String.valueOf((int) progress) + '%');
	}

	@Override
	public void dispose() {
		openSound.dispose();
		closeSound.dispose();
	}
}
