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.data.SampleManager;
import net.beadsproject.beads.ugens.PolyLimit;
import net.beadsproject.beads.ugens.SamplePlayer;
import net.beadsproject.beads.ugens.Static;
import net.beadsproject.beads.ugens.WavePlayer;
import net.beadsproject.beads.ugens.SamplePlayer.LoopType;
import net.beadsproject.touch.examples.JFrameFullScreenTouchSurface;
import net.beadsproject.touch.hardware.TouchSurfaceListener;
import processing.core.PGraphics;
import processing.core.PGraphics2D;

public class SamplePlayerSurface extends JFrameFullScreenTouchSurface implements TouchSurfaceListener {
		
	PGraphics gfx;
	Point2D[] oldPositions;
	Color[] colors;
	SamplePlayer[] sps;
	UGen g;
	AudioContext ac;	

	int numSounds;
	
	public void newSamplePlayer(int i, int x, int y)
	{
		float rate = 2.0f * x  / width;		
		int s = (int) (numSounds * ((float)y / height));		
		sps[i] = new SamplePlayer(ac, SampleManager.fromGroup("sounds",s));
		sps[i].setRateEnvelope(new Static(ac, rate));	
		sps[i].setLoopType(LoopType.LOOP_FORWARDS);
		g.addInput(sps[i]);
	}
	
	public void modifySamplePlayer(int i, int x, int y)
	{
		float rate = 2.0f * x  / width;
		sps[i].getRateEnvelope().setValue(rate);
	}	
	
	@Override
	public void moved(int i, int x, int y) {
		gfx.beginDraw();		
		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 (sps[i]!=null)
		{
			modifySamplePlayer(i,x,y);
		}
		else
		{
			newSamplePlayer(i,x,y);
		}	
		gfx.endDraw();		
	}

	@Override
	public void pressed(int i, int x, int y) {
		// TODO Auto-generated method stub
		if (sps[i]!=null)
		{
			g.removeAllConnections(sps[i]);
		}
		newSamplePlayer(i,x,y);	
	}

	@Override
	public void released(int i) {
		// TODO Auto-generated method stub
		oldPositions[i] = null;
		if (sps[i]!=null)
		{
			g.removeAllConnections(sps[i]);
		}
		sps[i] = null;		
	}
	
	public SamplePlayerSurface()
	{
		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];
		sps = new SamplePlayer[100];		
		for(int i=0;i<100;i++)
		{
			oldPositions[i] = null;
			sps[i] = null;
			colors[i] = new Color(Color.HSBtoRGB((float)Math.random(),0.5f,0.5f));
		}
		
		String folder = "D:\\audio\\Chiptune Odyssey\\Loops\\Nintendo Entertainment System\\Drums";
		// String folder = "D:\\audio\\Chiptune Odyssey\\Loops\\Nintendo Entertainment System\\Bass";
				
		SampleManager.group("sounds", folder);
		numSounds = SampleManager.getGroup("sounds").size();
		
		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 SamplePlayerSurface();
		    }
	    });		
	}
}
