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

import edu.prlab.tyler.iotgateway.cloud.pojo.auth.SensitiveUser;
import edu.prlab.tyler.iotgateway.cloud.pojo.device.Device;
import edu.prlab.tyler.iotgateway.cloud.pojo.privacy.PrivacyPolicyReport;
import edu.prlab.tyler.iotgateway.cloud.services.DeviceService;
import edu.prlab.tyler.iotgateway.cloud.services.PrivacyPolicyReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("device")
public class DeviceController {
    private DeviceService deviceService;
    private PrivacyPolicyReportService privacyPolicyReportService;
    @Autowired
    public DeviceController(DeviceService deviceService,
                            PrivacyPolicyReportService privacyPolicyReportService
                            ) {
        this.deviceService = deviceService;
        this.privacyPolicyReportService = privacyPolicyReportService;
    }

    @PostMapping
    public ResponseEntity<Device> addDevice(@RequestBody Device device) {
        return deviceService.add(device)
                .map(ResponseEntity::ok)
                .orElseGet(()->ResponseEntity.noContent().build());
    }

    @GetMapping("/{id}")
    public ResponseEntity<Device> readDevice(@PathVariable(value = "id",required = false) long id) {
        return deviceService.read(id)
                .map(ResponseEntity::ok)
                .orElseGet(()->ResponseEntity.noContent().build());
    }

    @GetMapping
    public ResponseEntity<Iterable<Device>> readDevices() {
        return Optional.of(deviceService.readll())
                .map(ResponseEntity::ok)
                .orElseGet(()->ResponseEntity.noContent().build());
    }

//    @GetMapping
//    public ResponseEntity<Iterable<Device>> readDevices(SensitiveUser user) {
//        return Optional.of(deviceService.readll())
//                .map(ResponseEntity::ok)
//                .orElseGet(()->ResponseEntity.noContent().build());
//    }

    @PostMapping("/privacy")
    public  ResponseEntity<PrivacyPolicyReport> addPrivacyPolicyReport(@RequestBody PrivacyPolicyReport privacyPolicy) {
        return  privacyPolicyReportService.add(privacyPolicy)
                    .map(ResponseEntity::ok)
                    .orElseGet(()->ResponseEntity.noContent().build());
    }


    @GetMapping("/privacy/{UDN}")
    public ResponseEntity<PrivacyPolicyReport> readPrivacyPolicyReportByDevice(@PathVariable(value = "UDN") String UDN) {
        return privacyPolicyReportService.readByDevice(UDN)
                .map(ResponseEntity::ok)
                .orElseGet(()->ResponseEntity.noContent().build());
    }
}
