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

import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.ActivityResultRegistry
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import java.io.File

abstract class TakePhotoLifecycleObserver(
    private val registry: ActivityResultRegistry,
    private val registerKey: String = "TakePhoto"
) :
    DefaultLifecycleObserver {
    private lateinit var takePhotoLauncher: ActivityResultLauncher<TakePhotoRequest>

    override fun onCreate(owner: LifecycleOwner) {
        takePhotoLauncher =
            registry.register("${registerKey}_TakePhoto", owner, TakePhotoContract(), {
                onResult(it.resultCode)
            })
    }

    fun launch(file: File) {
        try {
            takePhotoLauncher.launch(TakePhotoRequest(file))
        } catch (_: Exception) {

        }
    }

    abstract fun onResult(resultCode: Int)
}

abstract class RecordVideoLifecycleObserver(private val registry: ActivityResultRegistry) :
    DefaultLifecycleObserver {
    private lateinit var recordVideoLauncher: ActivityResultLauncher<RecordVideoRequest>

    override fun onCreate(owner: LifecycleOwner) {
        recordVideoLauncher = registry.register("RecordVideo", owner, RecordVideoContract(), {
            onResult(it)
        })
    }

    fun launch(limitedDuration: Int = CameraConst.DEF_VIDEO_LIMITED_DURATION) {
        try {
            recordVideoLauncher.launch(RecordVideoRequest(limitedDuration = limitedDuration))
        } catch (_: Exception) {

        }
    }

    abstract fun onResult(result: RecordVideoInfo)
}
