package com.base.appzxhy.ui.adapter

import android.content.Context
import android.graphics.Color
import android.text.SpannableString
import android.text.style.ForegroundColorSpan
import android.view.ViewGroup
import com.base.appzxhy.R
import com.base.appzxhy.bean.ShortPlayHistory
import com.base.appzxhy.databinding.ItemShortplayStyle5Binding
import com.base.appzxhy.utils.SpJsonUtils
import com.base.appzxhy.utils.SpJsonUtils.KEY_SHORTPLAY_FAVORITE
import com.base.appzxhy.utils.XmlEx.inflate
import com.bumptech.glide.Glide
import com.bytedance.sdk.shortplay.api.ShortPlay
import com.chad.library.adapter4.BaseQuickAdapter
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch

class ShortPlayHistoryAdapter(
    val layout: Int
) : BaseQuickAdapter<ShortPlayHistory, CommonViewHolder>() {

    var itemClick: ((shortList: ShortPlay) -> Unit)? = null

    override fun onBindViewHolder(holder: CommonViewHolder, position: Int, item: ShortPlayHistory?, payloads: List<Any>) {
        super.onBindViewHolder(holder, position, item, payloads)
        item ?: return
        when (layout) {
            R.layout.item_shortplay_style_5 -> {
                val binding = ItemShortplayStyle5Binding.bind(holder.itemView)
                kotlin.runCatching {
                    val payload = payloads[0]
                    when (payload) {
                        "changeFavorite" -> {
                            if (item.isFavorite) {
                                binding.ivFavorite.setImageResource(R.mipmap.shoucang_s)
                            } else {
                                binding.ivFavorite.setImageResource(R.mipmap.shoucang_n)
                            }

                            saveShortFavorite(item, item.isFavorite)
                        }

                    }
                }
            }
        }
    }

    /**
     * 保存短剧收藏
     */
    private fun saveShortFavorite(item: ShortPlayHistory, favorite: Boolean) {
        MainScope().launch(Dispatchers.IO) {
            val favoriteList = SpJsonUtils.getSpJsonList<ShortPlay>(KEY_SHORTPLAY_FAVORITE).toMutableList()
            if (favorite) {
                val findBean = favoriteList.find { it.id == item.shortPlay.id }
                if (findBean == null) {
                    favoriteList.add(item.shortPlay)
                    SpJsonUtils.saveJsonBeanList(KEY_SHORTPLAY_FAVORITE, favoriteList)
                }
            } else {
                favoriteList.removeIf { it.id == item.shortPlay.id }
                SpJsonUtils.saveJsonBeanList(KEY_SHORTPLAY_FAVORITE, favoriteList)
            }
        }
    }

    override fun onBindViewHolder(holder: CommonViewHolder, position: Int, item: ShortPlayHistory?) {
        item ?: return
        val context = holder.itemView.context
        when (layout) {
            R.layout.item_shortplay_style_5 -> {
                val binding = ItemShortplayStyle5Binding.bind(holder.itemView)
                Glide.with(context).load(item.shortPlay.coverImage).into(binding.iv)
                binding.tvName.text = item.shortPlay.title

                val epIndex = "EP." + item.epIndex + " "
                val epTotal = "/ EP." + item.shortPlay.total
                val fullText = epIndex + epTotal
                val spannableString = SpannableString(fullText)

                spannableString.setSpan(
                    ForegroundColorSpan(Color.parseColor("#FF3D71")),
                    0, epIndex.length, // 从 0 开始到 epIndex.length 结束
                    SpannableString.SPAN_INCLUSIVE_INCLUSIVE
                )

                spannableString.setSpan(
                    ForegroundColorSpan(Color.parseColor("#80FFFFFF")),
                    epIndex.length, fullText.length, // 从 epIndex.length 开始到 fullText.length 结束
                    SpannableString.SPAN_INCLUSIVE_INCLUSIVE
                )
                binding.tvWatchEP.text = spannableString

                if (item.isFavorite) {
                    binding.ivFavorite.setImageResource(R.mipmap.shoucang_s)
                } else {
                    binding.ivFavorite.setImageResource(R.mipmap.shoucang_n)
                }
                binding.ivFavorite.setOnClickListener {
                    item.isFavorite = !item.isFavorite
                    notifyItemChanged(position, "changeFavorite")
                }
            }
        }
    }

    override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): CommonViewHolder {
        return CommonViewHolder(layout.inflate(parent))
    }
}