package de.siphalor.was;

import de.siphalor.was.content.product.Product;
import de.siphalor.was.dummy.DummyProduct;
import de.siphalor.was.game.Storage;
import de.siphalor.was.game.StorageSlot;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class StorageTests {
	static Storage storage;
	static DummyProduct.Type productType;
	static Product simpleProduct;
	static Product deepProduct;

	@BeforeAll
	public static void prepareAll() {
		storage = new Storage(3, 3, 3);
		productType = new DummyProduct.Type("dummy", "one", "two");
		simpleProduct = productType.getProduct(new String[]{"simple", "a"});
		deepProduct = productType.getProduct(new String[]{"deep", "wood"});
	}

	@BeforeEach
	public void prepare() {
		storage.clear();
	}

	@Test
	public void illegalConstructorTest() {
		Assertions.assertThrows(IllegalArgumentException.class, () -> new Storage(0, 1, 2), "Illegal storage constructed");
		Assertions.assertThrows(IllegalArgumentException.class, () -> new Storage(1, 0, 2), "Illegal storage constructed");
		Assertions.assertThrows(IllegalArgumentException.class, () -> new Storage(1, 1, 0), "Illegal storage constructed");
	}

	@Test
	public void clearTest() {
		for (int x = 0; x < storage.getWidth(); x++) {
			for (int y = 0; y < storage.getHeight(); y++) {
				storage.get(x, y).add(productType.getProduct(new String[]{"test", Double.toString(Math.random())}));
			}
		}

		storage.clear();

		for (int x = 0; x < storage.getWidth(); x++) {
			for (int y = 0; y < storage.getHeight(); y++) {
				Assertions.assertNull(storage.get(x, y).front(), "Failed to clear storage");
			}
		}
	}

	@Test
	public void depthTest() {
		StorageSlot slot = storage.get(0, 0);

		Assertions.assertEquals(0, slot.add(simpleProduct));
		Assertions.assertEquals(1, slot.add(simpleProduct));
		Assertions.assertEquals(2, slot.add(simpleProduct));
		Assertions.assertEquals(-1, slot.add(simpleProduct), "Can add more products to storage than allowed");

		slot.clear();
		Assertions.assertEquals(0, slot.add(deepProduct));
		Assertions.assertEquals(-1, slot.add(simpleProduct), "Can add more depth to storage than allowed");

		slot.clear();
		Assertions.assertEquals(0, slot.add(simpleProduct));
		Assertions.assertEquals(-1, slot.add(deepProduct), "Can add more depth to storage than allowed");
	}
}
