package com.dlc.chemical.ui.msds

import android.content.Context
import android.content.Intent
import android.os.Bundle
import cn.bingoogolapple.qrcode.core.BGAQRCodeUtil
import cn.bingoogolapple.qrcode.zxing.QRCodeEncoder
import com.bumptech.glide.Glide
import com.dlc.chemical.R
import com.dlc.chemical.databinding.ActivityMsdsDetailBinding
import com.dlc.chemical.ui.common.BaseCountDownActivity
import com.rc.core.util.EscapeUnescape
import com.rc.core.util.WebViewHelper
import com.rc.httpcore.client.ApiRepository
import com.rc.httpcore.vo.response.HazardBook
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

/**
 * info
 *
 * @author ReiChin_
 */
class MsdsDetailActivity : BaseCountDownActivity<ActivityMsdsDetailBinding>() {

    companion object {
        fun startAction(context: Context, hazardId: String) {
            val intent = Intent(context, MsdsDetailActivity::class.java)
            intent.putExtra("hazardId", hazardId)
            context.startActivity(intent)
        }
    }

    override fun createViewBinding() = ActivityMsdsDetailBinding.inflate(layoutInflater)

    override fun initViews(savedInstanceState: Bundle?) {
        val webViewHelper = WebViewHelper(viewBinding.webView, viewBinding.progressbar)
        webViewHelper.initWebView()

        viewBinding.back.setOnClickListener { finish() }
    }

    override fun initData() {
        showLoading("查询中...")
        val disposable = ApiRepository.hazardBookDetail(intent.getStringExtra("hazardId")!!)
            .subscribe({ data ->
                dismissLoading()
                refreshDetailUI(data)
            }, { throwable ->
                dismissLoading()
                showNetError(throwable)
                throwable.printStackTrace()
            })
        addDisposable(disposable)
    }

    private fun refreshDetailUI(data: HazardBook?) {
        data?.let {
            // 文件内容
            if (!data.content.isNullOrEmpty()) {
                val html = StringBuilder()
                html.append("<HTML><HEAD><LINK href=\"simple_table.css\" type=\"text/css\" rel=\"stylesheet\"/></HEAD><body>")
                html.append(EscapeUnescape.unescape(data.content))
                html.append("</body></HTML>")

                viewBinding.webView.loadDataWithBaseURL(
                    "file:///android_asset/",
                    html.toString(),
                    "text/html",
                    "utf-8",
                    null
                )
            }

            // 二维码
            GlobalScope.launch(Dispatchers.Main) {
                val bitmap = withContext(Dispatchers.IO) {
                    QRCodeEncoder.syncEncodeQRCode(
                        data.qrCodeUrl,
                        BGAQRCodeUtil.dp2px(this@MsdsDetailActivity, 150f)
                    )
                }
                Glide.with(this@MsdsDetailActivity)
                    .asBitmap()
                    .load(bitmap)
                    .error(R.mipmap.img_error)
                    .into(viewBinding.qrCode)
            }
        }
    }

}