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

import com.google.cloud.bigtable.data.v2.models.Row;
import com.google.protobuf.InvalidProtocolBufferException;
import com.sonymusic.delphi.etl.apps.proto.TiktokSound.TikTokTopSounds;

import org.springframework.stereotype.Service;

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

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

@Service
public class TikTokTopIsrcSoundsReader extends BigtableEntityReader<TikTokTopIsrcSoundsEntity> {

    @Override
    public TikTokTopIsrcSoundsEntity readRow(Row row) {
        TikTokTopIsrcSoundsEntity result = new TikTokTopIsrcSoundsEntity();

        result.setDate(readDate(row, BigtableColumns.DATE));
        result.setIsrc(readString(row, ISRC));

        readBytes(row, BigtableColumns.TOP_SOUNDS).ifPresent(bytes -> {
            try {
                result.setTopSounds(TikTokTopSounds.parseFrom(bytes));
            } catch (InvalidProtocolBufferException e) {
                throw new ParseProtoException("Cannot parse tiktok proto stats.", e);
            }
        });

        return result;
    }

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

}
