package de.lampware.racing.istats.factory;

import static de.lampware.racing.istats.factory.JsonHelper.getAsInt;

import de.lampware.racing.istats.model.Lap;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

public class LapFactory implements IracingModelFactory<Lap> {

    private static final String SESSION_TIME_KEY = "ses_time";
    private static final String CUSTOMER_ID_KEY = "custid";
    private static final String FLAGS_KEY = "flags";
    private static final String LAP_NUM_KEY = "lap_num";

    @Override
    public Lap create(JsonElement jsonElement) {
        ModelFactoryPreconditions.isJsonObject(jsonElement);
        JsonObject jsonObject = (JsonObject) jsonElement;
        return Lap.builder()
                .withSessionTime(getAsInt(jsonObject, SESSION_TIME_KEY))
                .withCustomerId(getAsInt(jsonObject, CUSTOMER_ID_KEY))
                .withFlags(getAsInt(jsonObject, FLAGS_KEY))
                .withLapNumber(getAsInt(jsonObject, LAP_NUM_KEY))
                .build();
    }
}
