package zero1hd.polyjet.ui.windows;

import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.WidgetGroup;
import com.badlogic.gdx.scenes.scene2d.ui.Window;

import zero1hd.polyjet.Polyjet;
import zero1hd.polyjet.audio.AudioAnalyzer;
import zero1hd.polyjet.audio.AudioData;

public class BeatViewer extends Window {
	Pixmap lights;
	int songIndex;
	AudioData music;
	Texture lightOn;
	private AudioAnalyzer data;
	
	public BeatViewer(String title, Skin skin, Polyjet core) {
		super(title, skin);
		defaults().space(5f);
		setSize(64, 100);
		
		
		//Draw the 'lights'
		lights = new Pixmap(16, 16, Format.RGBA8888);
		lights.setColor(1, 0.2f, 0.2f, 1f);
		lights.fillCircle(lights.getWidth()/2, lights.getHeight()/2, 7);
		lightOn = new Texture(lights);
		
		
		WidgetGroup bassBar = new WidgetGroup();
		final Image bgBassBar = new Image(core.getDefaultSkin().getPatch("bar-empty"));
		bgBassBar.setHeight(50f);
		bassBar.setSize(bgBassBar.getWidth(), bgBassBar.getHeight());

		final Image bassBarFill = new Image(core.getDefaultSkin().getPatch("bar-fill")) {
			@Override
			public void act(float delta) {
				if (music != null && data.getBassPeaks().get(songIndex) != 0) {
					clearActions();
					try {
						addAction(Actions.sequence(Actions.sizeTo(getWidth(), (data.getBassPeaks().get(songIndex)/data.getBassMaxValue())*bgBassBar.getHeight()), Actions.sizeTo(getWidth(), 0, 0.3f)));
					} catch (IndexOutOfBoundsException e) {
						
					}
				}
				super.act(delta);
			}
		};
		bassBarFill.setHeight(0f);
		bassBar.addActor(bgBassBar);
		bassBar.addActor(bassBarFill);
		add(bassBar).minSize(bassBar.getWidth(), bassBar.getHeight());
		
		WidgetGroup UMBar = new WidgetGroup();
		final Image bgUMBar = new Image(core.getDefaultSkin().getPatch("bar-empty"));
		bgUMBar.setHeight(50f);
		UMBar.setSize(bgUMBar.getWidth(), bgUMBar.getHeight());
		Image UMBarFill = new Image(core.getDefaultSkin().getPatch("bar-fill")) {
			@Override
			public void act(float delta) {
				if (music != null && data.getUMPeaks().get(songIndex) != 0) {
					clearActions();
					try {
						addAction(Actions.sequence(Actions.sizeTo(getWidth(), (data.getUMPeaks().get(songIndex)/data.getUMMaxValue())*bgUMBar.getHeight()), Actions.sizeTo(getWidth(), 0f, 0.3f)));
					} catch (IndexOutOfBoundsException e) {
						
					}
				}
				super.act(delta);
			}
		};
		UMBarFill.setHeight(0f);
		UMBar.addActor(bgUMBar);
		UMBar.addActor(UMBarFill);
		add(UMBar).minSize(UMBar.getWidth(), UMBar.getHeight());
		
		
		row();
		
		Image bassIndicator = new Image(lightOn) {
			@Override
			public void act(float delta) {
				if (music != null && data.getBassPeaks().get(songIndex) != 0) {
					clearActions();
					addAction(Actions.sequence(Actions.alpha(1f), Actions.fadeOut(0.15f)));
				}
				super.act(delta);
			}
		};
		
		bassIndicator.setColor(1f, 1f, 1f, 0f);
		add(bassIndicator).center();
		
		
		Image UMIndicator = new Image(lightOn) {
			@Override
			public void act(float delta) {
				if (music != null && data.getUMPeaks().get(songIndex) != 0) {
					clearActions();
					addAction(Actions.sequence(Actions.alpha(1f), Actions.fadeOut(0.15f)));
				}
				super.act(delta);
			}
		};
		UMIndicator.setColor(1f, 1f, 1f, 0f);
		add(UMIndicator).center();
	}
	
	@Override
	public void act(float delta) {
		if (music != null) {
			songIndex = music.getReadIndex();
		}
		super.act(delta);
	}
	
	public void setMusic(AudioData audioData, AudioAnalyzer analyzer) {
		this.music = audioData;
		this.data = analyzer;
	}
}
