package com.adealink.weparty.playmate.list.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import com.adealink.frame.aab.util.getCompatString
import com.adealink.frame.util.onClick
import com.adealink.weparty.commonui.ext.gone
import com.adealink.weparty.commonui.ext.hide
import com.adealink.weparty.commonui.ext.show
import com.adealink.weparty.commonui.recycleview.adapter.BindingViewHolder
import com.adealink.weparty.commonui.recycleview.adapter.multitype.ItemViewBinder
import com.adealink.weparty.playmate.R
import com.adealink.weparty.playmate.data.PlaymateListData
import com.adealink.weparty.playmate.data.PlaymateListItemData
import com.adealink.weparty.playmate.databinding.ItemPlaymateHomeListBinding
import com.adealink.weparty.util.formatStar

class PlaymateListItemViewBinder(val listener: OnPlaymateListListener) :
    ItemViewBinder<PlaymateListItemData, BindingViewHolder<ItemPlaymateHomeListBinding>>() {

    override fun onBindViewHolder(
        holder: BindingViewHolder<ItemPlaymateHomeListBinding>,
        item: PlaymateListItemData,
    ) {
        holder.binding.root.onClick {
            listener.goCategoryDetail(item.data)
        }

        holder.binding.ivAvatar.setImageUrl(item.data.avatar)
        if (item.data.voice.isEmpty()) {
            holder.binding.vVoice.gone()
            holder.binding.vVoice.setSoundUrl(null)
        } else {
            holder.binding.vVoice.show()
            holder.binding.vVoice.setSoundUrl(item.data.voice)
        }


        holder.binding.tvName.text = item.data.nickname
        holder.binding.vGender.setAge(item.data.age)
        holder.binding.vGender.setGender(item.data.gender)
        holder.binding.tvLocation.text = item.data.area
        holder.binding.tvStar.text = formatStar(item.data.star)
        holder.binding.tvComment.text =
            getCompatString(R.string.playmate_comment_count, item.data.commentCount.toString())
        holder.binding.ivCategory.setImageUrl(item.data.categoryIcon)
        holder.binding.tvCategory.text = item.data.categoryName
        holder.binding.tvSummary.text = item.data.summary

        item.data.photos.getOrNull(0)?.takeIf { it.isNotEmpty() }?.let {
            holder.binding.ivPhoto1.show()
            holder.binding.ivPhoto1.setImageUrl(it)
        } ?: let {
            holder.binding.ivPhoto1.hide()
        }

        item.data.photos.getOrNull(1)?.takeIf { it.isNotEmpty() }?.let {
            holder.binding.ivPhoto2.show()
            holder.binding.ivPhoto2.setImageUrl(it)
        } ?: let {
            holder.binding.ivPhoto2.hide()
        }

        item.data.photos.getOrNull(2)?.takeIf { it.isNotEmpty() }?.let {
            holder.binding.ivPhoto3.show()
            holder.binding.ivPhoto3.setImageUrl(it)
        } ?: let {
            holder.binding.ivPhoto3.hide()
        }

        holder.binding.vPrice.setPrice(item.data.price, item.data.unit)
    }

    override fun onCreateViewHolder(
        inflater: LayoutInflater,
        parent: ViewGroup,
    ): BindingViewHolder<ItemPlaymateHomeListBinding> {
        return BindingViewHolder(ItemPlaymateHomeListBinding.inflate(inflater, parent, false))
    }

    override fun onViewDetachedFromWindow(holder: BindingViewHolder<ItemPlaymateHomeListBinding>) {
        super.onViewDetachedFromWindow(holder)
        holder.binding.vVoice.stopPlay()
    }

    override fun onViewRecycled(holder: BindingViewHolder<ItemPlaymateHomeListBinding>) {
        holder.binding.vVoice.stopPlay()
        super.onViewRecycled(holder)
    }

    interface OnPlaymateListListener {
        fun goCategoryDetail(data: PlaymateListData)
    }
}