package speiger.src.coreengine.rendering.guiOld.helper.box;

import java.util.List;

import speiger.src.collections.objects.lists.ObjectArrayList;
import speiger.src.collections.objects.lists.ObjectList;

public class GuiBox implements IGuiBox
{
	ObjectList<IGuiBox> children = new ObjectArrayList<>();
	IGuiBox parent;
		
	float minX;
	float minY;
	float maxX;
	float maxY;
	
	float width;
	float height;
	float scale = 1F;
	
	float baseX;
	float baseY;
	float baseWidth;
	float baseHeight;
	float baseScale = 1F;
	
	
	protected GuiBox() {}
	
	public GuiBox(float x, float y, float width, float height) {
		set(x, y, width, height);
		onChanged();
	}
	
	public static IGuiBox clone(IGuiBox box) {
		return new GuiBox(box.getMinX(), box.getMinY(), box.getWidth(), box.getHeight());
	}
	
	public static IGuiBox clonePadded(IGuiBox box, float padding) {
		return new GuiBox(box.getMinX() + padding, box.getMinY() + padding, box.getWidth() - padding * 2F, box.getHeight() - padding * 2F);
	}
	
	@Override
	public IGuiBox addChild(IGuiBox box) {
		children.add(box);
		box.setParent(this);
		return this;
	}
	
	@Override
	public IGuiBox removeChild(IGuiBox box) {
		if(children.remove(box)) box.setParent(null);
		return this;
	}
	
	@Override
	public IGuiBox clearChildren() {
		for(int i = 0,m=children.size();i<m;i++) {
			children.get(i).setParent(null);
		}
		children.clear();
		return this;
	}
	
	@Override
	public List<IGuiBox> children() {
		return children.unmodifiable();
	}
	
	@Override
	public IGuiBox onChanged() {
		minX = parent == null ? baseX : parent.getMinX(baseX);
		minY = parent == null ? baseY : parent.getMinY(baseY);
		maxX = parent == null ? baseX + width : parent.getMinX(baseX + width);
		maxY = parent == null ? baseY + height : parent.getMinY(baseY + height);
		scale = parent == null ? baseScale : parent.getScale() * baseScale;
		for(int i = 0,m=children.size();i<m;i++) {
			children.get(i).onChanged();
		}
		return this;
	}
	
	@Override
	public IGuiBox setParent(IGuiBox box) {
		parent = box;
		return this;
	}
	
	@Override
	public IGuiBox getParent() { return parent; }
	@Override
	public float getScale() { return scale; }
	@Override
	public float getBaseX() { return baseX; }
	@Override
	public float getBaseY() { return baseY; }
	@Override
	public float getRelativeX() { return parent != null ? baseX : 0F; }
	@Override
	public float getRelativeY() { return parent != null ? baseY : 0F; }
	@Override
	public float getWidth() { return baseWidth * scale; }
	@Override
	public float getWidth(float extra) { return baseWidth * scale + extra * baseScale; }
	@Override
	public float getHeight() { return baseHeight * scale; }
	@Override
	public float getHeight(float extra) { return baseHeight * scale + extra * baseScale; }
	@Override
	public float getMinX() { return minX; }
	@Override
	public float getMinX(float extra) { return minX + extra * scale; }
	@Override
	public float getMinY() { return minY; } 
	@Override
	public float getMinY(float extra) { return minY + extra * scale; }
	@Override
	public float getMaxX() { return maxX; }
	@Override
	public float getMaxX(float extra) { return maxX + extra * scale; }
	@Override
	public float getMaxY() { return maxY; }
	@Override
	public float getMaxY(float extra) { return maxY + extra * scale; }
	@Override
	public float getCenterX() { return minX + (maxX - minX) * 0.5F; } 
	@Override
	public float getCenterX(float extra) { return minX + (maxX - minX) * 0.5F + extra * scale; }
	@Override
	public float getCenterY() { return minY + (maxY - minY) * 0.5F; }
	@Override
	public float getCenterY(float extra) { return minY + (maxY - minY) * 0.5F + extra * scale; }
	@Override
	public IGuiBox setX(float x) {
		baseX = x;
		return this;
	}
	
	@Override
	public IGuiBox setY(float y) {
		baseY = y;
		return this;
	}
	
	@Override
	public IGuiBox setWidth(float width) {
		baseWidth = width;
		this.width = baseWidth * baseScale;
		return this;
	}
	
	@Override
	public IGuiBox setHeight(float height) {
		baseHeight = height;
		this.height = baseHeight * baseScale;
		return this;
	}
	
	@Override
	public IGuiBox setScale(float scale) {
		baseScale = scale;
		width = baseWidth * baseScale;
		height = baseHeight * baseScale;
		return this;
	}
	
	@Override
	public IGuiBox move(float xOffset, float yOffset) { return setXY(baseX + xOffset, baseY + yOffset); }
	@Override
	public IGuiBox resize(float xGrowth, float yGrowth) { return setBounds(baseWidth + xGrowth, baseHeight + yGrowth); }
	@Override
	public IGuiBox scale(float scale) { return setScale(scale * baseScale); }
	@Override
	public float getBaseScale() { return baseScale; }
	@Override
	public float getBaseWidth() { return baseWidth; }
	@Override
	public float getBaseHeight() { return baseHeight; }
	@Override
	public String toString() { return "GuiBox[minX="+minX+", minY="+minY+", maxX="+maxX+", maxY="+maxY+", width="+width+", height="+height+"]"; }
}
