package com.example.livingai.utils

import android.graphics.Bitmap
import android.graphics.Canvas


fun fitCenterToScreen(
    bitmap: Bitmap,
    screenW: Int,
    screenH: Int
): Bitmap {

    val srcW = bitmap.width
    val srcH = bitmap.height

    if (srcW == 0 || srcH == 0) return bitmap

    val scale = minOf(
        screenW.toFloat() / srcW,
        screenH.toFloat() / srcH
    )

    val newW = (srcW * scale).toInt()
    val newH = (srcH * scale).toInt()

    val scaled = Bitmap.createScaledBitmap(bitmap, newW, newH, true)

    val output = Bitmap.createBitmap(screenW, screenH, Bitmap.Config.ARGB_8888)
    val canvas = Canvas(output)

    val left = (screenW - newW) / 2f
    val top = (screenH - newH) / 2f

    canvas.drawBitmap(scaled, left, top, null)

    return output
}

fun fitImageToCrop(
    bitmap: Bitmap,
    screenW: Int,
    screenH: Int
): Bitmap {
    val srcW = bitmap.width
    val srcH = bitmap.height

    if (srcW == 0 || srcH == 0) return bitmap

    val scale = maxOf(
        screenW.toFloat() / srcW,
        screenH.toFloat() / srcH
    )

    val newW = (srcW * scale).toInt()
    val newH = (srcH * scale).toInt()

    val scaled = Bitmap.createScaledBitmap(bitmap, newW, newH, true)

    val output = Bitmap.createBitmap(screenW, screenH, Bitmap.Config.ARGB_8888)
    val canvas = Canvas(output)

    val left = (screenW - newW) / 2f
    val top = (screenH - newH) / 2f

    canvas.drawBitmap(scaled, left, top, null)

    return output
}