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.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 xn.hxp.R
import xn.hxp.comm.Constants
import xn.hxp.ui.adapter.SelectTopicAdapter
import com.blankj.utilcode.util.LogUtils
import com.rc.httpcore.bean.TopicDataBean

//新增--选择课题组
class SelectTopicDialog(
    private var ct: Context,
    private var dataList: List<TopicDataBean>,
    private var lint: IonItemLint
) :
    Dialog(ct) {

    private val mAdapter by lazy { SelectTopicAdapter() }

    private var mCounter = 0
    private var butClos: Button? = null
    init {
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        setContentView(R.layout.select_topic_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 rvView = findViewById<RecyclerView>(R.id.rvView)

        val layoutManager = LinearLayoutManager(ct, LinearLayoutManager.VERTICAL, false)
        rvView.layoutManager = layoutManager
        rvView.adapter = mAdapter
        mAdapter.setNewInstance(dataList.toMutableList())

        mAdapter.setOnItemClickListener { adapter, view, position ->
            lint.onItem(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 IonItemLint {
        fun onItem(topicDataBean: TopicDataBean)
    }
    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

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