package com.gh.common.pop

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.view.updateLayoutParams
import com.gh.gamecenter.common.databinding.LayoutPopupContainerBinding
import com.gh.gamecenter.common.view.BugFixedPopupWindow
import com.gh.gamecenter.core.utils.DisplayUtils
import com.gh.gamecenter.databinding.LayoutAddKaifuPopupBinding
import com.gh.gamecenter.databinding.LayoutAddKaifuPopupItemBinding
import com.gh.gamecenter.databinding.LayoutPopupOptionItemBinding
import com.gh.gamecenter.databinding.LayoutPopupReserveReminderOptionItemBinding

abstract class CommonPopupWindow(
    private val binding: LayoutPopupContainerBinding,
) : BugFixedPopupWindow(binding.root, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT) {

    private val inflater = LayoutInflater.from(binding.root.context)

    abstract val viewDataList: List<ViewData>

    fun initView() {
        isFocusable = true
        binding.container.updateLayoutParams<ViewGroup.LayoutParams> {
            width = DisplayUtils.dip2px(128F)
        }
        viewDataList.forEach {
            addItemView(it.textResId, it.click)
        }
    }

    private fun addItemView(textResId: Int, click: () -> Unit) =
        LayoutPopupReserveReminderOptionItemBinding.inflate(inflater, binding.container, false)
            .also {
                it.hintText.setText(textResId)
                binding.container.addView(it.root)
                it.root.setOnClickListener {
                    dismiss()
                    click()
                }
            }.root

    data class ViewData(
        val textResId: Int,
        val click: () -> Unit
    )
}