package speiger.src.coreengine.rendering.gui.layout.constraints;

import java.util.function.BooleanSupplier;

import speiger.src.coreengine.rendering.gui.components.base.GuiComponent;
import speiger.src.coreengine.rendering.gui.layout.constraints.IConstraint.ISimpleConstraint;
import speiger.src.coreengine.rendering.guiOld.helper.box.IGuiBox;

public class Constraints {
	public static record Pixels(float value, boolean inverted) implements ISimpleConstraint {
		
		public static Pixels of(float value) { return new Pixels(value, false); }
		public static Pixels inverted(float value) { return new Pixels(value, true); }
		
		@Override
		public void apply(IGuiBox owner, IGuiBox parent, Target target) {
			target.set(owner, inverted ? target.asArea().get(parent) - value : value);
		}
	}
	
	public static record Parent(float padding, boolean inv) implements ISimpleConstraint {
		
		public static Parent of() { return new Parent(0F, false); }
		public static Parent of(float padding) { return new Parent(padding, false); }
		public static Parent inverted() { return new Parent(0F, true); }
		public static Parent inverted(float padding) { return new Parent(padding, true); }
		
		@Override
		public void apply(IGuiBox owner, IGuiBox parent, Target target) {
			if(inv) target.set(owner, target.isPosition() ? target.asArea().get(parent) - padding : padding * 2);
			else target.set(owner, target.isPosition() ? padding : target.get(parent) - padding * 2);
		}
	}
	
	public static record Relative(float value, float padding) implements ISimpleConstraint {
		
		public static Relative of(float value) { return new Relative(value, 0F); }
		public static Relative of(float value, float padding) { return new Relative(value, padding); }
		
		@Override
		public void apply(IGuiBox owner, IGuiBox parent, Target target) {
			target.set(owner, value * (target.asArea().get(parent) - padding));
		}
	}
	
	public static record Center() implements ISimpleConstraint {
		private static final Center INSTANCE = new Center();
		
		public static Center of() { return INSTANCE; }
		
		@Override
		public void apply(IGuiBox owner, IGuiBox parent, Target target) {
			Target bounds = target.asArea();
			target.set(owner, (bounds.get(parent) * 0.5F) - (bounds.get(owner) * 0.5F));
		}
	}
	
	public static record Conditional(BooleanSupplier supplier, IConstraint onTrue, IConstraint onFalse) implements IConstraint {
		public static Conditional of(BooleanSupplier supplier, IConstraint onTrue, IConstraint onFalse) { return new Conditional(supplier, onTrue, onFalse); }
		
		@Override
		public void apply(GuiComponent owner, GuiComponent parent, Target target) {
			(supplier.getAsBoolean() ? onTrue : onFalse).apply(owner, parent, target);
		}
	}
}
