package com.guadou.cs_cptservices.popup

import android.annotation.SuppressLint
import android.content.Context
import android.view.View
import android.widget.TextView
import com.guadou.cs_cptservices.R
import com.guadou.lib_baselib.ext.checkEmpty
import com.guadou.lib_baselib.ext.click
import com.guadou.lib_baselib.ext.dp2px
import com.lxj.xpopup.core.CenterPopupView
import com.lxj.xpopup.util.XPopupUtils

/**
 * 默认的取消 确定弹窗
 */
@SuppressLint("ViewConstructor")
class CustomNormalPopup(
    context: Context, private val title: String?, private val content: CharSequence,
    private val yes: String? = null, private val no: String? = null,
    private val action: (() -> Unit)? = null,
) : CenterPopupView(context) {

    override fun getImplLayoutId(): Int = R.layout.popup_custom_normal

    override fun onCreate() {
        super.onCreate()

        val tvTitle = findViewById<TextView>(R.id.tv_title)
        val tvContent = findViewById<TextView>(R.id.tv_content)
        val tvYes = findViewById<TextView>(R.id.tv_yes)
        val tvNo = findViewById<TextView>(R.id.tv_no)
        val tvOk = findViewById<TextView>(R.id.tv_ok)

        if (!title.checkEmpty()) {
            tvTitle.text = title
        } else {
            tvTitle.text = "提示"
        }

        tvContent.text = content

        if (!yes.checkEmpty() && !no.checkEmpty()) {
            tvYes.visibility = View.VISIBLE
            tvNo.visibility = View.VISIBLE
            tvOk.visibility = View.GONE
            tvYes.text = yes
            tvNo.text = no

            tvYes.click {
                action?.invoke()
                dismiss()
            }
            tvNo.click {
                dismiss()
            }

        } else if (!yes.checkEmpty()) {
            tvYes.visibility = View.GONE
            tvNo.visibility = View.GONE
            tvOk.visibility = View.VISIBLE
            tvOk.text = yes
            tvOk.click {
                action?.invoke()
                dismiss()
            }
        } else if (!no.checkEmpty()) {
            tvYes.visibility = View.GONE
            tvNo.visibility = View.GONE
            tvOk.visibility = View.VISIBLE
            tvOk.text = no
            tvOk.click {
                action?.invoke()
                dismiss()
            }
        } else {
            tvYes.visibility = View.GONE
            tvNo.visibility = View.GONE
            tvOk.visibility = View.VISIBLE
            tvOk.text = "知道了"
            tvOk.click {
                action?.invoke()
                dismiss()
            }
        }

    }

    //如果不设置可能出现xml的背景圆角无法正常显示
    override fun getMaxWidth(): Int {
        return if (popupInfo.maxWidth == 0) (XPopupUtils.getWindowWidth(context) * 0.99f).toInt() else popupInfo.maxWidth
    }

    override fun getMaxHeight(): Int {
        return dp2px(500f)
    }

}