package cnp.ew.diagram;

import java.awt.*;

import cnp.ew.lightweight.*;

// NEEDS to be totally changed.  A handle should be a lightweight component also? seems like it?  Maybe not...
public class CpHandle
{
    static int basicDimension = 8;

    public CpDiagrammableLc owner; // should not be public
    CpLocator locator;
    public int decoration = BOX;
    //static double scale = 1.0;

    // Hmmm... want a general way for handles to paint, but too much overhead to BE lcs.  Maybe can HAVE an lc. but how to paint?  Stuff in x,y?
    public static final int NONE = 0;
    public static final int DOT = 1;
    public static final int ARROW = 2;
    public static final int DIAMOND = 3;
    public static final int BOX = 4;

    public CpHandle()
    {
    }

    public CpHandle(CpDiagrammableLc newOwner, CpLocator newLocator)
    {
        owner = newOwner;
        locator = newLocator;
    }

	public void paint(Graphics g, boolean firstSelection)
	{
	    int dimension = (int)(basicDimension * owner.getDiagram().getScale());
	    int halfDimension = dimension / 2;

	    if (locator == null) { // Default implicit handle
	        return;
	    }

        Rectangle rect = getBoundingBox();
            // Lifted right from CpDependentLineLc.  Generalize?
        switch (decoration) {
            case DOT : {
        	    if (firstSelection) {
            	    g.drawOval(rect.x, rect.y, rect.width, rect.height);
        		} else {
            	    g.fillOval(rect.x, rect.y, rect.width, rect.height);
    		    }
    		    break;
    		}
            case DIAMOND : {
                Polygon p = new Polygon();
                p.addPoint(rect.x, rect.y + halfDimension);
                p.addPoint(rect.x + halfDimension, rect.y);
                p.addPoint(rect.x + dimension, rect.y + halfDimension);
                p.addPoint(rect.x + halfDimension, rect.y + dimension);
                p.addPoint(rect.x, rect.y + halfDimension);
                if (firstSelection) {
                    g.drawPolygon(p);
                } else {
                    g.fillPolygon(p);
                }
                break;
            }
            case ARROW : {
                Polygon p = new Polygon();
                p.addPoint(rect.x, rect.y);
                p.addPoint(rect.x + dimension, rect.y + halfDimension);
                p.addPoint(rect.x, rect.y + dimension);
                p.addPoint(rect.x, rect.y);
                if (firstSelection) {
                    g.drawPolygon(p);
                } else {
                    g.fillPolygon(p);
                }
                break;
            }
            case BOX : {
        	    if (firstSelection) {
            	    g.drawRect(rect.x, rect.y, rect.width, rect.height);
        		} else {
            	    g.fillRect(rect.x, rect.y, rect.width, rect.height);
    		    }
    		    break;
    		}
		}
    }

    public static int getBasicDimension()
    {
        return basicDimension;
    }

	public boolean inside(int x, int y)
	{
	    return getBoundingBox().inside(x, y);
	}

    public Point getPoint()
    {
        return locator.getPoint(owner);
    }

	public Rectangle getBoundingBox()
	{
	    int dimension = (int)(basicDimension * owner.getDiagram().getScale());
	    int halfDimension = dimension / 2;

	    Point topLeft = locator.getPoint(owner);
	    return new Rectangle(topLeft.x - halfDimension, topLeft.y - halfDimension, dimension, dimension);
	}

	// These will be subclassed to do useful stuff
	public void startDrag(int x, int y) {}
	public void drag(int x, int y) {}
	public void endDrag(int x, int y) {}

	// This could be very bogus.  Scale every possible handle?  Class method?  Callback?
	/*public static void scale(double newScale)
	{
	    double factor = newScale / scale;
	    scale = newScale;
        dimension = new Dimension(Math.max(1, (int)(dimension * factor)), Math.max(1, (int)(dimension * factor)));
        halfDimension = new Dimension(dimension / 2, dimension / 2);
	}
	*/
}