package de.siphalor.was.dummy;

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

import java.util.Arrays;
import java.util.Random;
import java.util.function.Predicate;

public class DummyProduct implements Product {
	public final Type type;
	public final Predicate<Integer> yPredicate;
	public final int depth;
	public final String[] properties;

	public DummyProduct(Type type, Predicate<Integer> yPredicate, int depth, String[] properties) {
		this.type = type;
		this.yPredicate = yPredicate;
		this.depth = depth;
		this.properties = properties;
	}

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

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

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

	@Override
	public boolean testY(int y) {
		if (yPredicate != null)
			return yPredicate.test(y);
		return true;
	}

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

	@Override
	public boolean equals(Product product) {
		return product.getType() == getType() && product.getPropertySpecifier().equals(getPropertySpecifier());
	}

	public static class Type implements ProductType<DummyProduct> {
		public final String id;
		public final String[] properties;

		public Type(String id, String... properties) {
			this.id = id;
			this.properties = properties;
		}

		@NotNull
		@Override
		public DummyProduct getProduct(@NotNull String[] values) {
			Predicate<Integer> yPredicate = null;
			int depth = 1;
			if (Arrays.binarySearch(values, "deep") >= 0) {
				depth = 3;
			} else if (Arrays.binarySearch(values, "heavy") >= 0) {
				yPredicate = y -> y == 0;
			}
			return new DummyProduct(this, yPredicate, depth, values);
		}

		@Override
		public @NotNull String getId() {
			return id;
		}

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

		@Override
		public @NotNull Product randomProduct(Random random) {
			return null;
		}
	}
}
