package com.mrousavy.camera.utils

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.util.Size
import java.io.File
import java.io.FileOutputStream

class FileUtils {
  companion object {
    fun writeBitmapTofile(bitmap: Bitmap, file: File, quality: Int) {
      FileOutputStream(file).use { stream ->
        bitmap.compress(Bitmap.CompressFormat.JPEG, 50, stream)
      }
    }

    fun getImageSize(imagePath: String): Size {
      val bitmapOptions = BitmapFactory.Options().also {
        it.inJustDecodeBounds = true
      }
      BitmapFactory.decodeFile(imagePath, bitmapOptions)
      val width = bitmapOptions.outWidth
      val height = bitmapOptions.outHeight
      return Size(width, height)
    }

    fun createTempFile(context: Context, extension: String): File =
        File.createTempFile("mrousavy", extension, context.cacheDir).also {
      it.deleteOnExit()
    }
  }
}
