package com.adealink.weparty.playmate.detail.adapter

import android.content.res.Resources
import android.graphics.Bitmap
import android.os.Build
import android.text.SpannableStringBuilder
import android.text.style.TypefaceSpan
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.graphics.drawable.toDrawable
import com.adealink.frame.ext.findAndSetSpan
import com.adealink.frame.image.imageService
import com.adealink.frame.image.listener.ICloseableImageResultListener
import com.adealink.weparty.commonui.DEFAULT_FONT_BOLD
import com.adealink.weparty.commonui.ext.dp
import com.adealink.weparty.commonui.ext.isViewValid
import com.adealink.weparty.commonui.recycleview.adapter.BindingViewHolder
import com.adealink.weparty.commonui.recycleview.adapter.multitype.ItemViewBinder
import com.adealink.weparty.commonui.widget.CenterImageSpan
import com.adealink.weparty.playmate.databinding.ItemPlaymateDetailExtraFieldBinding
import com.adealink.weparty.playmate.detail.data.PlaymateExtraFieldItemData
import com.facebook.common.references.CloseableReference
import com.facebook.imagepipeline.image.CloseableBitmap
import com.facebook.imagepipeline.image.CloseableImage

class PlaymateDetailExtraFieldViewBinder() :
    ItemViewBinder<PlaymateExtraFieldItemData, PlaymateDetailExtraFieldViewBinder.ViewHolder>() {

    companion object {
        const val TAG_ICON = "[icon]"
    }

    override fun onBindViewHolder(holder: ViewHolder, item: PlaymateExtraFieldItemData) {
        holder.update(item)
    }

    override fun onCreateViewHolder(
        inflater: LayoutInflater,
        parent: ViewGroup,
    ): ViewHolder {
        return ViewHolder(
            ItemPlaymateDetailExtraFieldBinding.inflate(
                inflater,
                parent,
                false
            )
        )
    }

    inner class ViewHolder(
        binding: ItemPlaymateDetailExtraFieldBinding,
    ) : BindingViewHolder<ItemPlaymateDetailExtraFieldBinding>(binding) {

        private var item: PlaymateExtraFieldItemData? = null

        fun update(item: PlaymateExtraFieldItemData) {
            this.item = item

            val titleStr = item.data.fieldName
            val descStr = item.data.getFieldValueDesc() ?: ""
            val icon = item.data.icon
            if (icon.isNullOrEmpty()) {
                binding.tvDesc.text =
                    getDesc(binding.root.context.resources, null, titleStr, descStr)
            } else {
                imageService.fetchImage(icon, object : ICloseableImageResultListener {
                    override fun onSuccess(reference: CloseableReference<CloseableImage>) {
                        super.onSuccess(reference)
                        if (isViewValid() && this@ViewHolder.item?.data?.icon == icon) {
                            val icon = (reference.get() as? CloseableBitmap)?.underlyingBitmap
                            binding.tvDesc.text =
                                getDesc(binding.root.context.resources, icon, titleStr, descStr)
                        }
                    }

                    override fun onFailed() {
                        super.onFailed()
                        if (isViewValid()) {
                            binding.tvDesc.text =
                                getDesc(binding.root.context.resources, null, titleStr, descStr)
                        }
                    }
                })
            }
        }


        private fun getDesc(
            resource: Resources,
            icon: Bitmap?,
            title: String,
            desc: String
        ): SpannableStringBuilder {
            val text = if (icon == null || icon.isRecycled) {
                "$title $desc"
            } else {
                "$TAG_ICON $title $desc"
            }
            return SpannableStringBuilder(text).apply {
                if (icon != null && !icon.isRecycled) {
                    findAndSetSpan(
                        CenterImageSpan(icon.toDrawable(resource).apply {
                            setBounds(0, 0, 18.dp(), 18.dp())
                        }),
                        TAG_ICON
                    )
                }
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                    findAndSetSpan(
                        TypefaceSpan(DEFAULT_FONT_BOLD),
                        title
                    )
                }
            }
        }
    }

}