package com.isidroid.job.repository

import com.isidroid.job.CreateJobException
import com.isidroid.job.data.source.local.JobLocalSource
import com.isidroid.job.data.source.remote.JobNetworkSource
import com.isidroid.job.model.PrintJob
import java.io.File

internal class JobRepositoryImpl(
     private val jobNetworkSource: JobNetworkSource,
     private val jobLocalSource: JobLocalSource,
) : JobRepository {
     override suspend fun insert(vararg printJob: PrintJob) = jobLocalSource.insert(*printJob)

     override suspend fun createJob(
        name: String,
        clientName: String,
        spotId: String,
        profileId: String,
        printSize: Int,
        printOrientation: Int,
        copies: Int?,
        sourceFiles: List<File>
    ): PrintJob {
        val job = jobNetworkSource.createJob(spotId, profileId, clientName)
            ?.copy(printSize = printSize, printOrientation = printOrientation, sourceFiles = sourceFiles.map { it.absolutePath })
            ?: throw CreateJobException()

        jobLocalSource.insert(job)
        return job
    }

    override suspend fun readLocalList(spotId: String?, status: Int?, ids: List<String>?) = jobLocalSource.find(spotId = spotId, status = status, ids = ids)
    override suspend fun updateJobStatus(status: Int, vararg ids: String) = jobLocalSource.updateJobStatus(status, *ids)
}