package com.remax.visualnovel.app.widget.tips

import android.content.Context
import android.graphics.drawable.ColorDrawable
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.PopupWindow
import androidx.annotation.StringRes
import com.remax.visualnovel.R
import com.remax.visualnovel.extension.setOnClick
import com.remax.visualnovel.extension.setSize
import com.remax.visualnovel.utils.spannablex.utils.dp
import com.remax.visualnovel.widget.ui.IconFontTextView
import com.remax.visualnovel.widget.uitoken.changeTextStyle

class TipsMoreWindow {
    data class TipsMoreUIData(
        @StringRes val titleRes: Int,
        @StringRes val iconRes: Int,
    )

    private var popupWindow: PopupWindow? = null

    fun build(
        context: Context,
        tipsData: List<TipsMoreUIData>?,
        clickCallback: (TipsMoreUIData) -> Unit = {}
    ): TipsMoreWindow {
        val view = LayoutInflater.from(context).inflate(R.layout.popwindow_btn_tips, null)
        popupWindow = PopupWindow(context).apply {
            isFocusable = true
            contentView = view
            isOutsideTouchable = true
            setBackgroundDrawable(ColorDrawable())
        }
        val group = view.findViewById<LinearLayout>(R.id.group)
        group.removeAllViews()
        tipsData?.forEach { item ->
            val iconView = IconFontTextView(context)
            group.addView(iconView)
            iconView.apply {
                gravity = Gravity.START
                setSize(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                setPadding(8.dp, 12.dp, 8.dp, 12.dp)
                changeTextStyle {
                    textUITextToken = context.getString(R.string.txt_label_l)
                    textUIColorToken = context.getString(R.string.color_txt_primary_normal)

                }
                setIconFontDrawable(
                    startIconFont = context.getString(item.iconRes),
                    iconColorToken = context.getString(R.string.color_txt_primary_normal),
                    iconSize = 20,
                    iconPadding = 16
                )
                setText(item.titleRes)
                setOnClick(this) {
                    popupWindow?.dismiss()
                    clickCallback(item)
                }
            }
        }
        return this
    }

    fun showAsDropDown(view: View?) {
        if (popupWindow!!.isShowing) {
            popupWindow!!.dismiss()
        } else {
            popupWindow!!.showAsDropDown(view)
        }
    }

    fun showAsDropDown(view: View?, xoff: Int, yoff: Int) {
        if (popupWindow!!.isShowing) {
            popupWindow!!.dismiss()
        } else {
            popupWindow!!.showAsDropDown(view, xoff, yoff)
        }
    }

    fun showAtLocation(parent: View?, gravity: Int, x: Int, y: Int) {
        if (popupWindow!!.isShowing) {
            popupWindow!!.dismiss()
        } else {
            popupWindow!!.showAtLocation(parent, gravity, x, y)
        }
    }
}