package com.adealink.weparty.profile.dynamic.util

import android.view.View
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.updateLayoutParams
import com.adealink.frame.aab.util.getCompatDimensionPixelSize
import com.adealink.frame.image.imageService
import com.adealink.frame.image.listener.ICloseableImageResultListener
import com.adealink.frame.util.ImageUtil
import com.adealink.weparty.commonui.ext.safeAction
import com.adealink.weparty.module.image.data.PhotoData
import com.adealink.weparty.profile.R
import com.facebook.common.references.CloseableReference
import com.facebook.imagepipeline.image.CloseableBitmap
import com.facebook.imagepipeline.image.CloseableImage

fun updateMediaViewLp(
    mediaView: View,
    reqWidth: Int?,
    reqHeight: Int?,

    defaultRatio: String = "3:4",
    viewWidth: Int? = null
) {
    if (reqWidth == null || reqHeight == null) {
        mediaView.updateLayoutParams<ConstraintLayout.LayoutParams> {
            dimensionRatio = defaultRatio
        }
        return
    }
    val imageWidth = reqWidth.coerceIn(1, reqWidth)
    val imageHeight = reqHeight.coerceIn(1, reqHeight)
    mediaView.updateLayoutParams<ConstraintLayout.LayoutParams> {
        //宽高比小于3:4(长图,需要固定)
        dimensionRatio = if (imageWidth * 1f / imageHeight < 0.75f) {
            defaultRatio
        } else {
            "${imageWidth}:${imageHeight}"
        }
        viewWidth?.let {
            width = it
        }
    }
}


/**
 * mediaView 父布局必须是 ConstraintLayout
 */
fun updatePhotoMediaViewLp(
    mediaView: View,
    photoData: PhotoData?,
    defaultRatio: String = "3:4",
    horizontalWidth: Int? = getCompatDimensionPixelSize(R.dimen.dynamic_view_horizontal_width),
    verticalWidth: Int? = getCompatDimensionPixelSize(R.dimen.dynamic_view_vertical_width),
) {
    if (photoData == null || photoData.isNull()) {
        mediaView.updateLayoutParams<ConstraintLayout.LayoutParams> {
            dimensionRatio = defaultRatio
            horizontalWidth?.let {
                width = horizontalWidth
            }
        }
        return
    }
    var photoWidth = photoData.width
    var photoHeight = photoData.height
    if (photoWidth != null && photoHeight != null) {
        if (photoWidth > photoHeight) {
            updateMediaViewLp(
                mediaView, photoWidth, photoHeight, defaultRatio, viewWidth = horizontalWidth
            )
        } else {
            updateMediaViewLp(
                mediaView, photoWidth, photoHeight, defaultRatio, viewWidth = verticalWidth
            )
        }
        return
    }
    if (photoData.isLocal()) {
        val photoSize = ImageUtil.getPictureSize(photoData.path)
        photoWidth = photoSize.first
        photoHeight = photoSize.second
        if (photoWidth > 0 && photoHeight > 0) {
            photoData.width = photoWidth
            photoData.height = photoHeight
            if (photoWidth > photoHeight) {
                updateMediaViewLp(
                    mediaView, photoWidth, photoHeight, defaultRatio, horizontalWidth
                )
            } else {
                updateMediaViewLp(
                    mediaView, photoWidth, photoHeight, defaultRatio, verticalWidth
                )
            }
        }
        return
    }

    val remoteUrl = photoData.url
    if (photoData.isRemote() && !remoteUrl.isNullOrEmpty()) {
        imageService.fetchImage(remoteUrl, object : ICloseableImageResultListener {
            override fun onSuccess(reference: CloseableReference<CloseableImage>) {
                super.onSuccess(reference)
                val closeableBitmap = reference.get()
                if (closeableBitmap is CloseableBitmap) {
                    val bitmap = closeableBitmap.underlyingBitmap
                    photoWidth = bitmap.width
                    photoHeight = bitmap.height
                    if (photoWidth > 0 && photoHeight > 0) {
                        photoData.width = photoWidth
                        photoData.height = photoHeight
                        mediaView.safeAction {
                            if (photoWidth > photoHeight) {
                                updateMediaViewLp(
                                    mediaView, photoWidth, photoHeight, defaultRatio, horizontalWidth
                                )
                            } else {
                                updateMediaViewLp(
                                    mediaView, photoWidth, photoHeight, defaultRatio, verticalWidth
                                )
                            }
                        }
                    }
                }
            }
        })
    }

}