package org.geotools.demo;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import javax.swing.filechooser.FileFilter;

import org.geotools.factory.CommonFactoryFinder;
import org.geotools.gce.image.WorldImageReader;
import org.geotools.geometry.jts.ReferencedEnvelope;
import org.geotools.gui.swing.JMapPane;
import org.geotools.map.DefaultMapContext;
import org.geotools.map.MapContext;
import org.geotools.renderer.lite.StreamingRenderer;
import org.geotools.styling.FeatureTypeStyle;
import org.geotools.styling.RasterSymbolizer;
import org.geotools.styling.Rule;
import org.geotools.styling.Style;
import org.geotools.styling.StyleFactory;
import org.geotools.styling.Symbolizer;

/**
 * This example opens up a world plus image file.
 * 
 * @author Jody Garnett
 */
public class ImageLab2 {
	
	/**
	 * Prompt the user for a file and open up ImageLab.
	 * @param args filename of image
	 */
	public static void main( String[] args ) throws Exception {
		File file = getImageFile( args );
		
		WorldImageReader reader = new WorldImageReader( file );
		MapContext map = new DefaultMapContext(reader.getCrs());
		
		// set up style
		Style style = createStyle();
		
		map.addLayer(reader.read(null), style);
		map.setAreaOfInterest(new ReferencedEnvelope(reader.getOriginalEnvelope()));
		
		showMap(map);
	}

	private static Style createStyle() {
		StyleFactory styleFactory = CommonFactoryFinder.getStyleFactory(null);
		RasterSymbolizer symbolizer = styleFactory.createRasterSymbolizer();
		Rule rule = styleFactory.createRule();
		rule.setSymbolizers(new Symbolizer[] {symbolizer});
		FeatureTypeStyle fts = styleFactory.createFeatureTypeStyle();
		fts.setRules(new Rule[] {rule});
		Style style = styleFactory.createStyle();
		style.setFeatureTypeStyles(new FeatureTypeStyle[] {fts});
		return style;
	}
	private static void showMap(MapContext map) throws IOException {
	        final JMapPane mapPane = new JMapPane(new StreamingRenderer(), map);
	        mapPane.setMapArea(map.getLayerBounds());
	        JFrame frame = new JFrame("ImageLab2");

	        frame.setLayout(new BorderLayout());
	        frame.add(mapPane, BorderLayout.CENTER);
	        JPanel buttons = new JPanel();
	        JButton zoomInButton = new JButton("Zoom In");
	        zoomInButton.addActionListener(new ActionListener() {
	            public void actionPerformed(ActionEvent e) {
	                mapPane.setState(JMapPane.ZoomIn);
	            }
	        });
	        buttons.add(zoomInButton);

	        JButton zoomOutButton = new JButton("Zoom Out");
	        zoomOutButton.addActionListener(new ActionListener() {
	            public void actionPerformed(ActionEvent e) {
	                mapPane.setState(JMapPane.ZoomOut);
	            }
	        });
	        buttons.add(zoomOutButton);

	        JButton pamButton = new JButton("Move");
	        pamButton.addActionListener(new ActionListener() {
	            public void actionPerformed(ActionEvent e) {
	                mapPane.setState(JMapPane.Pan);
	            }
	        });
	        buttons.add(pamButton);

	        frame.add(buttons, BorderLayout.NORTH);

	        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
	        frame.setSize(600, 400);
	        frame.setVisible(true);
	    }
	private static File getImageFile(String[] args) throws FileNotFoundException {
		File file;
		if (args.length == 0) {
			JFileChooser chooser = new JFileChooser();
			chooser.setDialogTitle("Open Image file");
			chooser.setFileFilter(new FileFilter() {
				public boolean accept(File f) {
					return f.isDirectory() || f.getPath().endsWith("jpg")
							|| f.getPath().endsWith("JPG");
				}

				public String getDescription() {
					return "JPEG";
				}
			});
			int returnVal = chooser.showOpenDialog(null);

			if (returnVal != JFileChooser.APPROVE_OPTION) {
				System.exit(0);
			}
			file = chooser.getSelectedFile();

			System.out.println("Opening Image file: " + file.getName());
		} else {
			file = new File(args[0]);
		}
		if (!file.exists()) {
			throw new FileNotFoundException(file.getAbsolutePath());
		}
		return file;
	}
}
