package de.siphalor.was.content.product.dynamic;

import de.siphalor.was.content.product.Product;
import de.siphalor.was.content.product.ProductType;
import de.siphalor.was.util.Util;
import org.jetbrains.annotations.NotNull;

import java.util.Arrays;
import java.util.function.IntPredicate;

public class DynamicProduct implements Product {
	private ProductType<?> type;
	private final String[] properties;
	private int depth = 1;
	@NotNull
	private IntPredicate yPredicate = Util.dummyIntPredicate();

	public DynamicProduct(String[] properties) {
		this.properties = properties;
	}

	@Override
	@NotNull
	public String getPropertySpecifier() {
		return String.join("_", properties);
	}

	@Override
	public String[] getProperties() {
		return properties;
	}

	public void setDepth(int depth) {
		this.depth = depth;
	}

	@Override
	public int getDepth() {
		return depth;
	}

	@Override
	public boolean testY(int y) {
		return yPredicate.test(y);
	}

	// This function exists to resolve the circular references of ProductType <-> Product on creation
	void setType(ProductType<?> type) {
		this.type = type;
	}

	@Override
	@NotNull
	public ProductType<?> getType() {
		return type;
	}

	@Override
	public boolean equals(Product product) {
		return super.equals(product);
	}

	public void setYPredicate(@NotNull IntPredicate yPredicate) {
		this.yPredicate = yPredicate;
	}

	@Override
	public String toString() {
		return "DynamicProduct{" +
				"properties=" + Arrays.toString(properties) +
				", depth=" + depth +
				", yPredicate=" + yPredicate +
				'}';
	}
}
