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

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

@EqualsAndHashCode
public abstract sealed class ClientMessage permits CreateSessionMessage, InteractionMessage, JoinSessionMessage, LeaveSessionMessage, ReadyMessage, RejoinMessage {
    private static final ObjectMapper MAPPER = SerializationUtil.newObjectMapper(ClientMessage.class, PlayerMessage.class);

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

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