package com.adealink.weparty.playmate.saywelcome.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import com.adealink.frame.aab.util.getCompatColor
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.show
import com.adealink.weparty.commonui.recycleview.adapter.BindingViewHolder
import com.adealink.weparty.commonui.recycleview.adapter.multitype.ItemViewBinder
import com.adealink.weparty.module.playmate.data.SayWelcomeStatus
import com.adealink.weparty.module.profile.data.VoiceData
import com.adealink.weparty.playmate.R
import com.adealink.weparty.playmate.databinding.LayoutPlaymateSayWelcomeItemBinding
import com.adealink.weparty.playmate.saywelcome.data.SayWelcomeItemData
import com.adealink.weparty.R as APP_R

class SayWelcomeItemViewBinder(
    val onItemClick: (item: SayWelcomeItemData, position: Int) -> Unit
) : ItemViewBinder<SayWelcomeItemData, BindingViewHolder<LayoutPlaymateSayWelcomeItemBinding>>() {

    override fun onBindViewHolder(
        holder: BindingViewHolder<LayoutPlaymateSayWelcomeItemBinding>,
        item: SayWelcomeItemData,
    ) {
        holder.binding.root.onClick {
            onItemClick.invoke(item, holder.layoutPosition + 1)
        }

        holder.binding.tvTitle.text = getCompatString(
            R.string.playmate_welcome_title_index,
            (holder.layoutPosition + 1).toString()
        )

        val welcomeStatus = SayWelcomeStatus.map(item.data.status)
        when (welcomeStatus) {
            SayWelcomeStatus.PENDING_REVIEW -> {
                holder.binding.tvStatus.setTextColor(getCompatColor(APP_R.color.color_FFFFB836))
                holder.binding.tvStatus.text =
                    getCompatString(APP_R.string.playmate_welcome_review_processing)
            }

            SayWelcomeStatus.PASS -> {
                holder.binding.tvStatus.setTextColor(getCompatColor(APP_R.color.color_FF1D2129))
                holder.binding.tvStatus.text =
                    getCompatString(APP_R.string.playmate_welcome_review_pass)
            }

            SayWelcomeStatus.REJECT -> {
                holder.binding.tvStatus.setTextColor(getCompatColor(APP_R.color.color_FFF23051))
                holder.binding.tvStatus.text =
                    getCompatString(APP_R.string.playmate_welcome_review_reject)
            }
        }

        if (item.data.isText()) {
            holder.binding.clText.show()
            holder.binding.clVoice.gone()
            holder.binding.tvText.text = item.data.text

        } else if (item.data.isVoice()) {
            holder.binding.clText.gone()
            holder.binding.clVoice.show()
            holder.binding.vSound.setSound(
                VoiceData(
                    url = item.data.voice,
                    path = null,
                    duration = item.data.voiceDuration?.toLong()
                )
            )

        } else {
            holder.binding.clText.gone()
            holder.binding.clVoice.gone()
        }
    }

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

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

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