package com.haxademic.app.haxmapper.overlays;

import java.util.ArrayList;

import processing.core.PConstants;
import processing.core.PGraphics;
import processing.core.PShape;
import processing.core.PVector;

import com.haxademic.core.app.P;
import com.haxademic.core.audio.WaveformData;
import com.haxademic.core.draw.color.ColorHaxEasing;
import com.haxademic.core.draw.util.DrawUtil;
import com.haxademic.core.math.MathUtil;

public class MeshLines {

	protected ArrayList<MeshLineSegment> _meshLineSegments;
	protected PGraphics _texture;
	protected ColorHaxEasing _colorEase;

	protected int _mode = 4;
	protected int _numModes = 7;
	public static final int MODE_EQ_BARS = 0;
	public static final int MODE_LINE_EXTEND = 1;
	public static final int MODE_EQ_TOTAL = 2;
	public static final int MODE_EQ_BARS_BLACK = 3;
	public static final int MODE_WAVEFORMS = 4;
	public static final int MODE_DOTS = 5;
	public static final int MODE_NONE = 6;

	public MeshLines( PGraphics pg ) {
		_texture = pg;
		_meshLineSegments = new ArrayList<MeshLineSegment>();
		_colorEase = new ColorHaxEasing( "#ffffff", 5 );
	}

	public PGraphics texture() {
		return _texture;
	}

	public void addSegment( float x1, float y1, float x2, float y2 ) {
		boolean hasLineAlready = false;
		for( int i=0; i < _meshLineSegments.size(); i++ ) {
			if( _meshLineSegments.get(i).matches( x1, y1, x2, y2 ) == true ) {
				hasLineAlready = true;
			}
		}
		if( hasLineAlready == false ) {
			_meshLineSegments.add( new MeshLineSegment( x1, y1, x2, y2 ) );
		}
	}

	public void update() {
		_colorEase.update();
//		_texture.beginDraw();
//		_texture.clear();
		DrawUtil.setDrawCenter( _texture );

		float spectrumInterval = (int) ( 256 / _meshLineSegments.size() );	// 256 keeps it in the bottom half of the spectrum since the high ends is so overrun

		for( int i=0; i < _meshLineSegments.size(); i++ ) {
			_meshLineSegments.get(i).update( _texture, _mode, _colorEase.colorInt(), P.p.audioIn.getEqAvgBand( 15 ), P.p.audioIn.getEqBand( 20 + P.floor(i*spectrumInterval) ) );
		}

//		_texture.endDraw();
	}
	
	public void updateLineMode() {
		_mode++;
		if( _mode >= _numModes ) _mode = 0;
	}

	public void setColor( int color ) {
		_colorEase.setTargetColorInt( color );
	}

}

@@ -607,30 +607,40 @@
 ;%0A%09p
-rotected int _numModes
+ublic static final int NUM_MODES
  = 7
@@ -2127,47 +2127,132 @@
 mode
-++;%0A%09%09if( _mode %3E= _numModes ) 
+ += MathUtil.randRange(1, 2);%0A%09%09if( _mode %3E= NUM_MODES ) _mode = 0;%0A%09%7D%0A%0A%09public void resetLineMode( int index ) %7B%0A%09%09
 _mode = 
 0;%0A%09
@@ -2239,33 +2239,37 @@
 x ) %7B%0A%09%09_mode = 
-0
+index
 ;%0A%09%7D%0A%0A%09public vo

Add better mode setting