package com.gh.gamecenter.xapk.io

import android.os.Environment
import com.gh.gamecenter.xapk.XApkUnZipTask
import com.gh.gamecenter.xapk.data.XApkManifest
import java.io.File
import java.io.FileOutputStream

class OBBFileOutput : XApkFileOutput<Unit> {

    override fun output(
        writer: XApkUnZipTask.XApkWriter,
        manifest: XApkManifest
    ) {
        val expansions = manifest.expansions

        if (expansions.isNullOrEmpty()) {
            return
        }

        for (expansion in expansions) {
            val file = File(
                Environment.getExternalStorageDirectory(),
                expansion.installPath
            ).apply {
                val parentFile = parentFile
                if (parentFile != null && !parentFile.exists()) {
                    parentFile.mkdirs()
                }
                if (!exists()) createNewFile()
            }
            writer.write(expansion.file, FileOutputStream(file))
        }
    }
}