package com.srbenoit.modeling.cell;

import com.srbenoit.modeling.grid.Grid2D;

/**
 * A wall of fixed elements
 */
public class Wall {

    /** the fixed elements */
    private FixedElement[] fixed;

    /**
     * Constructs a new <code>Wall</code>.
     *
     * @param  startX       the starting X coordinate of the emitter
     * @param  startY       the starting Y coordinate of the emitter
     * @param  endX         the ending X coordinate of the emitter
     * @param  endY         the ending Y coordinate of the emitter
     * @param  maxSep       the maximum separation between elements
     * @param  includeLast  true to include the last element, false not to include it
     */
    public Wall(final double startX, final double startY, final double endX, final double endY,
        final double maxSep, final boolean includeLast) {

        double distX;
        double distY;
        double dist;
        int numSegs;
        double deltaX;
        double deltaY;
        double xPos;
        double yPos;
        int numElements;
        int total;

        distX = endX - startX;
        distY = endY - startY;
        dist = Math.sqrt((distX * distX) + (distY * distY));
        numSegs = (int) ((dist / maxSep) + 0.999);
        deltaX = distX / numSegs;
        deltaY = distY / numSegs;

        numElements = includeLast ? (numSegs + 1) : numSegs;

        this.fixed = new FixedElement[numElements];

        for (int i = 0; i < numElements; i++) {
            xPos = startX + (i * deltaX);
            yPos = startY + (i * deltaY);

            this.fixed[i] = new FixedElement(xPos, yPos);
        }
    }

    /**
     * Gets the number of fixed elements.
     *
     * @return  the number of fixed elements
     */
    public int getNumFixed() {

        return this.fixed.length;
    }

    /**
     * Gets a particular fixed element.
     *
     * @param   index  the index of the fixed element
     * @return  the fixed element
     */
    public FixedElement getFixed(final int index) {

        return this.fixed[index];
    }

    /**
     * Adds the emitter and any active signals to a grid, and stores the grid so emitted signals
     * can be automatically added to the grid and expiring signals can be removed.
     *
     * @param  grid  the grid
     */
    public void addToGrid(final Grid2D grid) {

        for (FixedElement elem : this.fixed) {
            elem.installInGrid(grid);
        }
    }
}
