package com.bomostory.sceneeditmodule.utils

import android.content.Context
import android.graphics.Bitmap
import android.net.Uri
import android.provider.MediaStore
import com.bomostory.sceneeditmodule.Config
import java.io.BufferedOutputStream
import java.io.File
import java.io.FileOutputStream

object FileUtils {
    fun saveImageForActor(context: Context, uri: Uri, fileName: String){
        val dir = File(Config.IMAGE_FOLDER,"")
        dir.mkdir()
        val file = File(Config.IMAGE_FOLDER, fileName)
        try {
            val bitmap = MediaStore.Images.Media.getBitmap(context.contentResolver, uri)
            val bos = BufferedOutputStream(FileOutputStream(file))
            bitmap.compress(Bitmap.CompressFormat.JPEG, 80, bos)
            bos.flush()
            bos.close()
            if (context is OnSaveActorImage) {
                context.onSaveActorImage(file.path)
            } else {
                throw RuntimeException(context.toString() + " must implement OnFragmentInteractionListener")
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    interface OnSaveActorImage {
        fun onSaveActorImage(filePath: String)
    }
}