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.MotionEvent
import android.view.Window
import android.view.WindowManager
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import androidx.lifecycle.lifecycleScope
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import com.example.chemical.ChemicalApp
import com.example.chemical.R
import com.rc.core.log.RcLog
import com.rc.httpcore.HttpConfig

//称重弹框
class WeighDialog(
    private val ct: Context,
    private var density: String,
    private var lint: IViewLint
) : Dialog(ct) {
    init {
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        setContentView(R.layout.weigh_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)
        if (ChemicalApp.confs!!.weighHintPicture!=null){
            RcLog.info("======称重示意图：${HttpConfig.API_BASE_IMG_URL}${ChemicalApp.confs!!.weighHintPicture}")
            val findViewById = findViewById<ImageView>(R.id.imgView)
            // 使用 Glide 加载网络图片
            Glide.with(ct)
                .load("${HttpConfig.API_BASE_IMG_URL}${ChemicalApp.confs!!.weighHintPicture}")
                .apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.AUTOMATIC))
                .into(findViewById)
        }
        findViewById<TextView>(R.id.density).text = "$density"
        var weighingValue = findViewById<EditText>(R.id.weigh)
        weighingValue.isEnabled = false  //禁止输入  需要链接蓝牙称
        findViewById<ImageView>(R.id.imgType).setOnClickListener {
            //手动输入
            weighingValue.isEnabled = true  //禁止链接蓝牙称
            lint.onMovement()
        }

        lint.weighingView(weighingValue)

        val tvClose = findViewById<TextView>(R.id.tvClose)
        tvClose.setOnClickListener {
            dismiss()
            lint.viewCloses()
        }
        mHandler.postDelayed({
            dismiss()
        }, 1000 * 60) // 15秒后关闭对话框
    }

    interface IViewLint {
        fun weighingView(weighingValue: EditText)
        fun viewCloses()
        fun onMovement() //手动输入 手动编辑  进行输入  需要断开蓝牙称
        fun onTimerReply() //重置万层计时器  防止未操作完成就返回页面
    }


    override fun onStop() {
        super.onStop()
        RcLog.info("========已经调用1")

        mHandler.removeCallbacksAndMessages(null)
    }
}