package com.gh.gamecenter.xapk.data

import com.google.gson.Gson
import com.google.gson.annotations.SerializedName
import java.io.InputStream

data class XApkManifest(
    @SerializedName("package_name")
    val packageName: String,
    @SerializedName("total_size")
    val totalSize: Long,
    val expansions: List<Expansion>? = null,
    @SerializedName("split_apks")
    val splitApks: List<Apk> = emptyList(),
) {

    data class Expansion(
        val file: String,
        @SerializedName("install_location")
        val installLocation: String,
        @SerializedName("install_path")
        var installPath: String
    )

    data class Apk(
        val file: String,
        val id: String
    )

    val isMultiApks: Boolean get() = splitApks.size > 1

    companion object {
        fun fromInputStream(
            inputStream: InputStream
        ): XApkManifest = inputStream
            .use {
                it.bufferedReader(Charsets.UTF_8)
                .readText()
                .let { json ->
                    Gson().fromJson(json, XApkManifest::class.java)
                }
            }

    }
}