package org.takiguchi.cerberus.cerberusapp.controller;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.takiguchi.cerberus.cerberusapp.model.Application;
import org.takiguchi.cerberus.cerberusapp.model.ApplicationStatus;
import org.takiguchi.cerberus.cerberusapp.model.ServiceStatus;
import org.takiguchi.cerberus.cerberusapp.service.ApplicationService;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

import static org.takiguchi.cerberus.cerberusapp.model.Application.anApplication;

@RestController
@RequestMapping("/api/applications")
public class ApplicationController {
    private final ApplicationService service;

    public ApplicationController(ApplicationService service) {
        this.service = service;
    }

    @GetMapping("/{applicationId}")
    public Optional<Application> getById(@PathVariable("applicationId") UUID applicationId) {
        return service.getById(applicationId);
    }

    @GetMapping
    public List<Application> getAll() {
        return service.getAll();
    }

    @PostMapping
    public Application add(@RequestBody Application application) {
        Application applicationToAdd = anApplication()
                .withName(application.getName())
                .withServiceName(application.getServiceName())
                .withServiceType(application.getServiceType())
                .withImage(application.getImage())
                .build();
        return service.add(applicationToAdd);
    }

    @PutMapping("/{applicationId}")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void update(@PathVariable("applicationId") UUID applicationId, @RequestBody Application application) {
        Application applicationToUpdate = anApplication()
                .withId(applicationId)
                .withName(application.getName())
                .withServiceName(application.getServiceName())
                .withServiceType(application.getServiceType())
                .build();
        service.update(applicationToUpdate);
    }

    @DeleteMapping("/{applicationId}")
    public void remove(@PathVariable("applicationId") UUID applicationId) {
        service.remove(applicationId);
    }

    @GetMapping("/{applicationId}/status")
    public ServiceStatus getStatus(@PathVariable("applicationId") UUID applicationId) {
        return service.checkStatus(applicationId);
    }

    @PostMapping("/{applicationId}/start")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void start(@PathVariable("applicationId") UUID applicationId) {
        service.start(applicationId);
    }

    @PostMapping("/{applicationId}/stop")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void stop(@PathVariable("applicationId") UUID applicationId) {
        service.stop(applicationId);
    }

    @PostMapping("/{applicationId}/restart")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void restart(@PathVariable("applicationId") UUID applicationId) {
        service.restart(applicationId);
    }

    @GetMapping("/status")
    public List<ApplicationStatus> getAllApplicationStatus() {
        return service.checkAllStatus();
    }
}
