package jp.ac.kyutech.mns.ist.plugin;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

import jp.ac.kyutech.mns.ist.Sheet;
import jp.ac.kyutech.mns.ist.ShortStroke;
import jp.ac.kyutech.mns.ist.play.SSManager;
import jp.ac.kyutech.mns.ist.play.ShortStrokeHolder;
import jp.ac.kyutech.mns.ist.play.TimePlaySlider;
import edu.umd.cs.piccolo.PCanvas;

public class SheetPlugin_Play extends DefaultPlugin implements ActionListener {
	Sheet sheet;
	PCanvas canvas;
	boolean isActive = false;
	
	ShortStrokeHolder strokes_on_sheet;
	SSManager ssmanager;
	TimePlaySlider timeplaycont;
	public SheetPlugin_Play(PCanvas can, Sheet f){
		canvas = can;
		sheet = f;
		strokes_on_sheet = new ShortStrokeHolder();
	}

	public void setEnabled(boolean b){
		if (b){
			strokes_on_sheet.clear();
			for(ShortStroke ss: sheet.getNote().getAll()){
				if (ss.paperid == sheet.num) strokes_on_sheet.addSS(ss);
			}
			ssmanager = new SSManager(strokes_on_sheet); // ここに再生表示したい筆記 Iterable
			// を指定する
			timeplaycont = new TimePlaySlider(sheet, ssmanager, canvas.getCamera());
			timeplaycont.fitToBottom();
		}else {
			timeplaycont.dispose();
			ssmanager = null;
			strokes_on_sheet.clear();
		}
	}
	public void actionPerformed(ActionEvent e){
		System.out.println(e.getActionCommand());
		if (e.getActionCommand().equals("Start")){
			setEnabled(true);
		} else if (e.getActionCommand().equals("Stop")){
			setEnabled(false);
		}
	}
	public void addMenu(JPopupMenu menu){
		JMenu insM;

		insM = new JMenu("Play");

		JMenuItem mi;

		mi = new JMenuItem("Start");
		mi.addActionListener(this);
		insM.add(mi);

		mi = new JMenuItem("Stop");
		mi.addActionListener(this);
		insM.add(mi);


		menu.add(insM);
	}
}
