package com.adealink.weparty.imageselect.view

import android.view.LayoutInflater
import android.view.ViewGroup
import com.adealink.weparty.commonui.recycleview.adapter.BindingViewHolder
import com.adealink.weparty.commonui.recycleview.adapter.multitype.ItemViewBinder
import com.adealink.weparty.databinding.ItemVideoBinding
import com.adealink.weparty.imageselect.listener.OnOperateListener
import com.adealink.weparty.imageselect.model.LocalMedia
import java.util.Locale

class VideoItemViewBinder(private val l: OnOperateListener) :
    ItemViewBinder<LocalMedia, BindingViewHolder<ItemVideoBinding>>() {

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

    override fun onBindViewHolder(
        holder: BindingViewHolder<ItemVideoBinding>,
        item: LocalMedia
    ) {
        holder.binding.coverIv.setImageUrl(item.uri)
        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.onSelectVideo(item)
        }
    }

}