package com.casic.detector.widgets

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ListView
import android.widget.PopupWindow
import android.widget.TextView
import com.casic.detector.R
import com.pengxh.kt.lite.extensions.getScreenWidth
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButton

class SamplePopupWindow constructor(context: Context) : PopupWindow() {
    private var clickListener: OnPopupWindowClickListener? = null
    private var showPosition = -1

    fun setShowPosition(showPosition: Int) {
        this.showPosition = showPosition
    }

    init {
        width = ((context.getScreenWidth() * 0.35).toInt())
        height = ViewGroup.LayoutParams.WRAP_CONTENT
        isOutsideTouchable = true
        isFocusable = true
        setBackgroundDrawable(null)
        contentView = LayoutInflater.from(context).inflate(R.layout.popup_menu_sample, null, false)
    }

    fun setPopupMenuItem(titleArray: Array<String>) {
        val popupListView = contentView.findViewById<ListView>(R.id.popupListView)
        val baseAdapter = object : BaseAdapter() {
            private val inflater: LayoutInflater = LayoutInflater.from(contentView.context)
            override fun getCount(): Int {
                return titleArray.size
            }

            override fun getItem(position: Int): Any {
                return titleArray[position]
            }

            override fun getItemId(position: Int): Long {
                return position.toLong()
            }

            override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
                val view: View
                val holder: PopupWindowHolder
                if (convertView == null) {
                    view = inflater.inflate(R.layout.item_popup_menu, null)
                    holder = PopupWindowHolder()
                    holder.titleView = view.findViewById(R.id.titleView)
                    holder.tagView = view.findViewById(R.id.tagView)
                    view.tag = holder
                } else {
                    view = convertView
                    holder = view.tag as PopupWindowHolder
                }
                holder.titleView.text = titleArray[position]
                if (showPosition == position) {
                    holder.tagView.visibility = View.VISIBLE
                } else {
                    holder.tagView.visibility = View.INVISIBLE
                }
                return view
            }
        }
        popupListView.adapter = baseAdapter
        popupListView.setOnItemClickListener { _, _, position, _ ->
            clickListener?.onPopupItemClicked(position)
            dismiss()
        }
    }

    interface OnPopupWindowClickListener {
        fun onPopupItemClicked(position: Int)
    }

    fun setOnPopupWindowClickListener(windowClickListener: OnPopupWindowClickListener?) {
        clickListener = windowClickListener
    }

    internal class PopupWindowHolder {
        lateinit var titleView: TextView
        lateinit var tagView: QMUIRoundButton
    }
}