package com.remax.visualnovel.utils

import android.content.Context
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.PopupWindow
import androidx.core.widget.PopupWindowCompat


object PopupUtils {
    fun showPopup(context: Context, anchorView: View, layoutRes: Int) {
        val contentView = LayoutInflater.from(context).inflate(layoutRes, null)
        val popupWindow = PopupWindow(
            contentView,
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT
        ).apply {
            isFocusable = true
            isOutsideTouchable = true
            elevation = 10f
        }

        PopupWindowCompat.setOverlapAnchor(popupWindow, true)
        PopupWindowCompat.showAsDropDown(popupWindow, anchorView, 0, 0, Gravity.START)
    }

    fun createPopup(context: Context, layoutRes: Int) : PopupWindow {
        val contentView = LayoutInflater.from(context).inflate(layoutRes, null)
        val popupWindow = PopupWindow(
            contentView,
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT
        ).apply {
            isFocusable = true
            isOutsideTouchable = true
            elevation = 10f
        }

        PopupWindowCompat.setOverlapAnchor(popupWindow, true)
        return popupWindow
    }

    fun createPopup(context: Context, contentView: View) : PopupWindow {
        val popupWindow = PopupWindow(
            contentView,
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT
        ).apply {
            isFocusable = true
            isOutsideTouchable = true
            elevation = 10f
        }

        PopupWindowCompat.setOverlapAnchor(popupWindow, true)
        return popupWindow
    }

}