package com.base.scanqrclear.qr

import android.graphics.Bitmap
import android.graphics.Color
import android.graphics.Point
import android.graphics.drawable.Drawable
import androidx.activity.addCallback
import androidx.core.view.updatePadding
import com.base.scanqrclear.base.BaseActivity
import com.base.scanqrclear.bean.QrStyleBean
import com.base.scanqrclear.databinding.ActivityQrStyleResultBinding
import com.base.scanqrclear.qr.QrStyleUtils.generateQRCodeWithBackground
import com.base.scanqrclear.utils.BarUtils
import com.base.scanqrclear.utils.LogEx
import com.bumptech.glide.Glide
import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.transition.Transition
import com.google.gson.Gson
import com.gyf.immersionbar.ktx.immersionBar

class QrStyleResultActivity : BaseActivity<ActivityQrStyleResultBinding>(ActivityQrStyleResultBinding::inflate) {

    private var qrStyleBean: QrStyleBean? = null
    private var qrString: String = ""

    override fun initView() {
        super.initView()
        binding.clTop.updatePadding(top = BarUtils.getStatusBarHeight())

        qrString = intent.extras?.getString("qrString", "") ?: ""
        LogEx.logDebug(TAG, "qrString=$qrString")
        val data = intent.extras?.getString("data", "")
        data?.let {
            qrStyleBean = Gson().fromJson(it, QrStyleBean::class.java)
        }
        qrStyleBean?.let { bean ->
            Glide.with(this)
                .asBitmap() // 指定加载为 Bitmap
                .load(bean.img_url)
                .into(object : CustomTarget<Bitmap>() {
                    override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
                        // 在这里获取到 Bitmap
                        processBitmap(resource, bean)
                    }

                    override fun onLoadCleared(placeholder: Drawable?) {
                        // 图片加载失败时的回调
                    }
                })
        }
    }

    private fun processBitmap(resource: Bitmap, bean: QrStyleBean) = Thread {

        val split1 = bean.coordinate_one.split(",")
        val points1 = Point(split1[0].toInt(), split1[1].toInt())

        val split2 = bean.coordinate_two.split(",")
        val points2 = Point(split2[0].toInt(), split2[1].toInt())

        val split3 = bean.coordinate_three.split(",")
        val points3 = Point(split3[0].toInt(), split3[1].toInt())

        val split4 = bean.coordinate_four.split(",")
        val points4 = Point(split4[0].toInt(), split4[1].toInt())


        var qrColor = Color.parseColor(bean.color)
        var qrBgColor = Color.WHITE
        var scale = 0.7f
        if (bean.id == 6) {
            scale = 0.8f
        }
        if (bean.id == 12) {
            scale = 0.7f
            qrBgColor = Color.BLACK
        }
        if (bean.id == 14) {
            scale = 0.7f
        }
        if (bean.id == 16) {
            scale = 0.7f
            qrColor = Color.WHITE
            qrBgColor = Color.BLACK
        }
        if (bean.id == 18) {
            scale = 0.7f
        }
        if (bean.id == 22) {
            scale = 0.7f
            qrColor = Color.WHITE
            qrBgColor = Color.TRANSPARENT
        }
        if (bean.id == 24) {
            scale = 0.7f
        }
        if (bean.id == 28) {
            qrBgColor = Color.TRANSPARENT
        }
        if (bean.id == 38) {
            scale = 0.6f
            qrColor = Color.WHITE
            qrBgColor = Color.TRANSPARENT
        }
        if (bean.id == 42) {
            qrColor = Color.WHITE
            qrBgColor = Color.TRANSPARENT
        }
        if (bean.id == 44) {
            qrColor = Color.WHITE
            qrBgColor = Color.TRANSPARENT
        }
        if (bean.id == 46) {
            qrColor = Color.WHITE
            qrBgColor = Color.TRANSPARENT
        }
        if (bean.id == 48) {
            qrColor = Color.WHITE
            qrBgColor = Color.TRANSPARENT
        }
        if (bean.id == 64) {
            qrColor = Color.WHITE
            qrBgColor = Color.TRANSPARENT
        }
        if (bean.id == 66) {
            qrColor = Color.WHITE
            qrBgColor = Color.TRANSPARENT
        }
        if (bean.id == 68) {
            qrColor = Color.WHITE
            qrBgColor = Color.TRANSPARENT
        }
        if (bean.id == 70) {
            qrColor = Color.WHITE
            qrBgColor = Color.TRANSPARENT
        }

        LogEx.logDebug(TAG, "id=${bean.id} scale=$scale ${bean.color} ${bean.img_url}")

        val qrBitmap = generateQRCodeWithBackground(
            qrString, resource,
            listOf(points1, points2, points3, points4), scale, qrColor, qrBgColor
        )

        runOnUiThread {
            binding.iv.setImageBitmap(qrBitmap)
        }

    }.start()

    override fun initListener() {
        super.initListener()
        onBackPressedDispatcher.addCallback { finish() }
        binding.flBack.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }
    }

    override fun configSystemBar() {
        immersionBar {
            statusBarColor("#FFFFFFFF")
        }
    }

}