package com.rosetta.messenger.utils

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import com.google.zxing.BarcodeFormat
import com.google.zxing.EncodeHintType
import com.google.zxing.qrcode.QRCodeWriter
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel
import com.rosetta.messenger.R

object QrCodeGenerator {

    fun generateQrBitmap(
        content: String,
        size: Int = 512,
        foregroundColor: Int = Color.BLACK,
        backgroundColor: Int = Color.WHITE,
        context: Context? = null
    ): Bitmap? {
        return try {
            val hints = mapOf(
                EncodeHintType.MARGIN to 1,
                EncodeHintType.CHARACTER_SET to "UTF-8",
                EncodeHintType.ERROR_CORRECTION to ErrorCorrectionLevel.H // High error correction for logo overlay
            )
            val bitMatrix = QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, size, size, hints)
            val bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888)
            for (x in 0 until size) {
                for (y in 0 until size) {
                    bitmap.setPixel(x, y, if (bitMatrix[x, y]) foregroundColor else backgroundColor)
                }
            }

            // Overlay logo in center
            if (context != null) {
                overlayLogo(bitmap, context)
            }

            bitmap
        } catch (_: Exception) {
            null
        }
    }

    private fun overlayLogo(qrBitmap: Bitmap, context: Context) {
        try {
            val qrSize = qrBitmap.width
            val logoSize = (qrSize * 0.22f).toInt()
            val cx = qrSize / 2f
            val cy = qrSize / 2f
            val canvas = Canvas(qrBitmap)

            // White circle background
            val bgRadius = logoSize * 0.65f
            val bgPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
                color = Color.WHITE
                style = Paint.Style.FILL
            }
            canvas.drawCircle(cx, cy, bgRadius, bgPaint)

            // Load logo from drawable
            val drawable = androidx.core.content.ContextCompat.getDrawable(context, R.mipmap.ic_launcher_round)
            if (drawable != null) {
                val left = (cx - logoSize / 2f).toInt()
                val top = (cy - logoSize / 2f).toInt()
                drawable.setBounds(left, top, left + logoSize, top + logoSize)
                drawable.draw(canvas)
            }
        } catch (_: Exception) {}
    }

    /** Profile QR payload */
    fun profilePayload(publicKey: String): String = "rosetta://profile/$publicKey"

    /** Group QR payload */
    fun groupPayload(inviteString: String): String = "rosetta://group/$inviteString"

    /** Profile share URL */
    fun profileShareUrl(username: String): String = "https://rosetta.im/@$username"

    /** Profile share URL fallback (by key) */
    fun profileShareUrlByKey(publicKey: String): String = "https://rosetta.im/u/$publicKey"
}
