package speiger.src.coreengine.math.collision2d;

import java.util.Iterator;

import speiger.src.coreengine.math.vector.ints.Vec2i;

public class Plane implements I2DCollision {
	Vec2i min;
	Vec2i max;
	
	public Plane() {
		this(0, 0, 0, 0);
	}
	
	public Plane(int minX, int minY, int maxX, int maxY) {
		this(Vec2i.mutable(minX, minY), Vec2i.mutable(maxX, maxY));
	}
	
	public Plane(Vec2i center, int radius) {
		this(center.sub(radius), center.add(radius));
	}
	
	public Plane(Vec2i min, Vec2i max) {
		this.min = min;
		this.max = max;
	}
	
	public Plane set(Plane other) {
		min.set(other.getMin());
		max.set(other.getMax());
		return this;
	}
	
	public Plane set(int minX, int minY, int maxX, int maxY) {
		min.set(minX, minY);
		max.set(maxX, maxY);
		return this;
	}
	
	@Override
	public boolean isColliding(int x, int y) { return x >= min.x() && y >= min.y() && x < max.x() && y < max.y(); }
	public boolean isIntersecting(Plane plane) { return isIntersecting(plane.getMinX(), plane.getMinY(), plane.getMaxX(), plane.getMaxY()); }
	public boolean isIntersecting(int minX, int minY, int maxX, int maxY) { return min.x() <= maxX && max.x() >= minX && min.y() <= maxY && max.y() >= minY; }
	public Vec2i getMin() { return min; }
	public Vec2i getMax() { return max; }
	public int getWidth() { return max.x() - min.x(); }
	public int getHeight() { return max.y() - min.y(); }
	public int getMinX() { return min.x(); }
	public int getMinY() { return min.y(); }
	public int getMaxX() { return max.x(); }
	public int getMaxY() { return max.y(); }
	
	@Override
	public Iterator<Vec2i> iterator() {
		return new Iterator<Vec2i>() {
			Vec2i iter = Vec2i.mutable(min);
			Vec2i result = Vec2i.mutable();
			
			@Override
			public boolean hasNext() {
				return iter != null;
			}
			
			@Override
			public Vec2i next() {
				result.set(iter);
				iter.add(1, 0);
				if(iter.x() == max.x()) {
					iter.set(min.x(), iter.y() + 1);
					if(iter.y() == max.y()) iter = null;
				}
				return result;
			}
			
		};
	}
	
}
