package com.remax.visualnovel.widget.imageviewer.viewholders

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.remax.visualnovel.R
import com.remax.visualnovel.databinding.ItemImageviewerVideoBinding
import com.remax.visualnovel.widget.imageviewer.ImageViewerAdapterListener
import com.remax.visualnovel.widget.imageviewer.adapter.ItemType
import com.remax.visualnovel.widget.imageviewer.core.Components.requireImageLoader
import com.remax.visualnovel.widget.imageviewer.core.Components.requireVHCustomizer
import com.remax.visualnovel.widget.imageviewer.core.Photo
import com.remax.visualnovel.widget.imageviewer.widgets.video.ExoVideoView2

class VideoViewHolder(
    parent: ViewGroup,
    callback: ImageViewerAdapterListener,
    val binding: ItemImageviewerVideoBinding =
        ItemImageviewerVideoBinding.inflate(LayoutInflater.from(parent.context), parent, false)
) : RecyclerView.ViewHolder(binding.root) {
    init {
        binding.videoView.addListener(object : ExoVideoView2.Listener {
            override fun onDrag(view: ExoVideoView2, fraction: Float) = callback.onDrag(this@VideoViewHolder, view, fraction)
            override fun onRestore(view: ExoVideoView2, fraction: Float) = callback.onRestore(this@VideoViewHolder, view, fraction)
            override fun onRelease(view: ExoVideoView2) = callback.onRelease(this@VideoViewHolder, view)
        })
        requireVHCustomizer().initialize(ItemType.VIDEO, this)
    }

    fun bind(item: Photo, position: Int) {
        binding.videoView.setTag(R.id.viewer_adapter_item_key, item.id())
        binding.videoView.setTag(R.id.viewer_adapter_item_data, item)
        binding.videoView.setTag(R.id.viewer_adapter_item_holder, this)
        requireVHCustomizer().bind(ItemType.VIDEO, item,position, this)
        requireImageLoader().load(binding.videoView, item, this)
    }
}