package com.adealink.weparty.module.widget.form.dialog

import android.view.View
import android.view.ViewGroup
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.updateLayoutParams
import com.adealink.frame.ext.isViewBindingValid
import com.adealink.frame.image.view.NetworkImageView
import com.adealink.frame.mvvm.view.viewBinding
import com.adealink.frame.util.DisplayUtil
import com.adealink.frame.util.onClick
import com.adealink.frame.util.runOnUiThread
import com.adealink.weparty.R
import com.adealink.weparty.commonui.ext.show
import com.adealink.weparty.commonui.widget.BottomDialogFragment
import com.adealink.weparty.databinding.WidgetDialogExampleBinding
import com.adealink.weparty.util.goImagePreviewActivity
import com.adealink.weparty.module.widget.form.data.ExampleType
import com.adealink.weparty.module.widget.form.data.StepFieldExample
import com.facebook.imagepipeline.image.ImageInfo

class ExampleDialog : BottomDialogFragment(R.layout.widget_dialog_example) {

    private val binding by viewBinding(WidgetDialogExampleBinding::bind)

    override val height: Int = DisplayUtil.getScreenHeight() / 2

    private var example: StepFieldExample? = null
    fun setExample(example: StepFieldExample?) {
        this.example = example
    }

    override fun initViews() {
        super.initViews()
        binding.root.onClick {
            dismiss()
        }
        binding.clContent.onClick {
            //Ntd.
        }
        binding.tvTitle.show(example?.title?.isEmpty() == false)
        binding.tvTitle.text = example?.title

        binding.tvDesc.show(example?.desc?.isEmpty() == false)
        binding.tvDesc.text = example?.desc

        when (ExampleType.map(example?.type)) {
            ExampleType.IMAGE -> {
                binding.clImage.show()
                binding.ivImg.setImageUrl(
                    example?.value,
                    object : NetworkImageView.IImageLoadResultListener {
                        override fun onFailed() {
                        }

                        override fun onSuccess(imageInfo: ImageInfo?) {
                            runOnUiThread {
                                if (this@ExampleDialog.isViewBindingValid()) {
                                    updateImageViewLp(
                                        binding.ivImg,
                                        imageInfo?.width,
                                        imageInfo?.height,


                                        binding.clImage,
                                        binding.vImageMask
                                    )

                                }
                            }
                        }
                    })
                binding.ivImg.onClick {
                    goImgPreview(example?.value)
                }
            }

            ExampleType.VOICE -> {
                binding.clSound.show()
                binding.vSound.setSoundUrl(example?.value)
            }

            null -> {

            }
        }
    }

    private fun updateImageViewLp(
        imageView: NetworkImageView,
        imageWidth: Int?,
        imageHeight: Int?,

        imageViewContainer: ViewGroup,
        imageMask: View,
    ) {
        val width = imageWidth ?: 1
        val height = imageHeight ?: 1
        val imageWidth = width.coerceIn(1, width)
        val imageHeight = height.coerceIn(1, height)
        var imageIsBig = false
        imageView.updateLayoutParams<ConstraintLayout.LayoutParams> {
            //宽高比小于1:3(长图,需要固定)
            dimensionRatio = if (imageWidth * 1f / imageHeight < 0.33f) {
                imageIsBig = true
                "1:3"
            } else {
                "${imageWidth}:${imageHeight}"
            }
        }

        if (imageIsBig) {
            imageMask.show()
            return
        }

        imageView.post {
            val parentHeight = imageViewContainer.measuredHeight
            val imageHeight = imageView.measuredHeight
            imageMask.show(parentHeight < imageHeight)
        }
    }

    private fun goImgPreview(url: String?) {
        if (url.isNullOrEmpty()) {
            return
        }
        val act = activity ?: return
        goImagePreviewActivity(
            activity = act,
            imageUriList = arrayListOf<String>().apply {
                add(url)
            },
            currentIndex = 0
        )
    }

}