package com.adealink.weparty.profile.ui.photowall.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.updateLayoutParams
import com.adealink.frame.image.view.NetworkImageView
import com.adealink.frame.util.onClick
import com.adealink.frame.util.runOnUiThread
import com.adealink.weparty.commonui.ext.isViewValid
import com.adealink.weparty.commonui.recycleview.adapter.BindingViewHolder
import com.adealink.weparty.commonui.recycleview.adapter.multitype.ItemViewBinder
import com.adealink.weparty.profile.databinding.LayoutPhotoWallItemBinding
import com.adealink.weparty.profile.ui.photowall.data.PhotoItemData
import com.facebook.imagepipeline.image.ImageInfo

class PhotoItemViewBinder(
    val onClick: (item: PhotoItemData, position: Int) -> Unit
) : ItemViewBinder<PhotoItemData, PhotoItemViewBinder.ViewHolder>() {

    override fun onCreateViewHolder(
        inflater: LayoutInflater,
        parent: ViewGroup,
    ): ViewHolder {
        return ViewHolder(LayoutPhotoWallItemBinding.inflate(inflater, parent, false))
    }

    override fun onBindViewHolder(
        holder: ViewHolder,
        item: PhotoItemData
    ) {
        holder.binding.root.onClick {
            onClick.invoke(item, holder.layoutPosition)
        }
        holder.update(item)
    }

    class ViewHolder(
        rootBinding: LayoutPhotoWallItemBinding,
    ) : BindingViewHolder<LayoutPhotoWallItemBinding>(rootBinding) {

        private var item: PhotoItemData? = null

        inner class IImageLoadResult(
            val item: PhotoItemData,
            val onSuccessResult: (imageInfo: ImageInfo?) -> Unit
        ) : NetworkImageView.IImageLoadResultListener {
            override fun onFailed() {

            }

            override fun onSuccess(imageInfo: ImageInfo?) {
                if (this@ViewHolder.item != item) {
                    return
                }
                item.photoWidth = imageInfo?.width
                item.photoHeight = imageInfo?.height
                onSuccessResult.invoke(imageInfo)
            }

        }

        fun update(item: PhotoItemData) {
            this.item = item
            val photoRatio = item.getPhotoRatio()
            val imageView = binding.img
            if (photoRatio.isNullOrEmpty()) {
                //图片需要测量
                imageView.setImageUrl(
                    item.data.url,
                    IImageLoadResult(item) { imageInfo ->
                        runOnUiThread {
                            if (isViewValid()) {
                                updateImageViewLp(
                                    imageView,
                                    imageInfo?.width,
                                    imageInfo?.height
                                )
                            }
                        }
                    })
            } else {
                //图片已经测量过
                imageView.setImageLoadResultListener(null)
                imageView.setImageUrl(item.data.url)
                imageView.updateLayoutParams<ConstraintLayout.LayoutParams> {
                    dimensionRatio = photoRatio
                }
            }
        }

        private fun updateImageViewLp(
            imageView: NetworkImageView,
            imageWidth: Int?,
            imageHeight: Int?
        ) {
            val width = imageWidth ?: 1
            val height = imageHeight ?: 1
            val imageWidth = width.coerceIn(1, width)
            val imageHeight = height.coerceIn(1, height)
            imageView.updateLayoutParams<ConstraintLayout.LayoutParams> {
                dimensionRatio = "${imageWidth}:${imageHeight}"
            }
        }

    }
}