/* $Id: RectangleMode.java 692 2013-04-03 14:20:43Z ohura $ */
package smart_gs.drawing_tool.drawing_mode;

import java.awt.Component;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;


import smart_gs.debugprint.Debugprint;
import smart_gs.drawing_tool.ImageLabel;
import smart_gs.drawing_tool.SpreadCanvas;
//import smart_gs.drawing_tool.state.DefaultState;
import smart_gs.drawing_tool.state.DefaultState;
import smart_gs.drawing_tool.state.State;
import smart_gs.drawing_tool.state.UniSelectedState;
import smart_gs.drawing_tool.view.RectangleView;
import smart_gs.drawing_tool.view.View;
import smart_gs.logical.Region;
import smart_gs.logical.region.RectangleRegion;
import smart_gs.reasoning_web.swingui.GlobalViewPanel;
import smart_gs.reasoning_web.swingui.ReasoningWebFrame;
import smart_gs.smleditor.swingui.GSEditor;
import smart_gs.swingui.GSMouseEvent;
import smart_gs.swingui.WorkspaceWindow;
import smart_gs.swingui.toolbar.GSEditorToolBar;
import smart_gs.swingui.toolbar.ImageToolBar;
import smart_gs.util.ImageToolBarSetModeByMouseEvent;

public class RectangleMode extends DrawingModeAdapter{
	private Component startcomponent = null;
	private boolean out = false;
	private static Mode singleton = new RectangleMode();
	
	private Point2D start;
	private Point2D end;
	
	private boolean temporary = false;


	public static Mode getInstance(){
		((RectangleMode)singleton).reset();
		return singleton;
	}
	
	private void reset() {
		startcomponent = null;
		out = false;
		start = null;
		end = null;		
	}
	@Override
	public boolean isTemporary() {
		return temporary;
	}
	@Override
	public void setTemporary(boolean sw) {
		temporary = sw;
	}
	@Override
	public void mouseEntered(GSMouseEvent e, SpreadCanvas canvas) {
		if (startcomponent == null) startcomponent = e.getComponent();
		if (e.getComponent() == startcomponent) {
			out = false;
		} else {
			out = true;
		}
		setCursor(canvas);
	}
	
	@Override
	public void mousePressed(GSMouseEvent e, SpreadCanvas canvas) {
		if (startcomponent == null) { 
			startcomponent = e.getComponent();
		}
		super.mousePressed(e, canvas);
		this.start = e.getPoint();
		setCursor(canvas);
	}
	@Override
	public void mouseDragged(GSMouseEvent e,SpreadCanvas canvas) {
		if(e.getComponent() != startcomponent){
			return ;
		}
		super.mouseDragged(e,canvas);
		this.end = e.getPoint();
		setCursor(canvas);
	}
	
	@Override
	public void mouseMoved(GSMouseEvent e,SpreadCanvas canvas) {
		if (startcomponent == null) startcomponent = e.getComponent();
		if (e.getComponent() == startcomponent) {
			out = false;
		} else {
			out = true;
			return;
		}
		if(startcomponent != null && (e.getComponent() != startcomponent)){
			return ;
		}
		setCursor(canvas);
	}
	
	@Override
	public void mouseReleased(GSMouseEvent e, SpreadCanvas canvas) {
		super.mouseReleased(e, canvas);
		if(start == null || end == null){
			return ;
		}

		if((end.getX() - start.getX()) > 10 &&  end.getY() - start.getY() > 10){
			Region reg = new RectangleRegion(canvas.getSpread(),adjustRect(start.getX(), start.getY()));
			reg.setTemporary(this.isTemporary());
			canvas.addRegion(reg);
			
			// 2013/03/23 kukita
			// The created region will be automatically selected.
			UniSelectedState.getInstance().changeSelectedRegions(reg);
		}
		
		this.start = null;
		this.end = null;
		/*
		 * カーソルをrectangleから元の状態に戻している
		 * 
		 */
		DefaultState.getInstance().setMode(DefaultDrawingMode.getInstance());
		new ImageToolBarSetModeByMouseEvent().setMode(e,ImageToolBar.modeNone);
		setCursor(canvas);
	}
	@Override
	public void paint(Graphics g,SpreadCanvas canvas) {
		if (out) return;
		super.paint(g,canvas);
		if(this.start ==null || this.end == null){
			return;
		}
		double ratio = canvas.getImageLabel().getRatio();
		double gapX = canvas.getImageLabel().getGapWidth();
		double gapY = canvas.getImageLabel().getGapHeight();
		View view = new RectangleView(adjustRect(start.getX(), start.getY())).enlargedView(ratio, gapX, gapY);
		
		view.draw((Graphics2D)g);
	}
	
	private Rectangle2D adjustRect(double sx, double sy){
				double width, height;
		double startX, startY;
		
		if(start.getX() > end.getX()){
			startX = end.getX();
			width = start.getX() - end.getX();
		}
		else{
			startX = start.getX();
			width = end.getX() - start.getX();
		}
		if(start.getY() > end.getY()){
			startY = end.getY();
			height = start.getY() - end.getY();
		}
		else{
			startY = start.getY();
			height= end.getY() - start.getY();
		}
		return new Rectangle2D.Double(startX,startY,width,height);
	}
}
