package zero1hd.polyjet.ui.builders;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.utils.FloatArray;

public class AudioGraph extends Actor {
	Pixmap audioGraph;
	Texture textureOfGraph;
	FloatArray mainGraph;
	FloatArray overlayGraph;
	int dataIndex;
	int displayMode;
	
	public AudioGraph(int graphSizeW, int graphSizeH) {
		audioGraph = new Pixmap(graphSizeW, graphSizeH, Format.RGBA8888);
		audioGraph.setColor(0.1f, 0.1f, 0.1f, 0.75f);
		audioGraph.fill();
		
		textureOfGraph = new Texture(audioGraph);
		setWidth(graphSizeW);
		setHeight(graphSizeH);
	}
	
	public void setAudioDataIndex(int audioDataIndex) {
		this.dataIndex = audioDataIndex;
	}
	
	float scale = 0.2f;
	@Override
	public void act(float delta) {
		audioGraph.setColor(0f, 0f, 0f, 0.75f);
		audioGraph.fill();
		
		if (Gdx.input.isKeyPressed(Keys.COMMA)) {
			if (scale > 0.02) {
				scale -= 0.005f;
			}
		} else if (Gdx.input.isKeyPressed(Keys.PERIOD)) {
			if (scale < 1.5) {
				scale += 0.005f;
			}
		}
		if (Gdx.input.isKeyJustPressed(Keys.M)) {
			displayMode++;
			if (displayMode != 3) {
				Gdx.app.debug("Graph", "Switching to another graph.");
			} else {
				displayMode = 0;
			}
		}
		switch (displayMode) {
		case 0:
			audioGraph.setColor(1f, 0f, 0f, 0.75f);
			for (int x = 0; x < audioGraph.getWidth(); x++) {
				try {
					audioGraph.drawLine(x, audioGraph.getHeight(), x, (int) (audioGraph.getHeight()-mainGraph.get(dataIndex+x-audioGraph.getWidth()/2)*scale));
				} catch (NullPointerException | IndexOutOfBoundsException e) {
					
				}
			}

		case 1:
			audioGraph.setColor(0f, 0f, 1f, 0.75f);
			for (int x = 0; x < audioGraph.getWidth() -1; x++) {
				try {
					audioGraph.drawLine(x, audioGraph.getHeight(), x, (int) (audioGraph.getHeight()-overlayGraph.get(dataIndex+x-audioGraph.getWidth()/2)*scale));
				} catch (NullPointerException | IndexOutOfBoundsException e) {
					
				}
			}
			break;
		case 2:
			audioGraph.setColor(1f, 0f, 0f, 0.75f);
			for (int x = 0; x < audioGraph.getWidth(); x++) {
				try {
					audioGraph.drawLine(x, audioGraph.getHeight(), x, (int) (audioGraph.getHeight()-mainGraph.get(dataIndex+x-audioGraph.getWidth()/2)*scale));
				} catch (NullPointerException | IndexOutOfBoundsException e) {
					
				}
			}
			break;
		}

		
		
		audioGraph.setColor(0f, 1f, 0f, 0.95f);
		audioGraph.drawLine(audioGraph.getWidth()/2, 0, audioGraph.getWidth()/2, audioGraph.getHeight());
		
		textureOfGraph.draw(audioGraph, 0, 0);
		super.act(delta);
	}
	
	@Override
	public void draw(Batch batch, float parentAlpha) {
		batch.draw(textureOfGraph, getX(), getY(), getWidth(), getHeight());
		super.draw(batch, parentAlpha);
	}
	
	public void setGraphingData(FloatArray dataSet1, FloatArray dataSet2) {
		this.mainGraph = dataSet1;
		this.overlayGraph = dataSet2;
	}
	
}
