package com.adealink.weparty.commonui.mediapicker.imageselect.comp

import androidx.lifecycle.LifecycleOwner
import com.adealink.frame.base.fastLazy
import com.adealink.frame.mvvm.view.ViewComponent
import com.adealink.weparty.commonui.mediapicker.imageselect.SelectImageLifecycleObserver
import com.adealink.weparty.commonui.mediapicker.imageselect.clip.ClipParamData
import com.adealink.weparty.commonui.mediapicker.imageselect.model.LocalMedia
import com.adealink.weparty.commonui.mediapicker.imageselect.model.MediaType
import com.adealink.weparty.commonui.mediapicker.videoselect.SelectVideoLifecycleObserver

class TakeFromAlbumComp(
    lifecycleOwner: LifecycleOwner,
    val clipParamData: ClipParamData?,
    val onSelect: (path: String?, uri: String?) -> Unit,
    val mediaType: MediaType = MediaType.IMAGE,
    val source: String = ""
) : ViewComponent(lifecycleOwner) {
    private val selectImageObserver: SelectImageLifecycleObserver by fastLazy {
        object : SelectImageLifecycleObserver(
            requireActivity().activityResultRegistry,
            registerKey = "selectImage_${this.source}"
        ) {

            override fun onResult(
                source: String,
                resultCode: Int,
                path: String?,
                uri: String?,
                mediaType: MediaType,
                mediaInfos: List<LocalMedia>?,
                fromPosition: Int?
            ) {
                if (this@TakeFromAlbumComp.source != source) {
                    return
                }
                if (path.isNullOrEmpty() || uri.isNullOrEmpty()) {
                    return
                }
                onSelect.invoke(path, uri)
            }

        }
    }

    private val selectVideoObserver: SelectVideoLifecycleObserver by fastLazy {
        object : SelectVideoLifecycleObserver(
            requireActivity().activityResultRegistry,
            registerKey = "selectVideo_${this.source}"
        ) {

            override fun onResult(
                source: String,
                resultCode: Int,
                path: String?,
                uri: String?
            ) {
                if (this@TakeFromAlbumComp.source != source) {
                    return
                }
                if (path.isNullOrEmpty() || uri.isNullOrEmpty()) {
                    return
                }
                onSelect.invoke(path, uri)
            }

        }
    }

    override fun onCreate() {
        super.onCreate()
        lifecycle.addObserver(selectImageObserver)
        lifecycle.addObserver(selectVideoObserver)
    }

    fun takeFromAlbum() {
        when (mediaType) {
            MediaType.IMAGE -> {
                selectImageObserver.launch(
                    clipParamData = clipParamData, gifOpt = false, source = source
                )
            }

            MediaType.VIDEO -> {
                selectVideoObserver.launch(
                    source = source
                )
            }

            MediaType.ALL -> {

            }
        }
    }
}