package com.gh.gamecenter.video.poster.photo

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.drawable.ColorDrawable
import android.view.View
import android.widget.AdapterView
import android.widget.CursorAdapter
import android.widget.PopupWindow
import androidx.appcompat.widget.ListPopupWindow

class PhotoAlbumsSpanner(val context: Context) {

    // 是否有更好的办法去除阴影???
    @SuppressLint("ResourceType")
    private var mListPopupWindow: ListPopupWindow = ListPopupWindow(context, null, com.gh.gamecenter.common.R.style.PosterListPopupWindow)

    private lateinit var mAdapter: CursorAdapter

    var onItemSelectedListener: AdapterView.OnItemSelectedListener? = null

    init {
        mListPopupWindow.isModal = true
        mListPopupWindow.setContentWidth(context.resources.displayMetrics.widthPixels)
        mListPopupWindow.setOnItemClickListener { parent, view, position, id ->
            mListPopupWindow.dismiss()
            onItemSelectedListener?.onItemSelected(parent, view, position, id)
        }
        mListPopupWindow.setBackgroundDrawable(ColorDrawable(context.resources.getColor(com.gh.gamecenter.common.R.color.text_28282E)))
    }

    fun setDismissListener(listener: PopupWindow.OnDismissListener) {
        mListPopupWindow.setOnDismissListener(listener)
    }

    fun setAdapter(adapter: CursorAdapter) {
        mListPopupWindow.setAdapter(adapter)
        mAdapter = adapter
    }

    fun setPopupAnchorView(view: View?) {
        mListPopupWindow.anchorView = view
    }

    fun show(popupHeight: Int) {
        if (popupHeight > 0) mListPopupWindow.height = popupHeight
        mListPopupWindow.show()
    }
}