package eu.jonahbauer.wizard.common.messages.server;

import com.fasterxml.jackson.databind.ObjectMapper;
import eu.jonahbauer.wizard.common.messages.ParseException;
import eu.jonahbauer.wizard.common.messages.observer.ObserverMessage;
import eu.jonahbauer.wizard.common.messages.util.SerializationUtil;
import lombok.EqualsAndHashCode;
import lombok.SneakyThrows;

@EqualsAndHashCode
public abstract sealed class ServerMessage permits AckMessage, GameMessage, NackMessage, PlayerLeftMessage, PlayerModifiedMessage, SessionJoinedMessage, SessionListMessage, SessionModifiedMessage, SessionRemovedMessage, StartingGameMessage {
    private static final ObjectMapper MAPPER = SerializationUtil.newObjectMapper(ServerMessage.class, ObserverMessage.class);

    public static ServerMessage parse(String json) throws ParseException {
        return SerializationUtil.parse(json, MAPPER, ServerMessage.class);
    }

    @Override
    @SneakyThrows
    public String toString() {
        return MAPPER.writeValueAsString(this);
    }
}
