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

import androidx.lifecycle.LiveData
import com.adealink.frame.coroutine.dispatcher.Dispatcher
import com.adealink.frame.mvvm.livedata.OnceMutableLiveData
import com.adealink.frame.mvvm.viewmodel.BaseViewModel
import com.adealink.frame.util.ImageUtil
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class ImageProcessorViewModel : BaseViewModel() {
    fun processImage(
        originFilePath: String,
        processorList: MutableList<ImageProcessor>,
    ): LiveData<ImageProcessResult> {
        val liveData = OnceMutableLiveData<ImageProcessResult>()
        viewModelScope.launch {
            val processPathList = mutableListOf<String>()
            withContext(Dispatcher.WENEXT_THREAD_POOL) {
                var filePath = originFilePath
                processorList.forEach { processor ->
                    filePath = processor.process(filePath)
                    processPathList.add(filePath)
                }
                val pictureSize = ImageUtil.getPictureSize(filePath)
                liveData.send(ImageProcessResult(filePath,
                    pictureSize.first,
                    pictureSize.second,
                    processPathList))
            }
        }
        return liveData
    }
}