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

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

@EqualsAndHashCode
public abstract sealed class PlayerMessage permits ContinueMessage, JuggleMessage, PickTrumpMessage, PlayCardMessage, PredictMessage {
    private static final Gson GSON = new GsonBuilder()
            .registerTypeAdapterFactory(SealedClassTypeAdapterFactory.of(PlayerMessage.class, "Message"))
            .create();

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

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