package com.adealink.weparty.profile.dynamic.comp

import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.updateLayoutParams
import androidx.lifecycle.LifecycleOwner
import com.adealink.frame.aab.util.getCompatDimensionPixelSize
import com.adealink.frame.mvvm.viewmodel.viewModels
import com.adealink.frame.util.ImageUtil
import com.adealink.frame.util.onClick
import com.adealink.weparty.commonui.BaseViewComponent
import com.adealink.weparty.commonui.mediapicker.videoselect.preview.goVideoPreview
import com.adealink.weparty.module.image.data.VideoData
import com.adealink.weparty.profile.R
import com.adealink.weparty.profile.databinding.ItemPublishDynamicVideoBinding
import com.adealink.weparty.profile.dynamic.util.updateMediaViewLp
import com.adealink.weparty.profile.dynamic.util.updatePhotoMediaViewLp
import com.adealink.weparty.profile.dynamic.viewmodel.PublishDynamicViewModel
import com.adealink.weparty.profile.edit.data.VideoItemData
import com.adealink.weparty.profile.viewmodel.ProfileViewModelFactory

class PublishDynamicVideoComp(
    lifecycleOwner: LifecycleOwner,
    val binding: ItemPublishDynamicVideoBinding,
    val onDelete: () -> Unit
) : BaseViewComponent(lifecycleOwner) {

    private val publishViewModel by viewModels<PublishDynamicViewModel> { ProfileViewModelFactory() }

    override fun observeViewModel() {
        super.observeViewModel()
        publishViewModel.mediaItemListLD.observe(this) { itemList ->
            if (publishViewModel.isVideoDynamic()) {
                setVideo((itemList.firstOrNull() as? VideoItemData)?.data)
            }
        }
    }

    private fun setVideo(video: VideoData?) {
        video ?: return
        binding.root.onClick {
            previewVideo(video)
        }
        binding.ivDelete.onClick {
            onDelete.invoke()
        }

        if (video.cover != null && !video.isNull()) {
            updatePhotoMediaViewLp(
                binding.ivCover, video.cover,
            )
            if (video.cover?.isRemote() == true) {
                binding.ivCover.setImageUrl(video.url)
            } else {
                ImageUtil.getPictureSize(video.uri)
                binding.ivCover.setImageURI(video.uri)
            }
        } else {
            updateMediaViewLp(
                binding.ivCover, null, null
            )
            binding.ivCover.updateLayoutParams<ConstraintLayout.LayoutParams> {
                this.width =
                    getCompatDimensionPixelSize(R.dimen.dynamic_view_horizontal_width)
            }
            if (video.isRemote()) {
                binding.ivCover.setImageUrl(video.url)
            } else {
                binding.ivCover.setImageURI(video.uri)
            }
        }

        binding.vUploadProgress.observeProgress(viewLifecycleOwner, video.mediaUpload)
    }

    private fun previewVideo(video: VideoData?) {
        val act = activity ?: return
        video ?: return
        if (video.path.isNullOrEmpty()) {
            return
        }
        goVideoPreview(act, video.path ?: "")
    }

}