package com.gh.gamecenter.qa.editor

import android.content.Context
import android.graphics.drawable.ColorDrawable
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import android.widget.CursorAdapter
import android.widget.FrameLayout
import android.widget.PopupWindow
import androidx.appcompat.widget.ListPopupWindow
import androidx.core.content.ContextCompat
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.R

class VideoAlbumsSpanner(val context: Context) {

    private var mListPopupWindow: ListPopupWindow = CustomListPopupWindow(context, 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(ContextCompat.getColor(context, R.color.transparent)))
    }

    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 (mListPopupWindow.isShowing) return
        if (popupHeight > 0) mListPopupWindow.height = popupHeight
        // 去掉ListPopupWindow默认动画
        mListPopupWindow.animationStyle = 0
        mListPopupWindow.show()
        val containerView = mListPopupWindow.listView as? ViewGroup
        val params = containerView?.layoutParams as ViewGroup.LayoutParams
        params.height = 280f.dip2px()
        containerView.background = ColorDrawable(ContextCompat.getColor(context, R.color.background_white))
        containerView.layoutParams = params
        val parentContainer = containerView.parent as FrameLayout

        //添加半透明maskView
        val maskView = View(context)
        val maskViewParams = FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, popupHeight)
        maskView.background = ColorDrawable(ContextCompat.getColor(context, R.color.black_alpha_40))
        maskView.layoutParams = maskViewParams
        parentContainer.addView(maskView, 0)
        maskView.setOnClickListener {
            mListPopupWindow.dismiss()
        }
    }
}

class CustomListPopupWindow(val context: Context, defStyleAttr: Int) : ListPopupWindow(context, null, defStyleAttr) {
    override fun dismiss() {
        val parentContainer = listView?.parent as? FrameLayout
        parentContainer?.getChildAt(0)?.alpha = 0f
        parentContainer?.postDelayed({ super.dismiss() }, 0)
    }
}