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

import android.os.Build
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.ActivityResultRegistry
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import com.adealink.frame.util.AppUtil
import com.adealink.frame.util.FileProvider7
import com.adealink.weparty.commonui.mediapicker.imageselect.clip.ClipImageLifecycleObserver
import com.adealink.weparty.commonui.mediapicker.imageselect.clip.ClipParamData
import com.adealink.weparty.commonui.mediapicker.imageselect.clip.MesTipData
import com.adealink.weparty.commonui.mediapicker.imageselect.clip.avatarClipParam
import com.adealink.weparty.commonui.mediapicker.imageselect.model.LocalMedia
import com.adealink.weparty.commonui.mediapicker.imageselect.model.MediaType
import com.adealink.weparty.storage.file.FilePath.clipImagePath
import java.io.File

abstract class SelectImageLifecycleObserver(
    private val registry: ActivityResultRegistry,
    private val registerKey: String = "SelectImage"
) :
    DefaultLifecycleObserver {
    private lateinit var launcher: ActivityResultLauncher<SelectImageRequest>

    private var source: String = ""
    private var mediaType: MediaType = MediaType.ALL

    override fun onCreate(owner: LifecycleOwner) {
        val clipImageObserver =
            object : ClipImageLifecycleObserver(registry, "${registerKey}ClipImage") {
                override fun onResult(resultCode: Int, inputPath: String, outputPath: String) {
                    if (resultCode != AppCompatActivity.RESULT_OK) {
                        return
                    }

                    onResult(
                        source,
                        resultCode,
                        outputPath,
                        FileProvider7.getUriForFile(AppUtil.appContext, File(outputPath))?.toString(),
                        mediaType
                    )
                }
            }
        owner.lifecycle.addObserver(clipImageObserver)
        launcher = registry.register(registerKey, owner, SelectImageContract()) {
            source = it.source
            val resultCode = it.resultCode
            val filePath = it.path
            val uri = it.uri
            val clipParamData = it.clipParamData
            mediaType = it.mediaType
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
                if (mediaType != MediaType.IMAGE) {
                    onResult(source, resultCode, filePath, uri, mediaType, it.mediaInfos,it.fromPosition)
                    return@register
                }

                if (filePath.isNullOrEmpty() || clipParamData == null) {
                    onResult(source, resultCode, filePath, uri, mediaType, it.mediaInfos,it.fromPosition)
                    return@register
                }

                val outputFile = File(clipImagePath, "${System.currentTimeMillis()}.jpg")
                val outputFilePath = outputFile.absolutePath
                clipImageObserver.launch(filePath, outputFilePath, clipParamData)
            } else {
                onResult(source, resultCode, filePath, uri, mediaType, it.mediaInfos, it.fromPosition)
            }
        }
    }

    fun launch(
        source: String = "",
        clipParamData: ClipParamData? = avatarClipParam,
        mediaType: MediaType = MediaType.IMAGE,
        gifOpt: Boolean? = false,
        mesTip: MesTipData? = null,
        maxNum: Int = 1,
        coverExpireTime: Long = 0L,
        fromPosition: Int? = null,
        selectedMediaList: List<LocalMedia>? = null,
        videoMaxSecond: Int? = null,
        imageMinSize : Int? = null, //图片最小尺寸限制,宽度高度
        topTipsString: CharSequence? = null
    ) {
        launcher.launch(
            SelectImageRequest(
                maxNum,
                source,
                clipParamData,
                mediaType,
                gifOpt,
                mesTip,
                coverExpireTime,
                fromPosition,
                selectedMediaList,
                videoMaxSecond,
                imageMinSize,
                topTipsString
            )
        )
    }

    abstract fun onResult(
        source: String,
        resultCode: Int,
        path: String?,
        uri: String?,
        mediaType: MediaType = MediaType.IMAGE,
        mediaInfos: List<LocalMedia>? = null,
        fromPosition: Int? = null
    )

    override fun onDestroy(owner: LifecycleOwner) {
        super.onDestroy(owner)
        launcher.unregister()
    }
}