package net.beadsproject.touch.hardware.tests;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.geom.Point2D;

import net.beadsproject.beads.core.AudioContext;
import net.beadsproject.beads.core.UGen;
import net.beadsproject.beads.data.Buffer;
import net.beadsproject.beads.ugens.PolyLimit;
import net.beadsproject.beads.ugens.Static;
import net.beadsproject.beads.ugens.WavePlayer;
import net.beadsproject.touch.examples.JFrameFullScreenTouchSurface;
import net.beadsproject.touch.hardware.TouchSurfaceListener;
import processing.core.PGraphics;
import processing.core.PGraphics2D;

public class WavePlayerSurface extends JFrameFullScreenTouchSurface implements TouchSurfaceListener {
		
	PGraphics gfx;
	Point2D[] oldPositions;
	Color[] colors;
	WavePlayer[] wps;
	UGen g;
	AudioContext ac;
	
	public void setup(){		
		
	}	

	@Override
	public void moved(int i, int x, int y) {
		gfx.beginDraw();		
		float freq = (x + 150)/4.f;
		Point2D p = new Point2D.Double(x, y);
		Point2D o = oldPositions[i]; 
		if (o != null)
		{
			// draw a line from old to new position
			Color c = colors[i];
			gfx.stroke(c.getRed(),c.getGreen(),c.getBlue());
			gfx.line((float)o.getX(),(float)o.getY(),(float)p.getX(),(float)p.getY());
		}
		oldPositions[i] = p;
		
		if (wps[i]!=null)
		{
			wps[i].getFrequencyEnvelope().setValue(freq);
		}
		else
		{
			
			Buffer b = null;
    		if (y > .5)
    		{
    			b = Buffer.SINE;
    		}
    		else
    		{
    			b = Buffer.SQUARE;
    		}	
			wps[i] = new WavePlayer(ac, new Static(ac, freq), b);
    		g.addInput(wps[i]);
		}	
		gfx.endDraw();		
	}

	@Override
	public void pressed(int i, int x, int y) {
		// TODO Auto-generated method stub
		float freq = (x + 150)/4.f;
    	float fy = (float) (1.0*y/gfx.height);		    	
    	
		if (wps[i]!=null)
		{
			g.removeAllConnections(wps[i]);
		}
		
		Buffer b = null;
		if (fy > .5)
		{
			b = Buffer.SINE;
		}
		else
		{
			b = Buffer.SQUARE;
		}
	
		wps[i] = new WavePlayer(ac, new Static(ac, freq), b);
		g.addInput(wps[i]);
	}

	@Override
	public void released(int i) {
		// TODO Auto-generated method stub
		oldPositions[i] = null;
		if (wps[i]!=null)
		{
			g.removeAllConnections(wps[i]);
		}
		wps[i] = null;		
	}
	
	public WavePlayerSurface()
	{
		ac = new AudioContext();
		PolyLimit pl = new PolyLimit(ac,2,20);
		g = pl;
		ac.out.addInput(pl);	
		
		gfx = new PGraphics2D();
		gfx.setSize(width,height);
		prepaint();			
		
		oldPositions = new Point2D[100];
		colors = new Color[100];
		wps = new WavePlayer[100];		
		for(int i=0;i<100;i++)
		{
			oldPositions[i] = null;
			wps[i] = null;
			colors[i] = new Color(Color.HSBtoRGB((float)Math.random(),0.5f,0.5f));
		}
		
		touchSurface.setListener(this);
		touchSurface.start();
		ac.start();
	}
	
	public void paint(Graphics g)
	{
		g.drawImage(gfx.getImage(),0,0,null);
	}
	
	public void prepaint()
	{
		gfx.beginDraw();
		gfx.background(255);
		gfx.noFill();
		gfx.stroke(0);
		gfx.strokeWeight(2);
		gfx.endDraw();
	}
	
	static public void main(String[] args)
	{
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
		    public void run() 
		    {
		    	new WavePlayerSurface();
		    }
	    });		
	}
}
