import java.io.File;

import net.beadsproject.beads.core.AudioContext;
import processing.core.*;
import controlP5.*;

/**
 * A basic sequencer app - demonstrates loading and caching a .jsfrx SoundEffect.
 * @author ben
 *
 */
@SuppressWarnings("serial")
public class Sequencer extends PApplet {
	AudioContext ac;
		
	static final String dir = "sfx/";
	GridControl gc[] = new GridControl[4];
	static final int COLS = 1;
	static final int ROWS = 4;
	
	SoundEffect se;
	
	public void setup()
	{
		frameRate(30);
		background(0);
		//size(GridControl.WIDTH*COLS,GridControl.HEIGHT*ROWS);
		size(256,280);
		//size(256,500);
		setLayout(null);
		
		ac = new AudioContext();
		String filename[] = {dir + "hit.jsfxr", 
					dir + "croak.jsfxr",
					dir + "tone.jsfxr", 
					dir + "coin.jsfxr"};
		try {
			int index = 0;
			int height = 0;
			for(int i=0;i<COLS;i++)
			{
				for(int j=0;j<ROWS;j++)
				{
					println("Loading sound effect " + (1+index) + "/" + gc.length);
					String spec = loadStrings(filename[index])[0];
					SoundEffect se = SoundEffect.read(spec);
					GridControl g = null;
					if (index==2 || index==3)
					{
						int o = 0;
						if (index==3) o = 1;
						g = new GridControlNote(o,ac,this,0,height,se);
					}
					else
						g = new GridControl(ac,this,0,height,se);
					g.setup();
					g.mute();
					height += g.HEIGHT;
					gc[index] = g;  
					index++;
				}
			}			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
				
		preset();
		ac.start();
	}
	
	public void draw()
	{
		background(50);		
		
		// update the time
		for(GridControl g:gc)
		{
			g.setTime((float)ac.getTime()%GridControl.MSWIDTH);
			g.draw();
			image(g.pg,g.x,g.y);
		}
	}
	
	public void mousePressed()
	{		
		for(GridControl g:gc)
			g.mousePressed(mouseX,mouseY);
	}	
	
	public void keyPressed()
	{
		switch (key)
		{
			case 'r':
			{
				// find the grid control it is in and then randomise it
				for(GridControl g:gc)
				{
					if (g.inBounds(mouseX,mouseY))
						g.random();
				}
				break;
			}
			case 'c':
			{
				// find the grid control it is in and then clear it
				for(GridControl g:gc)
				{
					if (g.inBounds(mouseX,mouseY))
						g.clear();
				}
				break;
			}
			case 'm':
			{
				// find the grid control it is in and then clear it
				for(GridControl g:gc)
				{
					if (g.inBounds(mouseX,mouseY))
					{
						if (g.muted()) g.unmute();
						else g.mute();
					}
				}
				break;
			}
			case 'd': // dump out the sequencer state to console
			{
				for(GridControl g: gc)
				{
					for(int j=0;j<g.SIZEH;j++)
					{
						for(int i=0;i<g.SIZEW;i++)
						{
							if (g.get(i,j)) print("#");
							else print (".");
						}
						println();
					}
					println();
				}
			}
		}
	}
	
	/**
	 * A lame starting pattern.
	 */
	public void preset()
	{
		String drums[] = {
		".............#.##...............",
		"#...#..........##.......#.......",
		"........#.....#.....#......##.#.",
		"....#.......##..#..............."
		};
		String bass[] = {
		"........##....##.#.##....#......",
		"....#......#......#.....#....#..",
		"....#.....#.#...........#...#...",
		"................................",
		};
		String tone[] = {
		"................................",
		"................................",
		"....#...##.............#.#......",
		"................................",
		"....#......#...........#.#.##...",
		"..........#.#...................",
		"....#........#.........#.#......",
		"...........................#....",
		"...............#...#.#..........",
		"...#..........................#.",
		"...................#.........#..",
		".........................#..#...",
		"................................",
		};
		String coin[] = {
		"....#...........................",
		"................................",
		"................................",
		"................................",
		"................................",
		"................................",
		"................................",
		"................................",
		"................................",
		"..............................#.",
		".............................#..",
		"............................#...",
		"................................",
		};
		
		String pre[][] = {drums,bass,tone,coin};
		int index = 0;
		for (GridControl g: gc)
		{
			String score[] = pre[index];
			for(int j=0;j<g.SIZEH;j++)
			for(int i=0;i<g.SIZEW;i++)
			{
				if (score[j].charAt(i)=='.') g.set(i,j,false);
				else g.set(i,j,true);
				
			}
			index++;
		}
	}
}
