package com.dlc.chemical.ui.use

import android.annotation.SuppressLint
import android.app.Dialog
import android.content.Context
import android.content.res.Resources
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import com.dlc.chemical.ChemicalApp
import com.dlc.chemical.R
import com.dlc.chemical.databinding.DialogUseVerifyFailBinding
import com.dlc.chemical.ui.common.ModuleEnum
import com.rc.httpcore.vo.response.ChemicalBean

/**
 * info
 *
 * @author ReiChin_
 */
class UseVerifyFailDialog(
    context: Context,
    private val moduleEnum: ModuleEnum,
    private val useBean: ChemicalBean,
    private val scanTagCode: String?,
    private val listener: (type: Int) -> Unit
) :
    Dialog(context, R.style.AlertDialogStyle) {

    companion object {
        const val TYPE_REOPEN_LOCK = 1
        const val TYPE_RESCAN = 2
    }

    private val viewBinding: DialogUseVerifyFailBinding by lazy {
        DialogUseVerifyFailBinding.inflate(LayoutInflater.from(context))
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(viewBinding.root)
        initWindowConfig()
        setCanceledOnTouchOutside(false)
        setCancelable(false)
        initViews()
    }

    private fun initWindowConfig() {
        window?.apply {
            val dm = Resources.getSystem().displayMetrics
            attributes.width = (dm.widthPixels * 0.4f).toInt()
            attributes.height = WindowManager.LayoutParams.WRAP_CONTENT
        }
    }

    @SuppressLint("SetTextI18n")
    private fun initViews() {
        viewBinding.reopenLock.visibility = if (true == ChemicalApp.sLearnLoginVo?.cabinetLock) View.VISIBLE else View.GONE
        viewBinding.reopenLock.setOnClickListener {
            listener.invoke(TYPE_REOPEN_LOCK)
            dismiss()
        }
        viewBinding.rescan.setOnClickListener {
            listener.invoke(TYPE_RESCAN)
            dismiss()
        }

        viewBinding.chemicalName.text = "选择化学品：${useBean.chemicalName ?: ""}"
        viewBinding.chemicalNum.text = "化学品编号：${useBean.chemicalNum ?: ""}"
        viewBinding.casNum.text = "CAS号：${useBean.casNum ?: ""}"
        viewBinding.tagCode.text = "标签编号：${useBean.tagCode ?: ""}"
        viewBinding.errorMessage.text = errorMessage()
    }

    private fun errorMessage(): String {
        return when (moduleEnum) {
            ModuleEnum.USE -> "当前扫描的标签编号：${scanTagCode ?: ""}，与选择领用的化学品不符，请核对确认。"
            ModuleEnum.OUTPUT_STORE -> "当前扫描的标签编号：${scanTagCode ?: ""}，与选择出库的化学品不符，请核对确认。"
            else -> ""
        }
    }

}