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.text.TextUtils
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 android.widget.Toast
import androidx.lifecycle.lifecycleScope
import com.blankj.utilcode.util.ActivityUtils
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import xn.hxp.R
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.SPUtils
import com.rc.httpcore.HttpConfig
import xn.hxp.app.ChemicalApp

//称重弹框
class WeighDialog(
    private val ct: Context,
    private var density: String,
    private var lint: IViewLint
) : Dialog(ct) {
    init {
        setContentView(R.layout.weigh_dialog)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        SPUtils.getInstance().put("WeighDialog",true)
        if (ChemicalApp.confs!!.weighHintPicture != null) {
            LogUtils.i("======称重示意图：${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 {
            val weigh = findViewById<EditText>(R.id.weigh).text.toString()
            if (TextUtils.isEmpty(weigh)) {
                Toast.makeText(ActivityUtils.getTopActivity(), "请输入重量", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }

            dismiss()
            lint.viewCloses()
        }
    }

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


    override fun onStop() {
        super.onStop()
        SPUtils.getInstance().put("WeighDialog",false)
    }
}