package bou.amine.apps.readerforselfossv2.android.utils.glide

import android.content.Context
import android.graphics.Bitmap
import android.widget.ImageView
import bou.amine.apps.readerforselfossv2.android.utils.CircleImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.InputStream

fun Context.bitmapCenterCrop(
    url: String,
    iv: ImageView,
) = Glide.with(this)
    .asBitmap()
    .load(url)
    .apply(RequestOptions.centerCropTransform())
    .into(iv)

fun Context.circularDrawable(
    url: String,
    view: CircleImageView,
) {
    view.textView.text = ""

    Glide.with(this)
        .load(url)
        .into(view.imageView)
}

fun getBitmapInputStream(
    bitmap: Bitmap,
    compressFormat: Bitmap.CompressFormat,
): InputStream {
    val byteArrayOutputStream = ByteArrayOutputStream()
    bitmap.compress(compressFormat, 80, byteArrayOutputStream)
    val bitmapData: ByteArray = byteArrayOutputStream.toByteArray()
    return ByteArrayInputStream(bitmapData)
}
