package com.adealink.weparty.imageselect.selectpreview

import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.ActivityResultRegistry
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner

abstract class SelectPreviewLifecycleObserver(private val registry: ActivityResultRegistry) :
    DefaultLifecycleObserver {
    private lateinit var selectPreviewLauncher: ActivityResultLauncher<SelectPreviewRequest>

    override fun onCreate(owner: LifecycleOwner) {
        selectPreviewLauncher = registry.register("SelectPreview", owner, SelectPreviewContract()) {
            onResult(it)
        }
    }

    fun launch(
        path: String?,
        uri: String?,
        source: String = "",
        showConfirm: Boolean = true,
        previewType: SelectPreviewType = SelectPreviewType.IMAGE,
    ) {
        selectPreviewLauncher.launch(
            SelectPreviewRequest(
                source, path, uri, previewType, showConfirm
            )
        )
    }

    abstract fun onResult(result: SelectPreviewResult)
}