package com.example.chemical.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.View
import android.view.Window
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import com.example.chemical.R

//0  没有图标 1 绿色   2红色
class CustomDialog(private var ct: Context, private var types: Int, private var msgs: String) :
    Dialog(ct) {

    init {
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        setContentView(R.layout.custom_dialog_layout)
        window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        window?.setGravity(Gravity.CENTER)
    }
    private var mHandler: Handler = Handler(Looper.getMainLooper())
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val msg = findViewById<TextView>(R.id.msg)
        val imgA = findViewById<ImageView>(R.id.imgA)   //红色
        val imgB = findViewById<ImageView>(R.id.imgB)   //绿色

        when (types) {
            1 -> {//显示绿色
                imgB.visibility = View.VISIBLE
            }
            2 -> { //显示红色
                imgA.visibility = View.VISIBLE
            }
        }
        msg.text = "$msgs"

        mHandler.postDelayed({
            try {
                dismiss()
            } catch (e: Exception) {
            }
        }, 1500) // 3秒后关闭对话框

    }

    override fun onStop() {
        super.onStop()
        mHandler.removeCallbacksAndMessages(null)
    }
}