package com.base.scanqrclear.utils

import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import com.google.zxing.BarcodeFormat
import com.google.zxing.EncodeHintType
import com.google.zxing.WriterException
import com.google.zxing.common.BitMatrix
import com.google.zxing.oned.EAN13Writer
import com.google.zxing.qrcode.QRCodeWriter

object QRCodeUtils {

    private val TAG = "QRCodeUtils"

//    fun generateQRCode(content: String, width: Int, height: Int): Bitmap? {
//        val writer = QRCodeWriter()
//        val hints = EnumMap<EncodeHintType, Any>(EncodeHintType::class.java)
//        hints[EncodeHintType.MARGIN] = 1 // 设置二维码边距
//        hints[EncodeHintType.ERROR_CORRECTION] = com.google.zxing.qrcode.decoder.ErrorCorrectionLevel.L // 设置二维码的容错级别
//
//        try {
//            val bitMatrix: BitMatrix = writer.encode(content, BarcodeFormat.QR_CODE, width, height, hints)
//            val w = bitMatrix.width
//            val h = bitMatrix.height
//            val bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888)
//            for (x in 0 until w) {
//                for (y in 0 until h) {
//                    bitmap.setPixel(x, y, if (bitMatrix[x, y]) Color.BLACK else Color.WHITE)
//                }
//            }
//            return bitmap
//        } catch (e: WriterException) {
//            e.printStackTrace()
//        }
//        return null
//    }

    fun generateQRCode(content: String, width: Int, height: Int, margin: Int = 1): Bitmap? {
        val writer = QRCodeWriter()
        val hints = HashMap<EncodeHintType, Any>()
        hints[EncodeHintType.MARGIN] = margin // 设置二维码边距，默认为1
        hints[EncodeHintType.ERROR_CORRECTION] = com.google.zxing.qrcode.decoder.ErrorCorrectionLevel.L // 设置二维码的容错级别
        hints[EncodeHintType.CHARACTER_SET] = "UTF-8" // 设置字符集为UTF-8，确保中文可以正确编码
        try {
            val bitMatrix: BitMatrix = writer.encode(content, BarcodeFormat.QR_CODE, width, height, hints)
            val bitmapWidth = bitMatrix.width + margin * 2
            val bitmapHeight = bitMatrix.height + margin * 2
            val bitmap = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Bitmap.Config.ARGB_8888)

            // 绘制带有边距的二维码
            for (x in 0 until bitMatrix.width) {
                for (y in 0 until bitMatrix.height) {
                    val nX = x + margin
                    val nY = y + margin
                    bitmap.setPixel(nX, nY, if (bitMatrix[x, y]) Color.BLACK else Color.WHITE)
                }
            }
            return bitmap
        } catch (e: WriterException) {
            e.printStackTrace()
        }
        return null
    }

    fun generateEAN13Barcode(content: String, width: Int, height: Int): Bitmap? {
        val writer = EAN13Writer()
        val hints = HashMap<EncodeHintType, Any>()
        hints[EncodeHintType.CHARACTER_SET] = "UTF-8"

        try {
            val bitMatrix: BitMatrix = writer.encode(content, BarcodeFormat.EAN_13, width, height, hints)
            val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
            bitmap.setHasAlpha(true)

            val canvas = Canvas(bitmap)
            val paint = Paint()
            paint.color = Color.BLACK
            paint.style = Paint.Style.FILL

            for (x in 0 until width) {
                for (y in 0 until height) {
                    if (bitMatrix[x, y]) {
                        canvas.drawRect(x.toFloat(), y.toFloat(), (x + 1).toFloat(), (y + 1).toFloat(), paint)
                    }
                }
            }
            return bitmap
        } catch (e: WriterException) {
            throw RuntimeException(e)
        }
    }

    fun getPairKeyValue(keyValue: String): Pair<String, String> {
        val split = keyValue.split(":")
        var key = ""
        kotlin.runCatching {
            key = split[0]
        }
        var value: String = ""
        kotlin.runCatching {
            value = split[1]
        }
        return Pair(key, value)
    }

}