package io.delphiplatform.api.integration.util;

import com.google.protobuf.ByteString;
import com.sonymusic.delphi.etl.apps.proto.Amazon.AmazonCountryStats;
import com.sonymusic.delphi.etl.apps.proto.Apple.AppleCountryStats;
import com.sonymusic.delphi.etl.apps.proto.DapdPlaylists;
import com.sonymusic.delphi.etl.apps.proto.Playlists;
import com.sonymusic.delphi.etl.apps.proto.Spotify.SpotifyCountryStats;
import com.sonymusic.delphi.etl.apps.proto.TiktokSound.TikTokTopSounds;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;

public class BigtableUtils {

    public static ByteString toByteString(String val) {
        return ByteString.copyFrom(val, Charset.defaultCharset());
    }

    /**
     * Non-public
     */
    public static ByteString toByteString(Playlists.PlaylistTrackCountryStats stats) {
        if (stats == null) {
            return null;
        }

        try {
            ByteArrayOutputStream os = new ByteArrayOutputStream();

            stats.writeTo(os);

            InputStream is = new ByteArrayInputStream(os.toByteArray());

            return ByteString.readFrom(is);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static ByteString toByteString(DapdPlaylists.DapdPlaylistTrackCountryStats stats) {
        if (stats == null) {
            return null;
        }

        try {
            ByteArrayOutputStream os = new ByteArrayOutputStream();

            stats.writeTo(os);

            InputStream is = new ByteArrayInputStream(os.toByteArray());

            return ByteString.readFrom(is);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static ByteString toByteString(DapdPlaylists.DapdPlaylistTrackPositionsCountryStats stats) {
        if (stats == null) {
            return null;
        }

        try {
            ByteArrayOutputStream os = new ByteArrayOutputStream();

            stats.writeTo(os);

            InputStream is = new ByteArrayInputStream(os.toByteArray());

            return ByteString.readFrom(is);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static ByteString toByteString(SpotifyCountryStats stats) {
        try {
            ByteArrayOutputStream os = new ByteArrayOutputStream();

            stats.writeTo(os);

            InputStream is = new ByteArrayInputStream(os.toByteArray());

            return ByteString.readFrom(is);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static ByteString toByteString(TikTokTopSounds stats) {
        try {
            ByteArrayOutputStream os = new ByteArrayOutputStream();

            stats.writeTo(os);

            InputStream is = new ByteArrayInputStream(os.toByteArray());

            return ByteString.readFrom(is);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static ByteString toByteString(AppleCountryStats stats) {
        try {
            ByteArrayOutputStream os = new ByteArrayOutputStream();

            stats.writeTo(os);

            InputStream is = new ByteArrayInputStream(os.toByteArray());

            return ByteString.readFrom(is);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static ByteString toByteString(AmazonCountryStats stats) {
        try {
            ByteArrayOutputStream os = new ByteArrayOutputStream();

            stats.writeTo(os);

            InputStream is = new ByteArrayInputStream(os.toByteArray());

            return ByteString.readFrom(is);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
