package com.adealink.weparty.util

import android.graphics.Bitmap
import android.graphics.BitmapShader
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Shader

object ImageUtil {
    @JvmStatic
    fun createCircularBitmapWithBorder(
        bitmap: Bitmap,
        borderWidthPx: Int,
        borderColor: Int
    ): Bitmap? {
        if (bitmap.isRecycled) {
            return null
        }

        val width: Int = bitmap.width + borderWidthPx
        val height: Int = bitmap.height + borderWidthPx
        val canvasBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
        val shader = BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP)
        val paint = Paint()
        paint.isAntiAlias = true
        paint.shader = shader
        val canvas = Canvas(canvasBitmap)
        val radius = if (width > height) height.toFloat() / 2f else width.toFloat() / 2f
        canvas.drawCircle((width / 2).toFloat(), (height / 2).toFloat(), radius, paint)
        paint.shader = null
        paint.style = Paint.Style.STROKE
        paint.color = borderColor
        paint.strokeWidth = borderWidthPx.toFloat()
        canvas.drawCircle(
            (width / 2).toFloat(),
            (height / 2).toFloat(),
            radius - borderWidthPx / 2,
            paint
        )
        return canvasBitmap
    }
}