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

import com.adealink.frame.base.CommonIOError
import com.adealink.frame.base.IError
import com.adealink.frame.base.Rlt
import com.adealink.frame.coroutine.dispatcher.Dispatcher
import com.adealink.frame.storage.file.videoProgressPath
import kotlinx.coroutines.withContext
import java.io.File

suspend fun processFastStart(videoPath: String): Rlt<String> {
    return withContext(Dispatcher.WENEXT_THREAD_POOL) {
        val inputFile = File(videoPath)
        val outputFile = File(
            videoProgressPath,
            "${System.currentTimeMillis()}_faststart.mp4"
        )
        try {
            QtFastStart.fastStart(inputFile, outputFile)
        } catch (e: Exception) {
            return@withContext Rlt.Failed(CommonIOError(e))
        }

        if (outputFile.length() == 0L) {
            return@withContext Rlt.Failed(IError("processFastStart fail"))
        }

        return@withContext Rlt.Success(outputFile.path)
    }
}