/* $Id: CloseUpViewer.java 1065 2016-01-29 06:30:17Z minao $ */
package smart_gs.smleditor.swingui;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Point2D;
import java.awt.image.*;
import javax.swing.*;

import reasoning_web.logical.inter_face.NoWayToShowException;
import smart_gs.drawing_tool.ImageLabel;
import smart_gs.drawing_tool.action.DeleteRegionAction;
import smart_gs.logical.Region;
import smart_gs.logical.URICreator;
import smart_gs.swingui.AbstractGSWindow;
import smart_gs.swingui.SetIcon;
import smart_gs.swingui.WorkspaceWindow;
import smart_gs.util.ImageCropper;

public class CloseUpViewer  {

	int magnification = 20;

	JFrame frame = new JFrame();
	JPanel current_panel = new JPanel();
	Image original_image;
	JPanel panel_100 = new JPanel();
	JPanel panel_150 = new JPanel();
	JPanel panel_200 = new JPanel();
	JPanel panel_300 = new JPanel();
	JPanel panel_500 = new JPanel();
	private Point loc = null;
	private MouseEvent start;
	Window window;
	private Region region;
	private static int lastScale = 300;
	JScrollPane sPane = new JScrollPane();

	public CloseUpViewer (Image arg, final Region region, Point point) {
		this.region = region;
		frame.setAlwaysOnTop(true);
		frame.setResizable(true);
		SetIcon.setIcon(frame);
		original_image = arg;
		window = frame;
		loc = window.getLocation();

		try {
			panel_100 = createElementViewPanelMagnified(100);
		} catch (NoWayToShowException e){
			e.printStackTrace();
		}
		try {
			panel_150 = createElementViewPanelMagnified(150);
		} catch (NoWayToShowException e){
			e.printStackTrace();
		}
		try {
			panel_200 = createElementViewPanelMagnified(200);
		} catch (NoWayToShowException e){
			e.printStackTrace();
		}
		try {
			panel_300 = createElementViewPanelMagnified(300);
		} catch (NoWayToShowException e){
			e.printStackTrace();
		}
		try {
			panel_500 = createElementViewPanelMagnified(500);
		} catch (NoWayToShowException e){
			e.printStackTrace();
		}
		
		if (lastScale == 100) {
			current_panel = panel_100;
		} else if (lastScale == 150) {
			current_panel = panel_150;
		} else if (lastScale == 200) {
			current_panel = panel_200;
		} else if (lastScale == 300) {
			current_panel = panel_300;
		} else {
			current_panel = panel_500;
		}
		sPane.setViewportView(current_panel);
		frame.getContentPane().add(sPane, BorderLayout.CENTER);
		frame.pack();
//		PointerInfo pointerInfo = MouseInfo.getPointerInfo();
//		Point p = pointerInfo.getLocation();
//		frame.setLocation(p.x-60,p.y-15);
		if (point == null) {
			frame.setLocationRelativeTo(null);
		} else {
			frame.setLocation(point);
		}
		frame.setVisible(true);
	}



	public JPanel createElementViewPanelMagnified(int percent) throws NoWayToShowException {
		Image resulted_image;
		int width = original_image.getWidth(null);
		resulted_image = original_image.getScaledInstance((int)(width*(percent/100.00)*0.8),-1,Image.SCALE_SMOOTH);
		JLabel label = new JLabel();
		ImageIcon icon = new ImageIcon(resulted_image);
		JPanel panel = new JPanel();
		panel.setLayout(new BorderLayout());
		label.setIcon(icon);
		panel.add(label,BorderLayout.CENTER);
		panel.addMouseMotionListener(new TheListener());
		panel.addMouseListener(new TheListener());
		return panel;
	}

	class TheListener implements MouseMotionListener, MouseListener{

		public void mouseMoved(MouseEvent me){}
		public void mouseDragged(MouseEvent me){
			loc = window.getLocation();
			if (start == null) start = me;
			int x = loc.x - start.getX() + me.getX();
			int y = loc.y - start.getY() + me.getY();
			window.setLocation(x, y);
		}
		@Override
		public void mouseClicked(MouseEvent e) {
			JPopupMenu menu = new JPopupMenu();
			JMenuItem title = new JMenuItem("Magnify");
			menu.add(title);
			menu.addSeparator();
			JMenuItem onehundred = new JMenuItem("100%");
			menu.add(onehundred);
			JMenuItem onehundredfifty = new JMenuItem("150%");
			menu.add(onehundredfifty);
			JMenuItem twohundred = new JMenuItem("200%");
			menu.add(twohundred);
			JMenuItem threehundred = new JMenuItem("300%");
			menu.add(threehundred);
			JMenuItem fivehundred = new JMenuItem("500%");
			menu.add(fivehundred);
			JMenuItem close = new JMenuItem("Close");
			menu.add(close);


			onehundred.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e) {
					lastScale = 100;
					Point point = frame.getLocation();
					new CloseUpViewer(original_image, region, point);
					frame.dispose();
				}
			});

			onehundredfifty.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e) {
					lastScale = 150;
					Point point = frame.getLocation();
					new CloseUpViewer(original_image, region, point);
					frame.dispose();
				}
			});

			twohundred.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e) {
					lastScale = 200;
					Point point = frame.getLocation();
					new CloseUpViewer(original_image, region, point);
					frame.dispose();
				}
			});

			threehundred.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e) {
					lastScale = 300;
					Point point = frame.getLocation();
					new CloseUpViewer(original_image, region, point);
					frame.dispose();
				}
			});
			
			fivehundred.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e) {
					lastScale = 500;
					Point point = frame.getLocation();
					new CloseUpViewer(original_image, region, point);
					frame.dispose();
				}
			});

			close.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e) {
					frame.dispose();
				}
			});

			menu.show(e.getComponent(), e.getX(), e.getY());
		}

		@Override
		public void mouseEntered(MouseEvent arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public void mouseExited(MouseEvent arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public void mousePressed(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseReleased(MouseEvent arg0) {
			// TODO Auto-generated method stub

		}
	}
}

