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.ItemImageviewerPhotoBinding
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.PhotoView2

class PhotoViewHolder(
    parent: ViewGroup,
    callback: ImageViewerAdapterListener,
    val binding: ItemImageviewerPhotoBinding =
        ItemImageviewerPhotoBinding.inflate(LayoutInflater.from(parent.context), parent, false)
) : RecyclerView.ViewHolder(binding.root) {
    init {
        binding.photoView.setListener(object : PhotoView2.Listener {
            override fun onDrag(view: PhotoView2, fraction: Float) = callback.onDrag(this@PhotoViewHolder, view, fraction)
            override fun onRestore(view: PhotoView2, fraction: Float) = callback.onRestore(this@PhotoViewHolder, view, fraction)
            override fun onRelease(view: PhotoView2) = callback.onRelease(this@PhotoViewHolder, view)
        })
        requireVHCustomizer().initialize(ItemType.PHOTO, this)
    }

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