package com.example.chemical.utils

import android.graphics.*
import android.util.Log
import com.example.chemical.ui.PrintBean
import com.google.zxing.BarcodeFormat
import com.google.zxing.EncodeHintType
import com.google.zxing.MultiFormatWriter
import com.google.zxing.WriterException
import com.rc.core.log.RcLog


object BitmapUtils {


    fun generateBitmap(printDate: PrintBean): Bitmap? {
        // 设置画布尺寸
        val width = 400
        val height = 400 // 225 * 3，每行占 225 的高度

        // 创建 Bitmap
        val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)

        // 创建 Canvas
        val canvas = Canvas(bitmap)

        // 设置背景色
        canvas.drawColor(Color.WHITE)

        // 居中绘制二维码
        val qrCodeSize = 225
        val qrCodeX = (width - qrCodeSize) / 2
        val qrCodeY = 0
        drawQRCode(printDate.wxCode,canvas, qrCodeX, qrCodeY, 230)
        // 在二维码下方居中绘制文本
        val text = "编码：${printDate.tag}"
        val textX = (width - getTextWidth(text, 20)) / 2
        val textY = qrCodeY + qrCodeSize-6
        drawText(canvas, text, textX, textY.toFloat(), 19)
        // 计算剩余内容的起始 Y 坐标
        val contentStartY = textY + 30
        // 绘制剩余的内容，左对齐显示
        val content = listOf(
            "${printDate.level} 一 ${printDate.name}",
            "CAS：${printDate.casNo}      ${printDate.types}",
            "归属人：${printDate.person}",
        )
        val lineHeight = 24
        content.forEachIndexed { index, line ->
            drawText(canvas, line, 10f, contentStartY + index * lineHeight.toFloat(), 20)
        }
        return bitmap
    }

    //编码 生成二维码
    private fun drawQRCode(tag:String,canvas: Canvas, x: Int, y: Int, size: Int) {
        val multiFormatWriter = MultiFormatWriter()
        try {
            val bitMatrix = multiFormatWriter.encode(
                "$tag", // 你想要生成二维码的内容
                BarcodeFormat.QR_CODE,
                size,
                size,
                hashMapOf(EncodeHintType.CHARACTER_SET to "UTF-8")
            )
            val width = bitMatrix.width
            val height = bitMatrix.height
            val pixels = IntArray(width * height)
            for (y in 0 until height) {
                val offset = y * width
                for (x in 0 until width) {
                    pixels[offset + x] = if (bitMatrix.get(x, y)) Color.BLACK else Color.WHITE
                }
            }
            val qrCodeBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
            qrCodeBitmap.setPixels(pixels, 0, width, 0, 0, width, height)
            canvas.drawBitmap(qrCodeBitmap, x.toFloat(), y.toFloat(), null)
        } catch (e: WriterException) {
            Log.e("BitmapUtils", "Failed to generate QR code: ${e.message}")
        }
    }

    private fun drawText(canvas: Canvas, text: String, x: Float, y: Float, textSize: Int) {
        val paint = Paint(Paint.ANTI_ALIAS_FLAG)
        paint.color = Color.BLACK
        paint.textSize = textSize.toFloat()
        paint.typeface = Typeface.create(Typeface.DEFAULT, Typeface.NORMAL)
        canvas.drawText(text, x, y, paint)
    }

    private fun getTextWidth(text: String, textSize: Int): Float {
        val paint = Paint(Paint.ANTI_ALIAS_FLAG)
        paint.textSize = textSize.toFloat()
        return paint.measureText(text)
    }

    //气瓶信息打印
    fun airBottlePrint(rfid:String): Bitmap? {
        // 设置画布尺寸
        val width = 400
        val height = 400 // 225 * 3，每行占 225 的高度

        // 创建 Bitmap
        val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)

        // 创建 Canvas
        val canvas = Canvas(bitmap)

        // 设置背景色
        canvas.drawColor(Color.WHITE)

        // 居中绘制二维码
        val qrCodeSize = 225
        val qrCodeX = (width - qrCodeSize) / 2
        RcLog.info("======二维码剧中数据$qrCodeX")
        val qrCodeY = 6
        drawQRCode(rfid,canvas, qrCodeX, qrCodeY, 230)
        // 在二维码下方居中绘制文本
        val text = "" +
                "$rfid"
        val textX = (width - getTextWidth(text, 20)) / 2
        val textY = qrCodeY + qrCodeSize-6
        drawText(canvas, text, textX, textY.toFloat(), 19)
        // 计算剩余内容的起始 Y 坐标
        val contentStartY = textY + 30
        // 绘制剩余的内容，左对齐显示
        val content = listOf(
            "",
            "",
            "",
        )
        val lineHeight = 24
        content.forEachIndexed { index, line ->
            drawText(canvas, line, 10f, contentStartY + index * lineHeight.toFloat(), 20)
        }
        return bitmap
    }
}