package edu.prlab.tyler.iotgateway.cloud.controllers;

import edu.prlab.tyler.iotgateway.cloud.pojo.privacy.PrivacyChoice;
import edu.prlab.tyler.iotgateway.cloud.services.PrivacyChoiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

@RestController
public class GatewayController {
    private PrivacyChoiceService privacyChoiceService;

    @Autowired
    public GatewayController(PrivacyChoiceService privacyChoiceService) {
        this.privacyChoiceService = privacyChoiceService;
    }

    @PostMapping("/choice")
    public ResponseEntity<PrivacyChoice> setPrivacyChoice(@RequestBody PrivacyChoice privacyChoice) {
        return privacyChoiceService.add(privacyChoice)
                .map(ResponseEntity::ok)
                .orElseGet(() -> ResponseEntity.noContent().build());
    }

    @GetMapping("/choice")
    public ResponseEntity<Iterable<PrivacyChoice>> readPrivacyChoice() {
        return Optional.of(privacyChoiceService.readll())
                .map(ResponseEntity::ok)
                .orElseGet(() -> ResponseEntity.noContent().build());
    }

    @GetMapping("/choice/{udn}")
    public ResponseEntity<Iterable<PrivacyChoice>> readPrivacyChoiceByDevice(@PathVariable String udn) {
        return Optional.ofNullable(privacyChoiceService.readPrivacyChoiceByDevice(udn))
                .map(ResponseEntity::ok)
                .orElseGet(() -> ResponseEntity.noContent().build());
    }
}
