/*
 * WorldEdit, a Minecraft world manipulation toolkit
 * Copyright (C) sk89q <http://www.sk89q.com>
 * Copyright (C) WorldEdit team and contributors
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

package com.sk89q.worldedit.function.pattern;

import static com.google.common.base.Preconditions.checkNotNull;

import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.math.MutableBlockVector3;
import com.sk89q.worldedit.world.block.BaseBlock;

/**
 * Returns the blocks from {@link Extent}, repeating when out of bounds.
 */
public class RepeatingExtentPattern extends AbstractExtentPattern {

    private final BlockVector3 size;
    private final MutableBlockVector3 mutable;
    private BlockVector3 origin;
    private BlockVector3 offset;

    /**
     * Create a new instance.
     *
     * @param extent the extent
     * @param offset the offset
     */
    public RepeatingExtentPattern(Extent extent, BlockVector3 origin, BlockVector3 offset) {
        super(extent);
        setOrigin(origin);
        setOffset(offset);
        size = extent.getMaximumPoint().subtract(extent.getMinimumPoint()).add(1, 1, 1);
        this.mutable = new MutableBlockVector3();
    }

    /**
     * Get the offset.
     *
     * @return the offset
     */
    public BlockVector3 getOffset() {
        return offset;
    }

    /**
     * Set the offset.
     *
     * @param offset the offset
     */
    public void setOffset(BlockVector3 offset) {
        checkNotNull(offset);
        this.offset = offset;
    }

    /**
     * Get the origin.
     *
     * @return the origin
     */
    public BlockVector3 getOrigin() {
        return origin;
    }

    /**
     * Set the origin.
     *
     * @param origin the origin
     */
    public void setOrigin(BlockVector3 origin) {
        checkNotNull(origin);
        this.origin = origin;
    }

    @Override
    public BaseBlock apply(BlockVector3 position) {
        int x = Math.abs(position.getX() + offset.getX()) % size.getBlockX() + origin.getX();
        int y = Math.abs(position.getY() + offset.getY()) % size.getBlockY() + origin.getY();
        int z = Math.abs(position.getZ() + offset.getZ()) % size.getBlockZ() + origin.getZ();
        return getExtent().getFullBlock(mutable.setComponents(x, y, z));
    }

}
