package space.nstart.pcp.slots_service.controller

import org.springframework.stereotype.Controller
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.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.ResponseBody
import org.springframework.web.bind.annotation.RestController
import space.nstart.pcp.pcp_types_lib.dto.ballistics.AngularMotionCalculationRequestDTO
import space.nstart.pcp.slots_service.dto.dynamicplan.DynamicPlanCalculationRequestDTO
import space.nstart.pcp.slots_service.service.DynamicPlanService
import java.time.LocalDateTime
import java.util.UUID

@Controller
class DynamicPlanPageController {

    @GetMapping("/dynamic-plan")
    fun dynamicPlanPage(): String = "dynamic-plan"
}


@Controller
class AngularMotionPageController {

    @GetMapping("/angular-motion")
    fun angularMotionPage(): String = "angular-motion"
}

@RestController
@RequestMapping("/api/dynamic-plan")
class DynamicPlanRestController(
    private val dynamicPlanService: DynamicPlanService
) {

    @PostMapping("/calc")
    @ResponseBody
    fun calculate(@RequestBody request: DynamicPlanCalculationRequestDTO) =
        dynamicPlanService.calculate(request)

    @GetMapping("/runs")
    @ResponseBody
    fun getRuns(
        @RequestParam(required = false) status: String?,
        @RequestParam(defaultValue = "50") limit: Int,
        @RequestParam(defaultValue = "0") offset: Int
    ) = dynamicPlanService.getRuns(status, limit, offset)

    @GetMapping("/runs/{runId}")
    @ResponseBody
    fun getRun(@PathVariable runId: UUID) =
        dynamicPlanService.getRun(runId)

    @GetMapping("/runs/{runId}/result")
    @ResponseBody
    fun getResult(@PathVariable runId: UUID) =
        dynamicPlanService.getResult(runId)

    @GetMapping("/runs/{runId}/routes")
    @ResponseBody
    fun getRoutes(
        @PathVariable runId: UUID,
        @RequestParam(defaultValue = "1000") limit: Int,
        @RequestParam(defaultValue = "0") offset: Int
    ) = dynamicPlanService.getRoutes(runId, limit, offset)

    @GetMapping("/runs/{runId}/intervals")
    @ResponseBody
    fun getIntervals(
        @PathVariable runId: UUID,
        @RequestParam(defaultValue = "1000") limit: Int,
        @RequestParam(defaultValue = "0") offset: Int
    ) = dynamicPlanService.getIntervals(runId, limit, offset)
}


@RestController
@RequestMapping("/api/angular-motion")
class AngularMotionRestController(
    private val dynamicPlanService: DynamicPlanService
) {
    @GetMapping("/satellites")
    @ResponseBody
    fun satellites(@RequestParam(required = false) time: LocalDateTime?) =
        dynamicPlanService.getAngularMotionSatellites(time)

    @GetMapping("/satellites/{satelliteId}/flight-line")
    @ResponseBody
    fun flightLine(
        @PathVariable satelliteId: Long,
        @RequestParam time: LocalDateTime
    ) = dynamicPlanService.getAngularMotionFlightLine(satelliteId, time)

    @PostMapping("/calculate")
    @ResponseBody
    fun calculate(@RequestBody request: AngularMotionCalculationRequestDTO) =
        dynamicPlanService.calculateAngularMotion(request)
}
