package io.delphiplatform.api.v3.bigtable.reader;

import com.google.cloud.bigtable.data.v2.models.Row;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import com.sonymusic.delphi.etl.apps.proto.Spotify.SpotifyCountryStats;

import org.springframework.stereotype.Service;

import io.delphiplatform.api.v3.bigtable.ParseProtoException;
import io.delphiplatform.api.v3.bigtable.entity.SpotifyTrackStream;

@Service
public class SpotifyTrackStreamReader extends BigtableEntityReader<SpotifyTrackStream> {

    @Override
    public SpotifyTrackStream readRow(Row row) {
        SpotifyTrackStream spotifyTrackStream = new SpotifyTrackStream();
        return readTrackStream(spotifyTrackStream, row, this::parseStats);
    }

    private SpotifyCountryStats parseStats(ByteString bytes) {
        try {
            return SpotifyCountryStats.parseFrom(bytes);
        } catch (InvalidProtocolBufferException e) {
            throw new ParseProtoException("Cannot parse spotify proto stats.", e);
        }
    }

    @Override
    public Class<SpotifyTrackStream> getParsedClass() {
        return SpotifyTrackStream.class;
    }

}
