package com.birdsounds.identify
import android.content.Context
import java.io.File
import java.io.FileOutputStream
import java.io.IOException

class Downloader(mainActivity: MainActivity) {

    private val settings = Settings;
    private var activity: MainActivity = mainActivity;
    private var context: Context = activity.applicationContext;



    fun copyAssetToFolder(assetName: String, destinationPath: String): Boolean {
        try {

            // Get the input stream from the asset
            val assetInputStream = context.assets.open(assetName)

            // Create the destination directory if it doesn't exist
            val destinationFile = File(activity.getDir("",Context.MODE_PRIVATE).absolutePath + "/" + destinationPath)
            destinationFile.parentFile?.mkdirs()

            // Copy the file
            val buffer = ByteArray(1024)
            val outputStream = FileOutputStream(destinationFile)
            var read: Int

            while (assetInputStream.read(buffer).also { read = it } != -1) {
                outputStream.write(buffer, 0, read)
            }

            assetInputStream.close()
            outputStream.flush()
            outputStream.close()

            return true
        } catch (e: IOException) {
            e.printStackTrace()
            return false
        }
    }

    fun prepareModelFiles() {

        for (files_to_copy in Settings.all_files) {
            copyAssetToFolder(
                files_to_copy["pkg"].toString(),
                files_to_copy["file"].toString()
            )
        }
    }
}