package cn.sagacloud.server.algorithm.services.modelCheck

import cn.sagacloud.server.algorithm.backstage.model.CreateRequest
import cn.sagacloud.server.algorithm.backstage.model.CreateResponse
import cn.sagacloud.server.algorithm.backstage.model.QueryRequest
import cn.sagacloud.server.algorithm.backstage.model.QueryResponse
import cn.sagacloud.server.algorithm.models.config.MyProperties
import cn.sagacloud.server.algorithm.models.modelFile.DownloadEntity
import cn.sagacloud.server.algorithm.models.modelFile.TaskEntity
import cn.sagacloud.server.algorithm.models.modelFile.TaskResultEntity
import cn.sagacloud.server.algorithm.services.http.HttpService
import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.TypeReference
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component

/**
 *
 *
 * @author jxing
 */
@Component
class TaskRequest {
    @Autowired
    lateinit var properties: MyProperties

    fun createTask(request : CreateRequest<TaskEntity>) : CreateResponse<TaskEntity>?{
        var postBody = JSON.toJSONString(request)
        val resultStr = HttpService.sendPost(properties.scheduler.createTask, postBody)
        return JSON.parseObject(resultStr, object : TypeReference<CreateResponse<TaskEntity>>() { })
    }

    fun createDownload(request : CreateRequest<DownloadEntity>) : CreateResponse<DownloadEntity> {
        var postBody = JSON.toJSONString(request)
        val resultStr = HttpService.sendPost(properties.scheduler.createDownload, postBody)
        return JSON.parseObject(resultStr, object : TypeReference<CreateResponse<DownloadEntity>>() { })
    }

    fun queryTask(request : QueryRequest) : QueryResponse<TaskEntity> {
        var postBody = JSON.toJSONString(request)
        val resultStr = HttpService.sendPost(properties.scheduler.queryTask, postBody)
        return JSON.parseObject(resultStr, object : TypeReference<QueryResponse<TaskEntity>>() { })
    }

    fun queryTaskResult(request : QueryRequest) : QueryResponse<TaskResultEntity> {
        var postBody = JSON.toJSONString(request)
        val resultStr = HttpService.sendPost(properties.scheduler.queryTaskResult, postBody)
        return JSON.parseObject(resultStr, object : TypeReference<QueryResponse<TaskResultEntity>>() { })
    }
}