////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////

package pp.battleship.game.server;

import pp.battleship.BattleshipConfig;
import pp.battleship.message.server.ServerMessage;
import pp.battleship.model.Battleship;
import pp.battleship.model.Battleship.Status;
import pp.battleship.model.IntPoint;
import pp.battleship.model.Rotation;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;

import static org.junit.Assert.assertEquals;

public abstract class AbstractServerGameTest {
    static final BattleshipConfig CONFIG = new BattleshipConfig();

    static {
        // Configure logging
        final Logger rootLogger = LogManager.getLogManager().getLogger("");
        rootLogger.setLevel(Level.WARNING);
        for (Handler h : rootLogger.getHandlers()) {
            h.setLevel(Level.WARNING);
        }
    }

    record ServerMessageWrapper(int id, ServerMessage msg) {}

    final Deque<ServerMessageWrapper> messages = new ArrayDeque<>();
    final ServerSender serverSender = (id, msg) -> messages.offer(new ServerMessageWrapper(id, msg));
    final ServerGameLogic serverLogic = new ServerGameLogic(serverSender, CONFIG);

    static IntPoint p(int x, int y) {
        return new IntPoint(x, y);
    }

    void checkShip(Battleship ship, int length, int x, int y, Rotation rot, Status status) {
        assertEquals(length, ship.getLength());
        assertEquals(x, ship.getX());
        assertEquals(y, ship.getY());
        assertEquals(rot, ship.getRot());
        assertEquals(status, ship.getStatus());
    }
}
