package xn.hxp.weidith

import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.Gravity
import android.view.Window
import android.view.WindowManager
import android.widget.Button
import android.widget.EditText
import xn.hxp.R
import java.lang.ref.WeakReference

//化学品标签--气瓶标签提示
class AirBottleDialog(private val ct: Context, private val lintDate: ILintDate) : Dialog(ct) {

    init {
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        setContentView(R.layout.air_bottle_dialog)
        window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        window?.setGravity(Gravity.CENTER)
        window?.setLayout(
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.WRAP_CONTENT
        )

    }

    private var mHandler: Handler = Handler(Looper.getMainLooper())

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        findViewById<Button>(R.id.butClos).setOnClickListener {
            dismiss()
        }

        findViewById<Button>(R.id.butDetermine).setOnClickListener {
            val trim = findViewById<EditText>(R.id.cont).text.toString().trim()
            if (trim.isEmpty()) {
                return@setOnClickListener
            } else {
                lintDate.onLintDate(trim)
                dismiss()
            }
        }

//        Handler().postDelayed({
//            dismiss()
//        }, 1000 * 15) // 15秒后关闭对话框
        mHandler.postDelayed({
            dismiss()
        }, 1000 * 15) // 15秒后关闭对话框

    }

    override fun onStop() {
        super.onStop()
        // 取消 Handler 的任务，避免潜在的内存泄漏
        mHandler.removeCallbacksAndMessages(null)
    }

    interface ILintDate {
        fun onLintDate(cont: String)
    }

}