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

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParseException;
import eu.jonahbauer.wizard.common.messages.observer.ObserverMessage;
import eu.jonahbauer.wizard.common.util.SealedClassTypeAdapterFactory;
import lombok.EqualsAndHashCode;

@EqualsAndHashCode
public abstract sealed class ServerMessage permits AckMessage, GameMessage, KickVotedMessage, KickedMessage, NackMessage, PlayerLeftMessage, PlayerModifiedMessage, SessionJoinedMessage, SessionListMessage, SessionModifiedMessage, SessionRemovedMessage, StartingGameMessage {
    private static final Gson GSON = new GsonBuilder()
            .registerTypeAdapterFactory(SealedClassTypeAdapterFactory.of(ServerMessage.class, "Message"))
            .registerTypeAdapterFactory(SealedClassTypeAdapterFactory.of(ObserverMessage.class, "Message"))
            .create();

    public static ServerMessage parse(String json) throws JsonParseException {
        return GSON.fromJson(json, ServerMessage.class);
    }

    @Override
    public String toString() {
        return GSON.toJson(this, ServerMessage.class);
    }
}
