package at.oefai.aaa.animation;

import org.w3c.dom.svg.SVGPoint;
import org.w3c.dom.svg.SVGSVGElement;

/**
 * SVG anim for moving an object on top of another one.
 * @author Stefan Rank
 */
public class PutObjectOntoObject extends BaseSVGAnim {
    private volatile boolean done = false;
    private SVGPoint dest = null;
    private SVGPoint coords = null;
    private String theObject = null;
    private String theDestObject = null;

    public BaseAnim newInstance() { return new PutObjectOntoObject(); }

    protected void doFirstTime() {
        assert (this.args.size() == 2) : "wrong arguments count for PutObjectOntoObject";
        this.theObject = this.args.get(0).getString();
        this.theDestObject = this.args.get(1).getString();
        // get the destination point
        SVGSVGElement eleDest = getNamedElement(this.theDestObject);
        this.dest = getElementsDocumentPos(eleDest);
        if (this.dest == null) {
            assert false : "could not get objects (" + this.theDestObject + ") document relative position";
            this.done = true;
        } else {
            // get the object out of the agent svg and set it to the same pos:
            SVGSVGElement eleObject = getNamedElement(this.theObject);
            SVGPoint curr = getElementsDocumentPos(eleObject);
            if (curr == null) {
                assert false : "could not get objects (" + this.theObject + ") document relative position";
                this.done = true;
            } else {
                eleObject.getParentNode().removeChild(eleObject);
                adjustHolding(getNamedElement(this.actor));
                setElementsPos(eleObject, curr);
                // add the object to the svg doc
                appendObjectToSVGDoc(eleObject);
                hideElement(this.theObject + SHADOW_POSTFIX);
            }
            // displace dest
            this.coords = getCoordsOfOntoObject(eleObject, eleDest);
            addToSVGPoint(this.dest, this.coords);
        }
    }




    public boolean isFinished() {
        return this.done;
    }

    protected void doIt() {
        // move the object if it is still away from the destination
        SVGSVGElement eleObject = getNamedElement(this.theObject);
        SVGPoint curr = getElementsPos(eleObject);
        if (curr == null) {
            assert false : "could not get objects (" + this.theObject + ") position";
            this.done = true;
        } else {
            SVGPoint nextPos = getNextObjectPos(curr, this.dest);
            if (nextPos == this.dest) {
                this.done = true;
                SVGSVGElement eleBelow = getNamedElement(this.theDestObject);
                // dont check if eleBelow already has a child svg (move would need to check)
                // take out above element from where it is now:
                eleObject.getParentNode().removeChild(eleObject);
                setElementsPos(eleObject, this.coords);
                eleBelow.appendChild(eleObject);
            } else {
                setElementsPos(eleObject, nextPos);
            }
        }
    }

}

