package com.zj365.mime.weight

import android.content.Context
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.PopupWindow
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.zj365.mime.R
import com.zj365.mime.adapter.MsgClassAdapter

class MessagePop(context: Context,var select:(position:Int)->Unit) {
    private var popupWindow: PopupWindow? = null
    private var adapter: MsgClassAdapter? = null
    val array by lazy{context.resources.getStringArray(R.array.msg_array)}


    init {
        initPop(context)
    }

    private fun initPop(context: Context) {
        var view = LayoutInflater.from(context).inflate(R.layout.layout_msg, null, false)
        var mRecycle = view.findViewById<RecyclerView>(R.id.mRecycle)
        adapter= MsgClassAdapter()
        mRecycle.adapter = adapter
        mRecycle.layoutManager = LinearLayoutManager(context)
        adapter!!.setOnItemClickListener { adapter, view, position ->
            popupWindow!!.dismiss()
            select.invoke(position)

        }
        popupWindow = PopupWindow(
            view,
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT,
            true
        )

    }


    fun showAction(view: View) {
        adapter!!.setNewInstance(array.toMutableList())
        popupWindow!!.showAsDropDown(view, Gravity.BOTTOM, 0, 0)
    }


}