package jp.ac.kyutech.mns.ist;

import java.awt.Color;
import java.util.ArrayList;

import edu.umd.cs.piccolo.event.PBasicInputEventHandler;
import edu.umd.cs.piccolo.event.PInputEvent;
import edu.umd.cs.piccolo.nodes.PPath;

public class DrawPenToolbar extends PPath {

	private static final long serialVersionUID = 1232113462523688590L;
	ArrayList<PPath> palettes; 
//	PCamera camera;
	public Color color;
	public int width;
	ArrayList<PenWidth> penwidths;
	public DrawPenToolbar(){
//		camera = cam;
		
		palettes = new ArrayList<PPath>();
		for(float f=0.0f; f<0.8f; f+=0.06f){
			final PPath p = PPath.createRectangle(0, 0, 18, 15);
			Color c = new Color(Color.HSBtoRGB(f, 0.9f, 0.7f));
			p.setPaint(c);
			if (color == null) color = c;
			addChild(p);
			p.offset(palettes.size()*18, 0);
			palettes.add(p);
			p.addAttribute("nozoom", true);
			
			p.addInputEventListener(new PBasicInputEventHandler(){
				public void mouseEntered(PInputEvent e){
					p.setScale(2.0f);
				}
				public void mousePressed(PInputEvent e){
					color = (Color) p.getPaint();
					setColor(color);
				}
				public void mouseExited(PInputEvent e){
					p.setScale(1.0f);
				}
			});
		}
		final PPath p = PPath.createRectangle(0, 0, 18, 15);
		p.setPaint(new Color(0, 0, 90));
		addChild(p);
		p.offset(palettes.size()*18, 0);
		palettes.add(p);
//		p.addAttribute("nozoom", true);
		
		p.addInputEventListener(new PBasicInputEventHandler(){
			public void mouseEntered(PInputEvent e){
				p.setScale(2.0f);
			}
			public void mousePressed(PInputEvent e){
				color = (Color) p.getPaint();
				setColor(color);
			}
			public void mouseExited(PInputEvent e){
				p.setScale(1.0f);
			}
		});
		
		penwidths = new ArrayList<DrawPenToolbar.PenWidth>();
		for(int i=2;i<=10;i+=2){
			PenWidth pw = new PenWidth(i);
			addChild(pw);
			palettes.add(pw);
			penwidths.add(pw);
			pw.offset(palettes.size()*18, 0);
		}
		
		setPenWidth(6);
		setBounds(getFullBounds());
	}
	public void setColor(Color c){
		color = c;
		for(PenWidth pw : penwidths){
			pw.centerP.setPaint(c);
			pw.centerP.setStrokePaint(c);
		}
	}
	public void setPenWidth(int w){
		for(PenWidth pw : penwidths){
			pw.setPaint(Color.white);
		}
		PenWidth pw = penwidths.get(w/2-1);
		pw.setPaint(Color.yellow);
		width = w;
	}
	class PenWidth extends PPath {
		private static final long serialVersionUID = -3703176136509527442L;
		int w;
		PPath centerP;
		PenWidth(int _w){
			setPathToRectangle(0, 0, 18, 16);
			setPaint(Color.white);
			w = _w;
			centerP = PPath.createEllipse(9-w/2, 8-w/2, w, w);
			centerP.setPaint(color);
			centerP.setStrokePaint(color);
			addChild(centerP);
			addInputEventListener(new PBasicInputEventHandler(){
				public void mouseEntered(PInputEvent e){
					setStroke(Note.getCachedBasicStroke(2f));
				}
				public void mousePressed(PInputEvent e){
					setPenWidth(w);
				}
				public void mouseExited(PInputEvent e){
					setStroke(Note.getCachedBasicStroke(1f));
				}
			});
		}
	}
}
