package com.gh.gamecenter.gamedetail.rating

import android.view.LayoutInflater
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.gh.gamecenter.R
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder
import com.gh.gamecenter.common.utils.setDrawableEnd
import com.gh.gamecenter.common.utils.showAutoOrientation
import com.gh.gamecenter.common.view.BugFixedPopupWindow
import com.gh.gamecenter.databinding.RatingItemBinding

class RatingItemViewHolder(val binding: RatingItemBinding) : BaseRecyclerViewHolder<Any>(binding.root) {

    /**
     * 注意:
     * contentList [POP_SORT] 和 [POP_FILTER] 文案的内容 与[RatingViewModel.filterList] 判断对应
     */
    fun showPopWindow(view: TextView, type: Int, selectedValue: String, clickListener: (String) -> Unit) {
        view.setDrawableEnd(R.drawable.game_comment_filter_top)

        val contentList = when (type) {
            POP_SORT -> arrayListOf("默认", "热门", "最新")
            POP_FILTER -> arrayListOf("全部评价", "同设备", "1星", "2星", "3星", "4星", "5星")
            else -> throw IllegalArgumentException()
        }

        val inflater = LayoutInflater.from(view.context)
        val layout = inflater.inflate(com.gh.gamecenter.common.R.layout.layout_popup_container, null)
        val popupWindow = BugFixedPopupWindow(
            layout,
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT
        )

        val container = layout.findViewById<LinearLayout>(R.id.container)
        for (text in contentList) {
            val item = inflater.inflate(R.layout.layout_popup_option_item, container, false)
            container.addView(item)

            val hitText = item.findViewById<TextView>(R.id.hint_text)
            hitText.text = text
            hitText.setTextColor(
                ContextCompat.getColor(
                    view.context,
                    if (text == selectedValue) com.gh.gamecenter.common.R.color.text_theme else com.gh.gamecenter.common.R.color.text_secondary
                )
            )

            item.setOnClickListener {
                popupWindow.dismiss()
                clickListener.invoke(text)
            }
        }

        popupWindow.setOnDismissListener {
            view.setDrawableEnd(R.drawable.game_comment_filter_bottom)
        }

        popupWindow.isTouchable = true
        popupWindow.isFocusable = true

        popupWindow.showAutoOrientation(view)
    }

    companion object {
        const val POP_SORT = 101
        const val POP_FILTER = 102
    }
}