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 androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.chemical.R
import com.example.chemical.comm.Constants
import com.example.chemical.ui.MainActivity
import com.example.chemical.ui.adapter.BelongingPersonAdapter
import com.example.chemical.ui.common.BaseCountDownActivity
import com.example.chemical.utils.UiManager
import com.rc.core.log.RcLog
import com.rc.httpcore.bean.BelongingPersonBean

//选择-归属人-信息列表
class BelongingPersonDialog(
    private val ct: Context,
    private val listData: List<BelongingPersonBean>,
    private val lint: IClickLit
) : Dialog(ct) {

    private val mAdapter by lazy { BelongingPersonAdapter() }

    init {
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        setContentView(R.layout.belonging_person_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)
        val rvInventory = findViewById<RecyclerView>(R.id.rvInventory)

        rvInventory.layoutManager = LinearLayoutManager(ct)
        rvInventory.adapter = mAdapter
        mAdapter.setNewInstance(listData.toMutableList())
        mAdapter.setOnItemClickListener { adapter, view, position ->
            RcLog.info("==========SHU")
            lint.onClickUserId(mAdapter.data[position])
            closHandle()
            dismiss()
        }

        findViewById<Button>(R.id.butClos).setOnClickListener {
            closHandle()
            dismiss()
        }
        mCountDownHandler.removeMessages(Constants.WHAT_COUNT_DOWN_TWO)
        mCountDownHandler.sendEmptyMessage(Constants.WHAT_COUNT_DOWN_TWO)
    }

    interface IClickLit {
        fun onClickUserId(data: BelongingPersonBean)
    }

    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

                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
    }

}