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.os.Message
import android.util.Log
import android.view.Gravity
import android.view.MotionEvent
import android.view.Window
import android.view.WindowManager
import android.widget.Button
import android.widget.TextView
import com.example.chemical.R
import com.example.chemical.comm.Constants
import com.example.chemical.ui.still.MsdsActivity
import com.example.chemical.utils.UiManager
import com.rc.core.log.RcLog
import com.rc.httpcore.bean.UseNoTwoBean

//查询-扫码标签-已登录
class ScanCodeDialog(private val ct: Context, private var data: UseNoTwoBean) : Dialog(ct) {
    private var butClos: Button? = null

    init {
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        setContentView(R.layout.scan_code_dialog)
        window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        window?.setGravity(Gravity.CENTER)
        window?.setLayout(
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.WRAP_CONTENT
        )
        // 设置点击外部空白处不关闭对话框
        setCanceledOnTouchOutside(false)
    }

    private var mCounter = 0
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        findViewById<TextView>(R.id.chemicalName).text = "${data.chemicalName}"
        if (data.casNum != null) {
            findViewById<TextView>(R.id.casNum).text = "CAS: ${data.casNum}"
        }else{
            findViewById<TextView>(R.id.casNum).text = "CAS:"
        }

        findViewById<TextView>(R.id.chemicalCategoryName).text = "类别: ${data.chemicalCategoryName}"
        findViewById<TextView>(R.id.specNum).text =
            "规格: ${data.specNum}${data.specUnit}/${data.packUnit}"
        if (data.expireTime != null) {
            findViewById<TextView>(R.id.expireTime).text = "${data.expireTime}"
        }

        findViewById<TextView>(R.id.storageLocation).text =
            "存储位置：${data.cabinetName}-${data.doorName}-${data.layers}层"
        if (data.collectTime != null) {
            findViewById<TextView>(R.id.collectTime).text = "领用时间: ${data.collectTime}"
        } else {
            findViewById<TextView>(R.id.collectTime).text = "领用时间: "
        }
        findViewById<TextView>(R.id.tagCode).text = "编码: ${data.tagCode}"
        findViewById<TextView>(R.id.chemicalLevelName).text = "级别: ${data.chemicalLevelName}"
        findViewById<TextView>(R.id.surplus).text = "库存量: ${data.surplus}${data.specUnit}"
        findViewById<TextView>(R.id.belongName).text = "归属人: ${data.belongName}"

        if (data.useName != null) {
            findViewById<TextView>(R.id.useName).text = "领用人: ${data.useName}"
        } else {
            findViewById<TextView>(R.id.useName).text = "领用人:"
        }

        findViewById<TextView>(R.id.statusStr).text = "${data.statusStr}"


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

        findViewById<TextView>(R.id.tvMsds).setOnClickListener {
            UiManager.switcher(ct, MsdsActivity::class.java)
        }



        mCountDownHandler.removeMessages(Constants.WHAT_COUNT_DOWN_TWO)
        mCountDownHandler.sendEmptyMessage(Constants.WHAT_COUNT_DOWN_TWO)
    }

    private fun closHandle() {
        mCountDownHandler.removeMessages(Constants.WHAT_COUNT_DOWN_TWO)
        mCountDownHandler.removeCallbacksAndMessages(null)
    }

    private val mCountDownHandler = object : Handler(Looper.getMainLooper()) {

        override fun handleMessage(msg: Message) {
            if (Constants.WHAT_COUNT_DOWN_TWO == msg.what) {

                val countDown = Constants.OFFTIME_TIME - mCounter

                Log.d("======返回时间", "$countDown")
                if (countDown <= 0) {
                    dismiss()
                } else {
                    sendEmptyMessageDelayed(Constants.WHAT_COUNT_DOWN_TWO, 1000)
                }
                mCounter++
            }
        }
    }

    override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
        // 在触摸事件发生时被调用
        when (ev.action) {
            MotionEvent.ACTION_DOWN -> {
                // 用户按下屏幕时的处理逻辑
                onUserInteraction()
            }
            MotionEvent.ACTION_MOVE -> {
                // 用户在屏幕上移动手指时的处理逻辑
            }
            MotionEvent.ACTION_UP -> {
                // 用户抬起手指时的处理逻辑
            }
        }
        return super.dispatchTouchEvent(ev)
    }

    private fun onUserInteraction() {
        // 在用户与对话框交互时的逻辑处理
        RcLog.info("=========弹框触发了")
        mCounter = 0
    }
}