package com.isidroid.job_sender.data.source.remote

import com.isidroid.job_sender.data.source.remote.api.ApiCommand
import com.isidroid.job_sender.data.source.remote.api.ApiUpload
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.RequestBody.Companion.asRequestBody
import java.io.File
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class SendJobNetworkSource @Inject constructor(
    private val apiUpload: ApiUpload,
    private val apiCommand: ApiCommand,
    private val isMock: Boolean
) {
    suspend fun uploadPage(jobId: String, token: String, filePath: String): Boolean {
//        if (isMock) {
//            delay(5)
//            return true
//        }

        val file = File(filePath)
        val requestBody = file.asRequestBody("application/octet-stream".toMediaType())
        val response = apiUpload.upload(jobId = jobId, token = token, requestBody).execute()
        return response.isSuccessful
    }

    suspend fun complete(jobId: String, token: String): Boolean {
//        if (isMock) {
//            delay(5)
//            return true
//        }

        apiCommand.complete(jobId = jobId, token = token).execute()
        return true
    }
}