package net.trevize.pascalvoc;

import java.awt.BorderLayout;
import java.awt.Point;
import java.util.ArrayList;
import java.util.Vector;

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

import net.trevize.galatee.GEvent;
import net.trevize.galatee.GItem;
import net.trevize.galatee.GListener;
import net.trevize.jmagine.Jmagine;
import net.trevize.jmagine.svgsymbol.SVGPolygon;
import net.trevize.jmagine.svgsymbol.SVGSymbol;
import net.trevize.tinker.SystemCommandHandler2;

/**
 * 
 * 
 * @author Nicolas James <nicolas.james@gmail.com> [[http://njames.trevize.net]]
 * GListenerImpl.java - May 27, 2010
 */

public class GListenerImpl implements GListener {

	class SystemCommandThread extends Thread {
		private SystemCommandHandler2 sch2 = new SystemCommandHandler2();
		private String command;

		public SystemCommandThread(String command) {
			this.command = command;
		}

		public void run() {
			String[] commands = new String[1];
			commands[0] = command;
			sch2.exec(commands);
		}
	}

	class JmagineThread extends Thread {
		private GItem gitem;

		public JmagineThread(GItem gitem) {
			this.gitem = gitem;
		}

		public void run() {
			JFrame f = new JFrame();
			f.setTitle("View image: " + gitem.getLocalFilepath());
			JPanel p0 = new JPanel();
			p0.setLayout(new BorderLayout());
			f.getContentPane().add(p0);
			Jmagine jmagine = new Jmagine(gitem.getLocalFilepath());

			//we get the annotated objects.
			Vector<Object> annotation_objects = (Vector<Object>) gitem
					.getData().get(1);

			//we push a new group for the polygons.
			jmagine.pushSVGGroup("polygons");
			ArrayList<SVGSymbol> svgpolygons_list = new ArrayList<SVGSymbol>();

			int i = 0; //for updating the group id for each polygon.
			for (Object o : annotation_objects) {
				net.trevize.pascalvoc.annotation.jaxb.Object annotation_object = (net.trevize.pascalvoc.annotation.jaxb.Object) o;
				ArrayList<Point> points = new ArrayList<Point>();

				Point point0 = new Point(Integer.parseInt(annotation_object
						.getBndbox().getXmin().getContent()), Integer
						.parseInt(annotation_object.getBndbox().getYmin()
								.getContent()));
				points.add(point0);

				Point point1 = new Point(Integer.parseInt(annotation_object
						.getBndbox().getXmax().getContent()), Integer
						.parseInt(annotation_object.getBndbox().getYmin()
								.getContent()));
				points.add(point1);

				Point point2 = new Point(Integer.parseInt(annotation_object
						.getBndbox().getXmax().getContent()), Integer
						.parseInt(annotation_object.getBndbox().getYmax()
								.getContent()));
				points.add(point2);

				Point point3 = new Point(Integer.parseInt(annotation_object
						.getBndbox().getXmin().getContent()), Integer
						.parseInt(annotation_object.getBndbox().getYmax()
								.getContent()));
				points.add(point3);

				SVGPolygon polygon = new SVGPolygon(points);
				polygon.setGroupId("polygon#" + i++);
				svgpolygons_list.add(polygon);
			}

			jmagine.pushSVGSymbol(svgpolygons_list, "polygons");

			p0.add(jmagine, BorderLayout.CENTER);
			f.setSize(512, 512);
			f.setLocationRelativeTo(null);
			f.setVisible(true);
			jmagine.updateScaleToComponentSizeIfLarger();
		}
	}

	@Override
	public void itemDoubleClicked(GEvent e) {
		System.out.println("item doubleclicked ["
				+ e.getSelectedItem().getLocalFilepath() + "]");

		//		SystemCommandThread sct = new SystemCommandThread("display \""
		//				+ e.getSelectedItem().getLocalFilepath() + "\"");
		//		sct.start();

		JmagineThread jmagine_thread = new JmagineThread(e.getSelectedItem());
		jmagine_thread.start();
	}

	@Override
	public void selectionChanged(GEvent e) {
		System.out.println("changing selection for ["
				+ e.getSelectedItem().getLocalFilepath() + "]");
	}

}

