package de.siphalor.was.game;

import org.jetbrains.annotations.NotNull;

/**
 * The class holding the stored products.
 */
public class Storage {
	/**
	 * The storage slots
	 */
	StorageSlot[][] slots;

	/**
	 * Constructs a new storage with the given lengths.
	 * @param width The width
	 * @param height The height
	 * @param depth The depth of the {@link StorageSlot}s
	 * @throws IllegalArgumentException if any of the lengths is smaller or equal to zero
	 */
	public Storage(int width, int height, int depth) {
		if (width <= 0 || height <= 0 || depth <= 0) {
			throw new IllegalArgumentException("Storage lengths must be bigger than zero");
		}
		slots = new StorageSlot[height][width];

		for (StorageSlot[] row : slots) {
			for (int i = 0; i < row.length; i++) {
				row[i] = new StorageSlot(depth);
			}
		}
	}

	/**
	 * Gets the storage slot at the specified location. <b>Coordinate must be valid!</b>
	 * @param x The x position (<b>Must be in bounds</b>)
	 * @param y The y position (<b>Must be in bounds</b>
	 * @return The storage slot at the given location
	 */
	@NotNull
	public StorageSlot get(int x, int y) {
		return slots[x][y];
	}

	/**
	 * Gets the width of the storage
	 * @return The width
	 */
	public int getWidth() {
		return slots.length;
	}

	/**
	 * Gets the height of the storage
	 * @return The height
	 */
	public int getHeight() {
		return slots[0].length;
	}

	/**
	 * Clears this storage from all products
	 */
	public void clear() {
		for (StorageSlot[] row : slots) {
			for (StorageSlot slot : row) {
				slot.clear();
			}
		}
	}
}
