package de.renew.gui.fs;

import java.awt.Point;

import CH.ifa.draw.standard.ConnectionHandle;
import CH.ifa.draw.util.ColorMap;
import de.renew.draw.storables.ontology.ConnectionFigure;
import de.renew.draw.storables.ontology.Connector;
import de.renew.draw.storables.ontology.Locator;
import de.renew.draw.ui.ontology.DrawingView;
import de.renew.gui.CPNTextFigure;


/**
 * The handle to associate {@link ConceptFigure}s.
 */
public class AssociationHandle extends ConnectionHandle {
    private int _line;
    private String _name;
    private String _type;
    private ConceptFigure _owner;
    private boolean _isCollection;

    /**
     * Constructor for the association handler.
     *
     * @param owner owner of the handle
     * @param line line associated with the handle
     * @param name name of the handle
     * @param type type associated with the handle
     * @param isCollection handle associated with collection
     * @param locator locator of the handle holding the handles location
     * @param prototype prototype associated with the handle
     */
    public AssociationHandle(
        ConceptFigure owner, int line, String name, String type, boolean isCollection,
        Locator locator, ConnectionFigure prototype)
    {
        super(owner, locator, prototype);
        this._owner = owner;
        this._line = line;
        this._name = name;
        this._type = type;
        this._isCollection = isCollection;
    }

    @Override
    protected ConnectionFigure createConnection() {
        return new AssocConnection();
    }

    /**
     * Connects the figures if the mouse is released over another
     * ConceptFigure;
     * otherwise, the respective figure is created!
     */
    @Override
    public void invokeEnd(int x, int y, int anchorX, int anchorY, DrawingView view) {
        // Does this click aim at the source?
        if (findConnectableFigure(x, y, view.drawing()) != _owner) {
            Connector target = findConnectionTarget(x, y, view.drawing());
            if (target == null) {
                if (_owner.findFigureInside(x, y) == null) {
                    ConceptFigure endFigure = new ConceptFigure();

                    endFigure.moveBy(x, y);
                    endFigure.setText(_type);

                    view.add(endFigure);
                }
            }

            String[] lines = _owner.getLines();
            StringBuffer newText = new StringBuffer();
            for (int i = 0; i < lines.length; i++) {
                if (i != _line) {
                    if (i > 0) {
                        newText.append('\n');
                    }
                    newText.append(lines[i]);
                }
            }
            _owner.setText(newText.toString());

            CPNTextFigure nameFigure = new CPNTextFigure(CPNTextFigure.INSCRIPTION);
            nameFigure.setAttribute("FillColor", ColorMap.BACKGROUND);
            nameFigure.setText(_name);
            nameFigure.setParent((AssocConnection) getConnection());
            view.drawing().add(nameFigure);

            CPNTextFigure starFigure = null;
            if (_isCollection) {
                starFigure = new CPNTextFigure(CPNTextFigure.INSCRIPTION);
                starFigure.setAttribute("FillColor", ColorMap.NONE);
                starFigure.setText("*");
                starFigure.setParent((AssocConnection) getConnection());
                view.drawing().add(starFigure);
                Point p1 = starFigure.center();
                starFigure.moveBy((x - p1.x) / 2, (y - p1.y) / 2);
            }
        }

        super.invokeEnd(x, y, anchorX, anchorY, view);

        view.selectionInvalidateHandles();
    }
}