package com.rdiot.yx485.view

import android.content.Context
import android.graphics.drawable.ColorDrawable
import android.view.LayoutInflater
import android.view.View
import android.widget.PopupWindow
import androidx.annotation.LayoutRes
import android.view.ViewGroup.LayoutParams
import com.rdiot.yx485.util.setClickLimitListener

/**
 * 自定义pop弹框
 */
class CustomPopupWindow(context: Context, @LayoutRes layout: Int, type: Int) :
    PopupWindow(context) {
    private val mMenuView: View

    init {
        val inflater = LayoutInflater.from(context)
        mMenuView = inflater.inflate(layout, null)
        contentView = mMenuView
        contentView.setClickLimitListener { (dismiss()) }

        setLayoutParams(type)

        isFocusable = true
        val dw = ColorDrawable(0)
        setBackgroundDrawable(dw)
        isOutsideTouchable = true
    }

    private fun setLayoutParams(type: Int) {
        when (type) {
            TYPE_FULL_SCREEN -> {
                width = LayoutParams.MATCH_PARENT
                height = LayoutParams.MATCH_PARENT
            }

            TYPE_WRAP_CONTENT -> {
                width = LayoutParams.WRAP_CONTENT
                height = LayoutParams.WRAP_CONTENT
            }

            TYPE_MATCH_WIDTH -> {
                width = LayoutParams.MATCH_PARENT
                height = LayoutParams.WRAP_CONTENT
            }
        }
    }

    companion object {
        const val TYPE_FULL_SCREEN = 1
        const val TYPE_WRAP_CONTENT = 2
        const val TYPE_MATCH_WIDTH = 3
    }
}