package com.gh.common.pop

import android.content.Context
import android.graphics.Color
import android.text.SpannableString
import android.text.TextPaint
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.text.style.ForegroundColorSpan
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.core.text.set
import com.gh.common.dialog.OnReserveSuccessListener
import com.gh.gamecenter.R
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.common.utils.toResString
import com.gh.gamecenter.common.view.BugFixedPopupWindow
import com.gh.gamecenter.core.utils.DisplayUtils
import com.gh.gamecenter.databinding.PopRealNameTipsBinding
import com.halo.assistant.fragment.reserve.OnReserveReminderListener

class RealNameTipsPop(
    private val binding: PopRealNameTipsBinding,
    private val listener: OnReserveReminderListener
) :
    BugFixedPopupWindow(binding.root, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT) {

    private val context: Context
        get() = binding.root.context

    init {
        initView()
    }

    private fun initView() {

        val text = R.string.reserve_real_name_description.toResString()
        val spannableString = SpannableString(text)
        val clickSpan = object : ClickableSpan() {
            override fun updateDrawState(ds: TextPaint) {
                ds.bgColor = Color.TRANSPARENT
                ds.setColor(com.gh.gamecenter.common.R.color.text_theme.toColor(context))
            }

            override fun onClick(widget: View) {
                dismiss()
                listener.realName()
            }
        }
        spannableString.set(start = text.length - 6, end = text.length, span = clickSpan)
        binding.tvRealNameDescription.text = spannableString

        binding.tvRealNameDescription.movementMethod = LinkMovementMethod.getInstance()
        binding.tvRealNameDescription.setHighlightColor(Color.TRANSPARENT)
    }

    companion object {

        @JvmStatic
        fun create(context: Context, listener: OnReserveReminderListener): RealNameTipsPop {
            val pop = RealNameTipsPop(PopRealNameTipsBinding.inflate(LayoutInflater.from(context)), listener)
            pop.isFocusable = true
            pop.isOutsideTouchable = false
            return pop
        }
    }
}