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

package pp.battleship.game.client;

import org.junit.Before;
import pp.battleship.message.client.ClientMessage;
import pp.battleship.model.Battleship;
import pp.battleship.model.Battleship.Status;
import pp.battleship.model.IntPoint;
import pp.battleship.model.Rotation;
import pp.battleship.notification.GameEventListener;
import pp.battleship.notification.InfoTextEvent;

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 AbstractClientGameTest {
    final Deque<String> infoTexts = new ArrayDeque<>();
    final Deque<ClientMessage> messages = new ArrayDeque<>();
    final ClientGameLogic clientLogic = new ClientGameLogic(messages::offer);

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

    @Before
    public void setup() {
        clientLogic.addListener(new GameEventListener() {
            @Override
            public void receivedEvent(InfoTextEvent event) {
                infoTexts.offer(event.key());
            }
        });
    }

    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());
    }
}
