package space.nstart.pcp.pcp_satellites_service.service

import org.springframework.beans.factory.ObjectProvider
import org.springframework.beans.factory.annotation.Value
import org.springframework.http.MediaType
import org.springframework.stereotype.Service
import org.springframework.web.reactive.function.client.WebClient
import space.nstart.pcp.pcp_types_lib.dto.ballistics.ExactTimePositionRequestDTO
import space.nstart.pcp.pcp_types_lib.dto.ballistics.OrbPointDTO
import space.nstart.pcp.pcp_types_lib.dto.ballistics.ObjViewRequestDTO
import space.nstart.pcp.pcp_types_lib.dto.ballistics.SquareViewParamDTO
import space.nstart.pcp.pcp_types_lib.dto.ballistics.SquareViewParamsWithPointsDTO


@Service
class BallisticsService(webClientBuilderProvider: ObjectProvider<WebClient.Builder>){
    private val webClientBuilder: WebClient.Builder = webClientBuilderProvider.ifAvailable ?: WebClient.builder()

    @Value("\${settings.ballistics-service:ic-service}")
    val url = ""

    private fun client() = webClientBuilder.baseUrl(url).build()


    fun mplSquare(req : ObjViewRequestDTO) =
        client()
            .post()
            .uri("/api/obj-view/mpl-square")
            .contentType(MediaType.APPLICATION_JSON)
            .bodyValue(req)
            .retrieve()
            .bodyToFlux(SquareViewParamDTO::class.java)
            .toIterable()



    fun cellCovering(cell : Long) =
        client()
            .get()
            .uri("/api/obj-view/cell-coverage/{cell}", cell)
            .retrieve()
            .bodyToFlux(SquareViewParamsWithPointsDTO::class.java)
            .toIterable()


    fun exactTime(sat : Long, req : ExactTimePositionRequestDTO) =
        client()
            .post()
            .uri("/api/satellites/{satelliteId}/extract-time", sat)
            .contentType(MediaType.APPLICATION_JSON)
            .bodyValue(req)
            .retrieve()
            .bodyToFlux(OrbPointDTO::class.java)
            .toIterable()


}
