package com.dlc.chemical.ui.revert

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.R
import com.dlc.chemical.databinding.DialogRevertWeightTipsBinding

/**
 * info
 *
 * @author ReiChin_
 */
class RevertWeightTipsDialog(
    context: Context,
    private val chemicalsName: String, // 化学品名称
    private val weight: String, // 称重重量
    private val suttle: String, // 化学品净重
    private val illegal: Boolean = false, // 非法归还：使用前重量 < 归还时重量
    private val dialogListener: (() -> Unit)? = null
) :
    Dialog(context, R.style.AlertDialogStyle) {

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

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

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

    @SuppressLint("SetTextI18n")
    private fun initViews() {
        viewBinding.title.text = chemicalsName
        viewBinding.weightValue.text = weight
        if (suttle.isEmpty()) {
            viewBinding.useValue.visibility = View.GONE
        } else {
            viewBinding.useValue.visibility = View.VISIBLE
            viewBinding.useValue.text = suttle
        }
        viewBinding.illegalTips.visibility = if (illegal) View.VISIBLE else View.GONE

        // 取消
        viewBinding.cancel.setOnClickListener { cancel() }
        // 确认
        viewBinding.confirm.setOnClickListener {
            if (illegal) {
                cancel()
            } else {
                dialogListener?.invoke()
                dismiss()
            }
        }
    }

}