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.os.Message
import android.util.Log
import android.view.*
import android.widget.Button
import android.widget.TextView
import xn.hxp.R
import xn.hxp.comm.Constants
import com.blankj.utilcode.util.LogUtils
import java.math.BigDecimal

//归还--柜门已开  使用量  剩余量
class CabinetOpenDialog(
    ct: Context,
    private var isLock: Boolean,
    private var useAmount: String,
    private var remark: String,
    private var lint: IClickLit
) : Dialog(ct) {
    private var butClos: Button? = null


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

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (useAmount.isNotEmpty()) {
            //使用量
            findViewById<TextView>(R.id.useAmount).text = "使用量：${useAmount}"
        }

        if (remark.isNotEmpty()) {
            //剩余量
            findViewById<TextView>(R.id.remark).text = "剩余量：${remark}"
        }

        findViewById<Button>(R.id.butClos).setOnClickListener {
            closHandle()
            dismiss()
            lint.onClickClo()
        }
        findViewById<Button>(R.id.butOpen).setOnClickListener {
            mCounter = 0
            lint.onOpenClick()
        }
        if (isLock) {
            findViewById<Button>(R.id.butOpen).visibility = View.VISIBLE
        }
        mCountDownHandler.removeMessages(Constants.WHAT_COUNT_DOWN_TWO)
        mCountDownHandler.sendEmptyMessage(Constants.WHAT_COUNT_DOWN_TWO)
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        closHandle()
    }

    interface IClickLit {
        fun onClickClo()//关闭后调用归还api
        fun onOpenClick()//再次开门
    }

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

    private var mCounter = 0
    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 && isShowing) {
                    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() {
        // 在用户与对话框交互时的逻辑处理
        mCounter = 0
    }
}