package com.adealink.weparty.commonui.mediapicker.videoselect.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import com.adealink.weparty.commonui.mediapicker.videoselect.data.VideoItem
import com.adealink.weparty.commonui.mediapicker.videoselect.listener.VideoSelectOpListener
import com.adealink.weparty.commonui.recycleview.adapter.BindingViewHolder
import com.adealink.weparty.commonui.recycleview.adapter.multitype.ItemViewBinder
import com.adealink.weparty.databinding.ItemVideoSelectBinding
import java.util.Locale

class VideoSelectItemViewBinder(private val l: VideoSelectOpListener) :
    ItemViewBinder<VideoItem, BindingViewHolder<ItemVideoSelectBinding>>() {

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

    override fun onBindViewHolder(
        holder: BindingViewHolder<ItemVideoSelectBinding>,
        item: VideoItem
    ) {
        holder.binding.coverIv.setImageUrl(item.url)
        val seconds = item.duration / 1000
        val time = String.format(
            Locale.getDefault(), "%d:%02d",
            seconds / 60, seconds % 60
        )
        holder.binding.videoDurationTv.text = time
        holder.binding.root.setOnClickListener {
            l.onVideoSelect(item)
        }
    }

}