package edu.prlab.tyler.iotgateway.gateway.services;

import edu.prlab.tyler.iotgateway.cloud.pojo.privacy.PrivacyChoice;
import edu.prlab.tyler.iotgateway.cloud.pojo.privacy.PrivacyPolicyReport;
import edu.prlab.tyler.iotgateway.gateway.pojo.PrivacyChoiceIndex;
import edu.prlab.tyler.iotgateway.gateway.repositories.PrivacyChoiceIndexRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.time.LocalDateTime;
import java.util.Optional;

@Service
public class PrivacyService {
    private RestTemplate template;
    private PrivacyChoiceIndexRepository privacyChoiceIndexRepository;

    private String cloudAddress;

    @Autowired
    public PrivacyService(PrivacyChoiceIndexRepository privacyChoiceIndexRepository,
                          RestTemplate template) {
        this.template = template;
        this.privacyChoiceIndexRepository = privacyChoiceIndexRepository;
    }

    @Value("${iotgateway.cloud.address}")
    private void setCloudAddress(String cloudAddress) {
        this.cloudAddress = cloudAddress;
    }

    public Optional<PrivacyPolicyReport> getRelatedPrivacyPolicies(String udn) {
        return Optional.ofNullable(template.getForObject(cloudAddress + "/privacy/" + udn, PrivacyPolicyReport.class));
    }

    public Optional<PrivacyChoice> setPrivacyPolicyChoice(PrivacyChoice privacyChoice) {
        return Optional.of(privacyChoice)
                .map(choice -> privacyChoiceIndexRepository.save(PrivacyChoiceIndex.builder()
                        .localDateTime(LocalDateTime.now())
                        .privacyChoice(choice)
                        .build()))
                .map((choice) -> template.postForObject(cloudAddress + "/choice", choice.getPrivacyChoice(), PrivacyChoice.class));
    }

//    public Optional<Iterable<PrivacyChoice>> getPrivacyPolicyChoices() {
//        return Optional.ofNullable(template.exchange(cloudAddress + "/choice", HttpMethod.GET, null,
//                new ParameterizedTypeReference<Iterable<PrivacyChoice>>() { }).getBody());
//    }

    public Iterable<PrivacyChoiceIndex> getPrivacyPolicyChoices() {
        return privacyChoiceIndexRepository.findAll();
    }
}
