package smart_gs.drawing_tool.state;

import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JPopupMenu;

import smart_gs.GSConstants;
import smart_gs.debugprint.Debugprint;
import smart_gs.drawing_tool.DrawingModeSelectionPopupMenu;
import smart_gs.drawing_tool.ImageLabel;
import smart_gs.drawing_tool.LineSegEditorImageLabel;
import smart_gs.drawing_tool.LineSegEditorCanvas;
import smart_gs.drawing_tool.RightClickMenu;
import smart_gs.drawing_tool.SpreadCanvas;
import smart_gs.drawing_tool.drawing_mode.AnchorMode;
import smart_gs.drawing_tool.drawing_mode.BookmarkMode;
import smart_gs.drawing_tool.drawing_mode.DefaultDrawingMode;
import smart_gs.drawing_tool.drawing_mode.LineSegEditorDrawMode;
import smart_gs.drawing_tool.drawing_mode.EraserMode;
import smart_gs.drawing_tool.drawing_mode.LineSegEditorMode;
import smart_gs.drawing_tool.drawing_mode.MarkerMode;
import smart_gs.drawing_tool.drawing_mode.MemoPadMode;
import smart_gs.drawing_tool.drawing_mode.Mode;
import smart_gs.drawing_tool.drawing_mode.PolygonMode;
import smart_gs.drawing_tool.drawing_mode.RectangleMode;
import smart_gs.logical.Preference;
import smart_gs.reasoning_web.swingui.GlobalViewPanel;
import smart_gs.swingui.GSMouseEvent;
import smart_gs.swingui.WorkspaceWindow;
import smart_gs.swingui.toolbar.ImageToolBar;

public class LineSegEditorDefaultState implements LineSegEditorState{
	
	private LineSegEditorMode mode;
	private Cursor cursor;
	private LineSegEditorCanvas canvas;
	
	public LineSegEditorDefaultState(){
		this.mode = LineSegEditorDrawMode.getInstance();
		this.setCursor(Cursor.getDefaultCursor());
	}
	
	public LineSegEditorDefaultState(LineSegEditorCanvas canvas) {
		this.canvas = canvas;
		this.mode = LineSegEditorDrawMode.getInstance();
		this.setCursor(Cursor.getDefaultCursor());
	}

	public void setMode(LineSegEditorMode mode){
		ImageLabel imageLabel = WorkspaceWindow.getInstance().getSpreadCanvas().getImageLabel();
		this.mode.cancel();
		this.mode = mode;
		imageLabel.setCursor(this.cursor);
	}

	public void mouseClicked(GSMouseEvent e, LineSegEditorCanvas canvas) {
		if (e.getButton() == MouseEvent.BUTTON1) {
			if (WorkspaceWindow.getInstance().onlyOneOfAltCtrlShiftIsPressed()) {
				if(WorkspaceWindow.getInstance().isAlt()){
					/* alternative for right button click */
					rightButtonClickAction(e);					
				} else if(WorkspaceWindow.getInstance().isCtrl()){
					setMode(LineSegEditorDrawMode.getInstance());
//					TODO Implement this!!! 2011.11.7 shayashi					
//					LineSegEditorToolBar.setMode(ImageToolBar.modeNone]);
				} 
			} else {
				this.mode.mouseClicked(e,canvas);
			}
		} else if (e.getButton() == MouseEvent.BUTTON3) {
			rightButtonClickAction (e);
		} else {
			this.mode.mouseClicked(e,canvas);
		}
	}

	private void rightButtonClickAction (GSMouseEvent e) {
//		JPopupMenu menu = new LineSegEditorRightClickPopupMenu(canvas.getSpread());;
//		menu.show(e.getComponent(),e.getOriginalPoint().x,e.getOriginalPoint().y);
//		GlobalViewPanel.getInstance().repaint();
	}
		
	public void mouseDragged(GSMouseEvent e, LineSegEditorCanvas canvas) {
		this.mode.mouseDragged(e,canvas);
	}

	public void mouseEntered(GSMouseEvent e, LineSegEditorCanvas canvas) {
		this.mode.mouseEntered(e,canvas);
	}

	public void mouseExited(GSMouseEvent e, LineSegEditorCanvas canvas) {
		this.mode.mouseExited(e,canvas);
	}

	public void mouseMoved(GSMouseEvent e, LineSegEditorCanvas canvas) {
		mode.mouseMoved(e,canvas);		
	}

	public void mousePressed(GSMouseEvent e, LineSegEditorCanvas canvas) {
		mode.mousePressed(e, canvas);
	}

	public void mouseReleased(GSMouseEvent e, LineSegEditorCanvas canvas) {
		mode.mouseReleased(e, canvas);
	}

	public void paint(Graphics g, LineSegEditorCanvas canvas) {
		mode.paint(g,canvas);
	}

	public LineSegEditorMode getMode() {
		return this.mode;
	}

	@Override
	public int getType() {
		return State.DEFAULT;
	}
	
	public void changeCursorTo(Mode mode) {
		Point hotSpot = new Point(0, 0);
		Toolkit kit = Toolkit.getDefaultToolkit();
		if (mode instanceof DefaultDrawingMode) {
			this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
		} else if (mode instanceof RectangleMode) {
			this.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
		} else if (mode instanceof PolygonMode) {
			this.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
		} else if (mode instanceof AnchorMode) {
			Image img = new ImageIcon(GSConstants.ICON_PATH+"anchor_cursor.png").getImage();
			String name = "anchor_cursor";
			this.setCursor(kit.createCustomCursor(img, hotSpot, name));
		} else if (mode instanceof BookmarkMode) {
			ImageIcon imageIcon = new ImageIcon(GSConstants.ICON_PATH+ Preference.getInstance().getDefaultBookmarkPath());
			Image img = imageIcon.getImage();
			hotSpot = new Point(10,30);
			String name = "bookmark_cursor";
			this.setCursor(kit.createCustomCursor(img, hotSpot, name));
		} else if (mode instanceof EraserMode) {
			Image img = new ImageIcon(GSConstants.ICON_PATH+"eraser.png").getImage();
			String name = "eraser_cursor";
			this.setCursor(kit.createCustomCursor(img, new Point(5,5), name));
		} else if (mode instanceof MemoPadMode) {
			Image img = new ImageIcon(GSConstants.ICON_PATH+"memopad_cursor.png").getImage();
			String name = "memopad_cursor";
			this.setCursor(kit.createCustomCursor(img, hotSpot, name));
		} else if (mode instanceof MarkerMode) {
			Image img = new ImageIcon(GSConstants.ICON_PATH+"marker_cursor.png").getImage();
			String name = "marker_cursor";
			this.setCursor(kit.createCustomCursor(img, hotSpot, name));
		} else {
			this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
		}
		
	}

	public void setCursor(Cursor cursor) {
		this.cursor = cursor;
	}

	public Cursor getCursor() {
		return cursor;
	}
}
