package com.gh.gamecenter.gamedetail.desc

import android.content.Context
import android.graphics.drawable.BitmapDrawable
import android.net.Uri
import android.text.Spanned
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder
import com.gh.gamecenter.common.utils.ImageUtils
import com.gh.gamecenter.common.view.CustomLinkMovementMethod
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.utils.toBinding
import com.gh.gamecenter.common.utils.tryWithDefaultCatch
import com.gh.gamecenter.core.utils.*
import com.gh.gamecenter.databinding.GamedetailItemCustomColumnItemBinding
import com.gh.gamecenter.entity.TagEntity
import com.lightgame.adapter.BaseRecyclerAdapter
import com.squareup.picasso.Picasso
import io.reactivex.Single
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

class GameDetailCustomColumnAdapter(context: Context) : BaseRecyclerAdapter<RecyclerView.ViewHolder>(context) {

    private var mTags: ArrayList<TagEntity> = arrayListOf()
    private var mShowTagDes: Boolean = false

    fun updateData(tags: ArrayList<TagEntity>, showTagDes: Boolean) {
        this.mTags = tags
        this.mShowTagDes = showTagDes
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return GameDetailCustomViewHolderViewHolder(parent.toBinding())
    }

    override fun onBindViewHolder(viewHolder: RecyclerView.ViewHolder, position: Int) {
        if (viewHolder is GameDetailCustomViewHolderViewHolder) {
            val (_, name, icon, des, color) = mTags[position]

            val marginBetweenIconAndName = "  "
            val content = if (mShowTagDes) "$marginBetweenIconAndName$name  $des" else "$marginBetweenIconAndName$name"

            val spannable = SpanBuilder(content)
                .color(
                    marginBetweenIconAndName.length,
                    name?.length?.plus(marginBetweenIconAndName.length) ?: 1,
                    color ?: "#000000"
                )
                .build()

            tryWithDefaultCatch {
                Single.just(icon)
                    .map {
                        ImageUtils.picasso.load(Uri.parse(it)).priority(Picasso.Priority.HIGH).get()
                    }.subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe({
                        val bitmapDrawable = BitmapDrawable(mContext.resources, it)
                        bitmapDrawable.setBounds(0, 0, 16F.dip2px(), 16F.dip2px())
                        spannable.setSpan(CenterImageSpan(bitmapDrawable), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
                        viewHolder.binding.contentTv.run {
                            movementMethod = CustomLinkMovementMethod.getInstance()
                            text = spannable
                        }
                    }, {
                        it.printStackTrace()
                    })
            }
        }
    }

    override fun getItemViewType(position: Int): Int {
        return position
    }

    override fun getItemCount(): Int {
        return mTags.size
    }

    class GameDetailCustomViewHolderViewHolder(val binding: GamedetailItemCustomColumnItemBinding) :
        BaseRecyclerViewHolder<Any>(binding.root)
}