package pothos_tegaki;


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.Point2D;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * 動作チェック用
 * 
 * @author miuramo
 *
 */
public class PothosTestApplication extends JPanel implements MouseListener, MouseMotionListener, TegakiCallBack {
	private static final long serialVersionUID = -6151727190512894026L;
	static PothosLibRecognizer recognizer;

	static {
		recognizer = new PothosLibRecognizer();
	}

	ArrayList<ArrayList<Point2D>> pointsVec;
	ArrayList<Point2D> currentLine;
	Point preP = null;
	
	public PothosTestApplication(){
		setPreferredSize(new Dimension(500,500));
		pointsVec = new ArrayList<ArrayList<Point2D>>();
		addMouseListener(this);
		addMouseMotionListener(this);
		
	}
	public void paintComponent(Graphics g){
		g.setColor(Color.white);
		Dimension d = getSize();
		g.fillRect(0,0,d.getSize().width,d.getSize().height);
		
		g.setColor(Color.blue);
		Point2D pre = null;
		for(ArrayList<Point2D> pw : pointsVec){
			for(int i=1;i<pw.size();i++){
				pre = pw.get(i-1);
				Point2D now = pw.get(i);
				g.drawLine((int)pre.getX(),(int)pre.getY(),(int)now.getX(),(int)now.getY());
			}
		}
	}
	public void recog(){
		if (pointsVec.size()>2) {
			TegakiRecogTask trt = new TegakiRecogTask(this);
			trt.setPointArray(pointsVec);
			System.out.println(recognizer.recognize(trt));
		}
		repaint();
	}
	
	public static void main(String[] args) {
		JFrame jf = new JFrame("TegakiPanel(枠なし文字認識エンジンのテスト)");
		final PothosTestApplication tegakiP = new PothosTestApplication();
		JButton recogB = new JButton("Recog!");
		JButton clearB = new JButton("Clear");
//		Box box = new Box(BoxLayout.Y_AXIS);
		
//		Enumeration<JCheckBox> enu = cb2int.keys();
//		while(enu.hasMoreElements()){
//			JCheckBox jcb = enu.nextElement();
//			box.add(jcb);
//			jcb.addItemListener(tegakiP);
//		}
		
		jf.getContentPane().add(tegakiP);
		jf.getContentPane().add(recogB, BorderLayout.SOUTH);
		jf.getContentPane().add(clearB, BorderLayout.NORTH);
//		jf.getContentPane().add(box, BorderLayout.WEST);
		jf.pack();
		jf.setVisible(true);
		jf.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent we){
				System.exit(0);
			}
		});
		jf.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		
		
		recogB.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				tegakiP.recog();
			}
		});
		clearB.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				tegakiP.pointsVec = new ArrayList<ArrayList<Point2D>>();
				tegakiP.repaint();
			}
		});
	}
	
	public void mouseClicked(MouseEvent arg0) {
	}
	
	public void mousePressed(MouseEvent arg0) {
		currentLine = new ArrayList<Point2D>();
		pointsVec.add(currentLine);
		Point p = arg0.getPoint();
		currentLine.add(new Point2D.Float(p.x, p.y));
		repaint();
	}
	
	public void mouseReleased(MouseEvent arg0) {
		Point p = arg0.getPoint();
		currentLine.add(new Point2D.Float(p.x, p.y));
		currentLine = null;
	}
	
	public void mouseEntered(MouseEvent arg0) {
	}
	
	public void mouseExited(MouseEvent arg0) {
	}
	
	public void mouseDragged(MouseEvent arg0) {
		Point p = arg0.getPoint();
		currentLine.add(new Point2D.Float(p.x, p.y));
		repaint();
	}
	
	public void mouseMoved(MouseEvent arg0) {		
	}
//	public void itemStateChanged(ItemEvent arg0) {
//		int tmask = 0;
//		Enumeration<JCheckBox> enu = cb2int.keys();
//		while(enu.hasMoreElements()){
//			JCheckBox jcb = enu.nextElement();
//			if (jcb.isSelected()){
//				tmask |= cb2int.get(jcb);
//			}
//		}
//		TegakiPanel.mask = tmask;
//		System.out.println("mask : "+tmask);
//		/*
//		Object src = arg0.getSource();
//		if (src instanceof JCheckBox){
//			JCheckBox cb = (JCheckBox) src;
//			int tm = cb2int.get(cb);
//			if (cb.isSelected()){
//				TegakiPanel.recognizer.mask &= tm; 
//			} else {
//				TegakiPanel.recognizer.mask ^= tm; 				 
//			}
//		}*/
//	}
	@Override
	public void callback(TegakiRecogTask s) {
		System.out.println(s.result);
	}
	
}
