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.Apple.AppleCountryStats;

import org.springframework.stereotype.Service;

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

@Service
public class AppleTrackStreamReader extends BigtableEntityReader<AppleTrackStream> {

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

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

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

}
