package space.nstart.pcp.slots_service.rest

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController
import space.nstart.pcp.pcp_types_lib.dto.ballistics.RevolutionSign
import space.nstart.pcp.slots_service.service.CZMLService
import space.nstart.pcp.slots_service.service.CoverageSchemeService
import java.time.LocalDateTime


@RestController
@RequestMapping("/requests")
class RequestRestController {
    @Autowired
    private lateinit var czmlService: CZMLService

    @Autowired
    private lateinit var coverageSchemeService: CoverageSchemeService

    @PostMapping()
    fun test (
        @RequestParam importance: Double,
        @RequestParam(required = false) countLat: Int?,
        @RequestParam(required = false) countLong: Int?,
    ) =
        czmlService.drawHeatMap(importance, countLat, countLong)
        //czmlService.showWithCells()

    @PostMapping("/req-with-cells/{id}")
    fun reqWithCells(@PathVariable("id") id : String) =
        czmlService.showWithCells(id)

    @PostMapping("/plan/{satellite_id}")
    fun plan(@PathVariable("satellite_id") id : Long,
             @RequestParam tn : LocalDateTime,
             @RequestParam tk : LocalDateTime,
             @RequestParam(required = false) runId: Long?) =
        czmlService.showSatPlan(id, tn, tk, runId)

    @GetMapping("/plan/{satellite_id}")
    fun planGet(
        @PathVariable("satellite_id") id: Long,
        @RequestParam tn: LocalDateTime,
        @RequestParam tk: LocalDateTime,
        @RequestParam(required = false) runId: Long?
    ) = czmlService.showSatPlan(id, tn, tk, runId)


    @PostMapping("/sat/{satellite_id}")
    fun sat(
            @PathVariable("satellite_id") id : Long,
            @RequestParam view : Boolean,
            @RequestParam orbit : Boolean,
            @RequestParam sw : Boolean,
            @RequestParam fl : Boolean,
            @RequestParam(required = false) tn : LocalDateTime?,
            @RequestParam(required = false) tk : LocalDateTime?) =

        czmlService.satFLD(id, view, orbit, sw, fl, tn, tk)


    @PostMapping("/station/{station_id}")
    fun sat(
        @PathVariable("station_id") id : String,
        @RequestParam view : Boolean,
        @RequestParam(required = false) orbitHeightKm : Double?
    ) = czmlService.station(id, view, orbitHeightKm)



    @PostMapping("req-with-covs/{request_id}")
    fun covs(
        @PathVariable("request_id") id : String,
        @RequestParam tn : LocalDateTime,
        @RequestParam tk : LocalDateTime,
        @RequestParam sign : RevolutionSign?,
        @RequestParam cov : Boolean?,
        @RequestParam(required = false) sun : Double?,
        @RequestParam(name = "satellites", required = false) satellites : List<Long>?,
        @RequestParam(name = "satellites[]", required = false) satellitesArray : List<Long>?
    ) = czmlService.covs(
        id,
        tn,
        tk,
        sign,
        cov,
        sun,
        satellites?.takeIf { it.isNotEmpty() } ?: satellitesArray?.takeIf { it.isNotEmpty() }
    )

    @PostMapping("req-with-coverage-scheme/{request_id}")
    fun coverageScheme(
        @PathVariable("request_id") id: String,
        @RequestParam tn: LocalDateTime,
        @RequestParam tk: LocalDateTime,
        @RequestParam(name = "satellites", required = false) satellites: List<Long>?,
        @RequestParam(name = "satellites[]", required = false) satellitesArray: List<Long>?,
        @RequestParam(required = false) rollStepDegrees: Double?,
        @RequestParam(required = false) minimumTechnologyOverlap: Double?,
        @RequestParam(required = false) strictContinuousCoverage: Boolean?,
        @RequestParam(required = false) allowPartialCoverage: Boolean?,
        @RequestParam(required = false) orientationToleranceDegrees: Double?,
        @RequestParam(required = false) includeDebugInfo: Boolean?
    ) = coverageSchemeService.requestCoverageScheme(
        id,
        tn,
        tk,
        satellites?.takeIf { it.isNotEmpty() } ?: satellitesArray?.takeIf { it.isNotEmpty() } ?: emptyList(),
        rollStepDegrees,
        minimumTechnologyOverlap,
        strictContinuousCoverage,
        allowPartialCoverage,
        orientationToleranceDegrees,
        includeDebugInfo
    )

}
