package eu.eudat.logic.managers;

import com.fasterxml.jackson.databind.ObjectMapper;
import eu.eudat.data.entities.DatasetProfile;
import eu.eudat.logic.mapper.prefilling.PrefillingMapper;
import eu.eudat.logic.proxy.config.ExternalUrlCriteria;
import eu.eudat.logic.proxy.config.configloaders.ConfigLoader;
import eu.eudat.logic.proxy.config.entities.PrefillingConfig;
import eu.eudat.logic.proxy.config.entities.PrefillingGet;
import eu.eudat.logic.services.ApiContext;
import eu.eudat.models.data.datasetwizard.DatasetWizardModel;
import eu.eudat.models.data.prefilling.Prefilling;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class PrefillingManager {

    private final ApiContext apiContext;
    private final ConfigLoader configLoader;
    private final ObjectMapper objectMapper;
    private final DatasetManager datasetManager;
    private final LicenseManager licenseManager;

    @Autowired
    public PrefillingManager(ApiContext apiContext, ConfigLoader configLoader, DatasetManager datasetManager, LicenseManager licenseManager) {
        this.apiContext = apiContext;
        this.configLoader = configLoader;
        this.objectMapper = new ObjectMapper();
        this.datasetManager = datasetManager;
        this.licenseManager = licenseManager;
    }

    public List<Prefilling> getPrefillings(String like, String configId) {
        PrefillingConfig prefillingConfig = configLoader.getExternalUrls().getPrefillings().get(configId);
        ExternalUrlCriteria externalUrlCriteria = new ExternalUrlCriteria();
        externalUrlCriteria.setLike(like);
        List<Map<String, String>> map = apiContext.getOperationsContext().getRemoteFetcher().getExternalGeneric(externalUrlCriteria, prefillingConfig.getPrefillingSearch());
        return map.stream().map(submap -> objectMapper.convertValue(submap, Prefilling.class)).collect(Collectors.toList());
    }

    public DatasetWizardModel getPrefilledDataset(String prefillId, String configId, UUID profileId) throws Exception {
        PrefillingConfig prefillingConfig = configLoader.getExternalUrls().getPrefillings().get(configId);
        PrefillingGet prefillingGet = prefillingConfig.getPrefillingGet();
        Map<String, Object> prefillingEntity = getSingle(prefillingGet.getUrl(), prefillId);
        DatasetProfile datasetProfile = apiContext.getOperationsContext().getDatabaseRepository().getDatasetProfileDao().find(profileId);
        return PrefillingMapper.mapPrefilledEntityToDatasetWizard(prefillingEntity, prefillingGet, prefillingConfig.getType(), datasetProfile, datasetManager, licenseManager);
    }

    private Map<String, Object> getSingle(String url, String id) {
        RestTemplate restTemplate = new RestTemplate();
        String parsedUrl = url.replace("{id}", id);
        HttpHeaders headers = new HttpHeaders();
        headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
        HttpEntity<String> entity = new HttpEntity("", headers);

        return restTemplate.exchange(parsedUrl, HttpMethod.GET, entity, LinkedHashMap.class).getBody();
    }
}
