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

import android.content.Context
import android.graphics.drawable.ColorDrawable
import android.view.LayoutInflater
import android.view.View
import android.widget.PopupWindow
import com.remax.visualnovel.R
import com.remax.visualnovel.databinding.PopwindowFeedsBackTipsBinding
import com.remax.visualnovel.entity.request.AIFeedback
import com.remax.visualnovel.extension.setOnClick

class TipsFeedbackWindow {

    private var popupWindow: PopupWindow? = null

    fun build(
        context: Context,
        optType: Int,
        clickCallback: (Int, optType: Int) -> Unit
    ): TipsFeedbackWindow {
        val view = LayoutInflater.from(context).inflate(R.layout.popwindow_feeds_back_tips, null)
        popupWindow = PopupWindow(context).apply {
            isFocusable = true
            contentView = view
            isOutsideTouchable = true
            setBackgroundDrawable(ColorDrawable())
        }
        var currOptType = optType

        PopwindowFeedsBackTipsBinding.bind(view).run {
            fun changeOptType() {
                val likeColorToken = context.getString(R.string.color_primary_variant_normal)
                val normalColorToken = context.getString(R.string.color_txt_primary_normal)
                val iconSize = 20
                val iconPadding = 16

                fun updateLikeDislikeIcons(
                    likeIconRes: String,
                    likeColor: String,
                    dislikeIconRes: String,
                    dislikeColor: String,
                ) {
                    like.setIconFontDrawable(
                        likeIconRes,
                        iconColorToken = likeColor,
                        iconSize = iconSize,
                        iconPadding = iconPadding
                    )
                    dislike.setIconFontDrawable(
                        dislikeIconRes,
                        iconColorToken = dislikeColor,
                        iconSize = iconSize,
                        iconPadding = iconPadding
                    )
                }

                when (currOptType) {
                    AIFeedback.LIKE -> updateLikeDislikeIcons(
                        context.getString(R.string.icon_post_recommend_fill),
                        likeColorToken,
                        context.getString(R.string.icon_post_notrecommend),
                        normalColorToken
                    )

                    AIFeedback.DISLIKE -> updateLikeDislikeIcons(
                        context.getString(R.string.icon_post_recommend),
                        normalColorToken,
                        context.getString(R.string.icon_post_notrecommend_fill),
                        likeColorToken
                    )

                    else -> updateLikeDislikeIcons(
                        context.getString(R.string.icon_post_recommend),
                        normalColorToken,
                        context.getString(R.string.icon_post_notrecommend),
                        normalColorToken
                    )
                }

                when (currOptType) {
                    AIFeedback.LIKE -> {
                        like.setIconFontDrawable(
                            context.getString(R.string.icon_post_recommend_fill),
                            iconColorToken = likeColorToken,
                            iconSize = iconSize,
                            iconPadding = iconPadding
                        )
                        dislike.setIconFontDrawable(
                            context.getString(R.string.icon_post_notrecommend),
                            iconColorToken = normalColorToken,
                            iconSize = iconSize,
                            iconPadding = iconPadding
                        )
                    }
                }
            }

            changeOptType()
            setOnClick(copy, like, dislike) {
                when (this) {
                    copy -> {
                        clickCallback.invoke(0, currOptType)
                        popupWindow?.dismiss()
                    }

                    like -> {
                        currOptType = if (currOptType == AIFeedback.LIKE) AIFeedback.NONE else AIFeedback.LIKE
                        changeOptType()
                        clickCallback.invoke(1, currOptType)
                    }

                    dislike -> {
                        currOptType = if (currOptType == AIFeedback.DISLIKE) AIFeedback.NONE else AIFeedback.DISLIKE
                        changeOptType()
                        clickCallback.invoke(2, currOptType)
                    }
                }
            }
        }
        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)
        }
    }
}
