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.text.Editable
import android.text.TextWatcher
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.EditText
import android.widget.RadioButton
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.example.chemical.R
import com.example.chemical.comm.Constants
import com.rc.core.log.RcLog
import java.math.BigDecimal

//废弃弹框   wasteVolume  废弃量
class DiscardDialog(private val ct: Context, private var wasteVolume: String, var lint: IClickLit) :
    Dialog(ct) {
    private var butClos: Button? = null

    init {
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        setContentView(R.layout.discard_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)
        val radioOne = findViewById<RadioButton>(R.id.radio_one)
        val radioTwo = findViewById<RadioButton>(R.id.radio_two)
        val radioThree = findViewById<RadioButton>(R.id.radio_three)
        findViewById<TextView>(R.id.wasteVolume).text = "本次废气量：$wasteVolume，请选择废弃原因"
        radioOne.setOnClickListener {
            radioOne.isChecked = true
            if (radioTwo.isChecked) {
                radioTwo.isChecked = false
                // 设置背景颜色（未选中状态）
                radioTwo.setBackgroundResource(R.drawable.bg_shu_ru_kuang)

                // 设置字体颜色（未选中状态）
                radioTwo.setTextColor(ContextCompat.getColor(ct, R.color.black))
            }
            if (radioThree.isChecked) {
                radioThree.isChecked = false
                // 设置背景颜色（未选中状态）
                radioThree.setBackgroundResource(R.drawable.bg_shu_ru_kuang)

                // 设置字体颜色（未选中状态）
                radioThree.setTextColor(ContextCompat.getColor(ct, R.color.black))
            }
            // 设置背景颜色
            radioOne.setBackgroundResource(R.drawable.bg_shou_ru_two)

            // 设置字体颜色
            radioOne.setTextColor(ContextCompat.getColor(ct, R.color.purple_ff0072dd))
        }

        radioTwo.setOnClickListener {
            radioTwo.isChecked = true
            if (radioOne.isChecked) {
                radioOne.isChecked = false
                // 设置背景颜色（未选中状态）
                radioOne.setBackgroundResource(R.drawable.bg_shu_ru_kuang)

                // 设置字体颜色（未选中状态）
                radioOne.setTextColor(ContextCompat.getColor(ct, R.color.black))
            }
            if (radioThree.isChecked) {
                radioThree.isChecked = false
                // 设置背景颜色（未选中状态）
                radioThree.setBackgroundResource(R.drawable.bg_shu_ru_kuang)

                // 设置字体颜色（未选中状态）
                radioThree.setTextColor(ContextCompat.getColor(ct, R.color.black))
            }
            // 设置背景颜色
            radioTwo.setBackgroundResource(R.drawable.bg_shou_ru_two)

            // 设置字体颜色
            radioTwo.setTextColor(ContextCompat.getColor(ct, R.color.purple_ff0072dd))
        }

        radioThree.setOnClickListener {
            radioThree.isChecked = true
            if (radioOne.isChecked) {
                radioOne.isChecked = false
                // 设置背景颜色（未选中状态）
                radioOne.setBackgroundResource(R.drawable.bg_shu_ru_kuang)

                // 设置字体颜色（未选中状态）
                radioOne.setTextColor(ContextCompat.getColor(ct, R.color.black))
            }
            if (radioTwo.isChecked) {
                radioTwo.isChecked = false
                // 设置背景颜色（未选中状态）
                radioTwo.setBackgroundResource(R.drawable.bg_shu_ru_kuang)

                // 设置字体颜色（未选中状态）
                radioTwo.setTextColor(ContextCompat.getColor(ct, R.color.black))
            }
            // 设置背景颜色
            radioThree.setBackgroundResource(R.drawable.bg_shou_ru_two)

            // 设置字体颜色
            radioThree.setTextColor(ContextCompat.getColor(ct, R.color.purple_ff0072dd))
        }



        findViewById<Button>(R.id.butClos).setOnClickListener {
            closHandle()
            dismiss()
            lint.onClickClose()
        }
        findViewById<EditText>(R.id.edPut).addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun afterTextChanged(p0: Editable?) {
                onUserInteraction()
            }

        })
        findViewById<Button>(R.id.determine).setOnClickListener {
            var cont = findViewById<EditText>(R.id.edPut).text.toString()
            if (cont.isNotEmpty()) {
                RcLog.info("======废弃时填写得参数 $cont")
                lint.onDetermineClick(cont)
            } else {
                if (radioOne.isChecked) {
                    val text = radioOne.text.toString()
                    lint.onDetermineClick(text)
                }
                if (radioTwo.isChecked) {
                    val text = radioTwo.text.toString()
                    lint.onDetermineClick(text)
                }
                if (radioThree.isChecked) {
                    val text = radioThree.text.toString()
                    lint.onDetermineClick(text)
                }
            }
            closHandle()
            dismiss()
        }

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

    }

    interface IClickLit {
        fun onClickClose()//关闭
        fun onDetermineClick(cont: String?)//确定
        fun onTimerReply()//重置最外层弹框信息
    }

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