#include "engine/data/vector2.hpp"
#include "game/cell_helper.hpp"

#include "mocks/matrix.hpp"

#include <gtest/gtest.h>

#include <list>
#include <memory>

class CellHelperTest : public testing::Test
{
protected:
};

TEST_F(CellHelperTest, IsCellDying)
{
	const auto mock_matrix = MockMatrix<char>();

	const auto rows = 8U;
	const auto columns = 6U;

	const auto position = Vector2({.x = 3, .y = 5});

	EXPECT_CALL(mock_matrix, get_row_cnt()).WillRepeatedly(testing::Return(rows));

	EXPECT_CALL(mock_matrix, get_column_cnt()).WillRepeatedly(testing::Return(columns));

	EXPECT_CALL(mock_matrix, get(position + Vector2::up()))
		.WillOnce(testing::Return('x'))
		.WillOnce(testing::Return(' '))
		.WillOnce(testing::Return(' '));

	EXPECT_CALL(mock_matrix, get(position + Vector2::down()))
		.WillRepeatedly(testing::Return(' '));

	EXPECT_CALL(mock_matrix, get(position + Vector2::left()))
		.WillOnce(testing::Return('x'))
		.WillOnce(testing::Return('x'))
		.WillOnce(testing::Return(' '));

	EXPECT_CALL(mock_matrix, get(position + Vector2::right()))
		.WillOnce(testing::Return('x'))
		.WillOnce(testing::Return(' '))
		.WillOnce(testing::Return(' '));

	EXPECT_CALL(mock_matrix, get(position + Vector2::up() + Vector2::left()))
		.WillRepeatedly(testing::Return(' '));

	EXPECT_CALL(mock_matrix, get(position + Vector2::up() + Vector2::right()))
		.WillRepeatedly(testing::Return('x'));

	EXPECT_CALL(mock_matrix, get(position + Vector2::down() + Vector2::left()))
		.WillRepeatedly(testing::Return(' '));

	EXPECT_CALL(mock_matrix, get(position + Vector2::down() + Vector2::right()))
		.WillRepeatedly(testing::Return(' '));

	auto cell_helper = CellHelper<char>(mock_matrix);

	EXPECT_EQ(cell_helper.is_cell_dying(position), true);

	EXPECT_EQ(cell_helper.is_cell_dying(position), false);

	EXPECT_EQ(cell_helper.is_cell_dying(position), true);
}

TEST_F(CellHelperTest, FindNeighbours)
{
	const auto mock_matrix = MockMatrix<char>();

	const auto rows = 8U;
	const auto columns = 6U;

	const auto position = Vector2({.x = 3, .y = 5});

	EXPECT_CALL(mock_matrix, get_row_cnt()).WillOnce(testing::Return(rows));

	EXPECT_CALL(mock_matrix, get_column_cnt()).WillOnce(testing::Return(columns));

	auto cell_helper = CellHelper<char>(mock_matrix);

	const auto living_neighbour_cells = cell_helper.find_neighbours(position);

	EXPECT_EQ(living_neighbour_cells.size(), 8U);

	EXPECT_THAT(
		living_neighbour_cells,
		testing::ContainerEq(std::list<Vector2>(
			{position + Vector2::up(),
			 position + Vector2::down(),
			 position + Vector2::left(),
			 position + Vector2::right(),
			 position + Vector2::up() + Vector2::left(),
			 position + Vector2::up() + Vector2::right(),
			 position + Vector2::down() + Vector2::left(),
			 position + Vector2::down() + Vector2::right()})));
}