package de.siphalor.was.content.product;

import org.jetbrains.annotations.NotNull;

import java.util.ResourceBundle;

/**
 * An interface for a generic product
 */
public interface Product {
	/**
	 * The property specifier. Should contain all property values separated by low dashes.
	 * @return The specifier
	 */
	@NotNull
	String getPropertySpecifier();

	/**
	 * Gets all the property values
	 * @return The values as array
	 */
	String[] getProperties();

	/**
	 * Gets this product's depth
	 * @return The depth
	 */
	int getDepth();

	/**
	 * Checks whether the product is allowed to go into the specified y layer.
	 * @param y The y layer to test
	 * @return Whether the product is allowed to go into that y layer
	 */
	boolean testY(int y);

	/**
	 * Gets the products type.
	 * @return Its type
	 */
	@NotNull
	ProductType<?> getType();

	/**
	 * Checks the two products for equality.
	 * @param product A product to check
	 * @return Whether they match
	 */
	boolean equals(Product product);

	/**
	 * Gets the translation key for this product. Usually the same as the products specific.
	 * @return The translation key
	 */
	@NotNull
	default String getTranslationKey() {
		return "products." + getType().getId();
	}

	/**
	 * Gets the name of this product. This will
	 * @param i18n The resources to load the translation from
	 * @return The localized name
	 */
	@NotNull
	default String getName(@NotNull ResourceBundle i18n) {
		return i18n.getString(getTranslationKey());
	}

	/**
	 * Gets the localized description for this product. This includes all the properties their values.
	 * @param i18n The resources to load the translations from
	 * @return The localized description
	 */
	@NotNull
	default String getDescription(@NotNull ResourceBundle i18n) {
		String[] props = getType().getProperties();
		String[] values = getProperties();

		String base = getTranslationKey();

		StringBuilder res = new StringBuilder();
		for (int i = 0; i < props.length; i++) {
			res.append(i18n.getString(base + "." + props[i]));
			res.append(": ");
			res.append(i18n.getString(base + "." + props[i] + "." + values[i]));
			res.append(", ");
		}
		res.delete(res.length() - 2, res.length());

		return res.toString();
	}

	/**
	 * Gets the resource location for this product
	 * @return The resource location
	 */
	@NotNull
	default String getTextureLocation() {
		return "textures/products/" + getType().getId() + "/" + getPropertySpecifier() + ".png";
	}

	/**
	 * Return the placeholder texture for products. Why isn't this static?
	 * @return The resource location
	 */
	@NotNull
	static String getPlaceholderTextureLocation() {
		return "textures/products/package.png";
	}
}
