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

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView
import com.remax.visualnovel.R
import com.remax.visualnovel.databinding.ItemImageviewerSubsamplingBinding
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.SubsamplingScaleImageView2

class SubsamplingViewHolder(
    parent: ViewGroup,
    callback: ImageViewerAdapterListener,
    val binding: ItemImageviewerSubsamplingBinding =
        ItemImageviewerSubsamplingBinding.inflate(LayoutInflater.from(parent.context), parent, false)
) : RecyclerView.ViewHolder(binding.root) {
    init {
        binding.subsamplingView.setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_START)
        binding.subsamplingView.setListener(object : SubsamplingScaleImageView2.Listener {
            override fun onDrag(view: SubsamplingScaleImageView2, fraction: Float) = callback.onDrag(this@SubsamplingViewHolder, view, fraction)
            override fun onRestore(view: SubsamplingScaleImageView2, fraction: Float) = callback.onRestore(this@SubsamplingViewHolder, view, fraction)
            override fun onRelease(view: SubsamplingScaleImageView2) = callback.onRelease(this@SubsamplingViewHolder, view)
        })
        requireVHCustomizer().initialize(ItemType.SUBSAMPLING, this)
    }

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


