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.Amazon.AmazonCountryStats;

import org.springframework.stereotype.Service;

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

import static io.delphiplatform.api.v3.bigtable.reader.BigtableColumns.DSP;

@Service
public class AmazonTrackStreamReader extends BigtableEntityReader<AmazonTrackStream> {

    @Override
    public AmazonTrackStream readRow(Row row) {
        AmazonTrackStream amazonTrackStream = new AmazonTrackStream();
        amazonTrackStream.setFullDsp(readString(row, meta, DSP));
        return readTrackStream(amazonTrackStream, row, this::parseStats);
    }

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

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


}
