package jp.ac.kyutech.mns.ist;


import java.awt.Color;
import java.awt.Dimension;

import jp.ac.kyutech.mns.ist.util.CameraFitConstraint;
import jp.ac.kyutech.mns.ist.util.CameraFitConstraint.PositionEnum;
import edu.umd.cs.piccolo.PNode;
import edu.umd.cs.piccolo.event.PBasicInputEventHandler;
import edu.umd.cs.piccolo.event.PInputEvent;
import edu.umd.cs.piccolo.nodes.PPath;
import edu.umd.cs.piccolo.nodes.PText;

public class InsideToolbar_SheetVisible extends PPath {
	private static final long serialVersionUID = -971944466554582429L;
	SimpleATN satn;
	PPath[][] buttons = new PPath[3][7];
	//	ArrayList<PPath> palettes;
	String[] lbls = {"Hide", "Cond", "Always"};
	public InsideToolbar_SheetVisible(SimpleATN _satn){
		satn = _satn;

		//		palettes = new ArrayList<PPath>();
		for(int j=0;j<3;j++){
			for(int i=0;i<7;i++){
				if (j==1&&i==0) continue;
				PPath p = PPath.createRectangle(0, 0, 25, 25);
				p.setPaint(Color.lightGray);
				PText pt = new PText(String.valueOf(i));
				if (i==0) pt.setText("N");
				pt.setScale(1.4f);
				pt.setOffset((p.getWidth()-pt.getWidth()*pt.getScale())/2,(p.getHeight()-pt.getHeight()*pt.getScale())/2);
				p.addChild(pt);
				pt.setPickable(false);
				addChild(p);
				p.offset((i-1)*25+50, (3-j)*25);
				//				palettes.add(p);
				buttons[j][i]=p;
				p.addAttribute("nozoom", true);
				p.addAttribute("sheetnum", i);
				p.addAttribute("status", j);


				p.addInputEventListener(new PBasicInputEventHandler(){
					public void mouseEntered(PInputEvent e){
						PNode pn = e.getPickedNode();
						if (pn instanceof PPath){
							((PPath)e.getPickedNode()).setStroke(Note.getCachedBasicStroke(2f));
							e.getPickedNode().moveToFront();
						}
						//						InsideToolbar_SheetVisible.this.setVisible(true);
					}
					public void mousePressed(PInputEvent e){
						PNode pn = e.getPickedNode();
						if (pn instanceof PPath){
							int snum = ((Integer)((PPath)e.getPickedNode()).getAttribute("sheetnum")).intValue();
							int stat = ((Integer)((PPath)e.getPickedNode()).getAttribute("status")).intValue();
//							satn.sheetVisibleMode[snum] = stat;
							satn.setSheetVisible(snum, stat);
							updateStatus();
						}
					}
					public void mouseExited(PInputEvent e){
						PNode pn = e.getPickedNode();
						if (pn instanceof PPath){
							((PPath)e.getPickedNode()).setStroke(Note.getCachedBasicStroke(1f));
						}
						//						InsideToolbar_SheetVisible.this.setVisible(false);
					}
				});
			}
			//all set
			PPath p = PPath.createRectangle(0, 0, 60, 25);
			p.setPaint(Color.lightGray);
			PText pt = new PText(lbls[j]);
			pt.setScale(1.4f);
			pt.setOffset((p.getWidth()-pt.getWidth()*pt.getScale())/2,(p.getHeight()-pt.getHeight()*pt.getScale())/2);
			p.addChild(pt);
			pt.setPickable(false);
			addChild(p);
			p.offset(6*25+52, (3-j)*25);
			//			palettes.add(p);
			p.addAttribute("nozoom", true);
			p.addAttribute("sheetnum", -1);
			p.addAttribute("status", j);


			p.addInputEventListener(new PBasicInputEventHandler(){
				public void mouseEntered(PInputEvent e){
					PNode pn = e.getPickedNode();
					if (pn instanceof PPath){
						((PPath)e.getPickedNode()).setStroke(Note.getCachedBasicStroke(2f));
						e.getPickedNode().moveToFront();
					}
					//					InsideToolbar_SheetVisible.this.setVisible(true);
				}
				public void mousePressed(PInputEvent e){
					//					int snum = ((Integer)((PPath)e.getPickedNode()).getAttribute("sheetnum")).intValue();
					PNode pn = e.getPickedNode();
					if (pn instanceof PPath){
						int stat = ((Integer)((PPath)e.getPickedNode()).getAttribute("status")).intValue();
						for(int snum=1;snum<7;snum++){
							satn.sheetVisibleMode[snum] = stat;
						}
						satn.setSheetVisible(-1, stat);
						updateStatus();
					}
				}
				public void mouseExited(PInputEvent e){
					PNode pn = e.getPickedNode();
					if (pn instanceof PPath){
						((PPath)e.getPickedNode()).setStroke(Note.getCachedBasicStroke(1f));
					}
					//					InsideToolbar_SheetVisible.this.setVisible(false);
				}
			});
		}

		//		satn.getCanvas().getCamera().addChild(this);
		setBounds(getFullBoundsReference());
		//		fitToFR();

		CameraFitConstraint cfc = new CameraFitConstraint(this, satn.getCanvas().getCamera(), PositionEnum.BOTTOMLEFT, new Dimension(0,24));
		cfc.fitTo(satn.getCanvas().getCamera().getBoundsReference());
		satn.getCanvas().getCamera().addPropertyChangeListener(cfc);

		setTransparency(0.5f);
		setVisible(false);

	}
	public void setVisible(boolean f){
		super.setVisible(f);
		updateStatus();
	}
	public void updateStatus(){
		for(int i=1;i<7;i++){
			int status = satn.sheetVisibleMode[i];
			for(int j=0;j<3;j++) buttons[j][i].setPaint(Color.lightGray);
			buttons[status][i].setPaint(Color.pink);
		}
	}
	//	public void fitToFR(){
	//		setOffset(5 ,-this.getFullBounds().getHeight()*this.getScale()/3);
	//	}
}
