package com.ydl.confide.home.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ObservableField
import androidx.recyclerview.widget.RecyclerView
import com.ydl.confide.R
import com.ydl.confide.databinding.ItemConfideHomeRecentBinding
import com.ydl.confide.databinding.ItemVideoShowBinding
import com.ydl.confide.home.bean.ConfideHomeBodyBean
import com.ydl.confide.home.bean.ConfideHomeDataBean
import com.ydl.confide.home.event.IConfideHomeEvent
import com.ydl.confide.intro.BindingViewHolder

class VideoShowAdapter(private val data: List<ItemVideoShowViewModel>, private val event: IConfideHomeEvent) :
    RecyclerView.Adapter<BindingViewHolder<ItemVideoShowBinding>>() {
    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): BindingViewHolder<ItemVideoShowBinding> {
        val layoutInflater = LayoutInflater.from(parent.context)
        val binding =
            DataBindingUtil.inflate<ItemVideoShowBinding>(layoutInflater, R.layout.item_video_show, parent, false)
        return BindingViewHolder(binding)
    }

    override fun onBindViewHolder(holder: BindingViewHolder<ItemVideoShowBinding>, position: Int) {
        val itemVideoShowViewModel = data[position]
        holder.binding.item = itemVideoShowViewModel
        holder.itemView.setOnClickListener { event.videoShowClick(position) }
    }

    override fun getItemCount() = data.size
}


class RecentConfideAdapter(val data: List<ItemVideoShowViewModel>, private val event: IConfideHomeEvent) :
    RecyclerView.Adapter<BindingViewHolder<ItemConfideHomeRecentBinding>>() {
    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): BindingViewHolder<ItemConfideHomeRecentBinding> {
        val layoutInflater = LayoutInflater.from(parent.context)
        val binding =
            DataBindingUtil.inflate<ItemConfideHomeRecentBinding>(
                layoutInflater,
                R.layout.item_confide_home_recent,
                parent,
                false
            )
        return BindingViewHolder(binding)
    }

    override fun onBindViewHolder(holder: BindingViewHolder<ItemConfideHomeRecentBinding>, position: Int) {
        val itemVideoShowViewModel = data[position]
        holder.binding.item = itemVideoShowViewModel
        holder.itemView.setOnClickListener { event.consultantClick(itemVideoShowViewModel.linkUrl) }
    }

    override fun getItemCount() = data.size
}

class ItemVideoShowViewModel {
    var linkUrl: String? = null
    val name = ObservableField<String>("")
    val coverUrl = ObservableField<String>("")
    val videoCoverUrl = ObservableField<String>("")
    val state = ObservableField<String>("")
}

internal fun ItemVideoShowViewModel.mapOf(bean: ConfideHomeBodyBean): ItemVideoShowViewModel {
    name.set(bean.confidedName)
    coverUrl.set(bean.confidedIcon/*coverPicture*/)
    videoCoverUrl.set(bean.confidedIcon/*coverVideoPicture*/)
    state.set(bean.confideLine.toString())
    linkUrl = bean.linkUrl
    return this
}





