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.model.PrivacyChoiceResponse;
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.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

@Service
public class PrivacyService {
    private PrivacyChoiceIndexRepository privacyChoiceIndexRepository;
    private RemoteService remoteService;

    @Autowired
    public PrivacyService(PrivacyChoiceIndexRepository privacyChoiceIndexRepository, RemoteService remoteService) {
        this.privacyChoiceIndexRepository = privacyChoiceIndexRepository;
        this.remoteService = remoteService;
    }

    public Optional<PrivacyPolicyReport> getRelatedPrivacyPolicies(String udn) {
        return remoteService.readRelatedPrivacyPolicies(udn);
    }

    public Optional<PrivacyChoiceResponse> setPrivacyChoice(PrivacyChoice privacyChoice) {
        System.out.println("setPrivacyPolicyChoice : " + privacyChoice.toString());
        return Optional.of(privacyChoice)
                .flatMap(choice -> remoteService.setPrivacyChoice(choice))
                .flatMap(choice -> Optional.of(privacyChoiceIndexRepository.save(PrivacyChoiceIndex.builder()
                        .id(choice.getId())
                        .localDateTime(LocalDateTime.now())
                        .build()))
                        .map(choiceIndex -> PrivacyChoiceResponse.builder()
                                .id(choice.getId())
                                .localDateTime(LocalDateTime.now())
                                .privacyChoice(choice)
                                .build()));
    }

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

    public Optional<Iterable<PrivacyChoiceResponse>> getPrivacyPolicyChoices() {
        return remoteService.readPrivacyChoices()
                .map(choices -> StreamSupport.stream(choices.spliterator(), false)
                        .map(choice -> privacyChoiceIndexRepository.findById(choice.getId())
                                .map(choiceIndex -> PrivacyChoiceResponse.builder()
                                        .id(choiceIndex.getId())
                                        .localDateTime(choiceIndex.getLocalDateTime())
                                        .privacyChoice(choice)
                                        .build())
                                .orElseGet(() -> new PrivacyChoiceResponse()))
                        .collect(Collectors.toList()));
    }
}
