package com.dlc.chemical.ui.output

import android.app.Dialog
import android.content.Context
import android.content.res.Resources
import android.os.Bundle
import android.view.LayoutInflater
import com.dlc.chemical.R
import com.dlc.chemical.databinding.DialogOutputErrorBinding
import com.rc.core.util.DeviceUtils

/**
 * info
 *
 * @author ReiChin_
 */
class OutputErrorDialog(
    context: Context,
    private val message: String,
    private val dialogListener: (() -> Unit)? = null
) : Dialog(context, R.style.AlertDialogStyle) {

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

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

    private fun initWindowConfig() {
        window?.apply {
            val dm = Resources.getSystem().displayMetrics
            attributes.width = (dm.widthPixels * 0.32f).toInt()
            attributes.height = DeviceUtils.dip2px(context, 158.0f)
        }
    }

    private fun initViews() {
        viewBinding.message.text = message

        viewBinding.cancel.setOnClickListener { cancel() }

        // 确认
        viewBinding.confirm.setOnClickListener {
            dialogListener?.invoke()
            dismiss()
        }
    }
}