package at.rs.alexa.me.opendata.services.population.populationdataprovider;


import at.rs.alexa.me.opendata.model.PopulationData;
import at.rs.alexa.me.opendata.services.population.PopulationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

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

@Service
public class DataGVLowerAustriaPopulationDataProvider implements PopulationDataProvider {

    private static final Logger LOGGER = LoggerFactory.getLogger(PopulationService.class);

    private static final String LOWER_AUSTRIA_DATA_URL = "http://open-data.noe.gv.at/RU2/noe_pop_1869-2011_lau2.csv";

    @Override
    public List<PopulationData> getPopulationData() {
        RestTemplate restTemplate = new RestTemplate();
        HttpEntity<String> rawData = restTemplate.getForEntity(LOWER_AUSTRIA_DATA_URL, String.class);

        String[] lines = rawData.getBody().split("\r\n");

        LOGGER.info(this.getClass() + " + got " + lines.length + " data points");
        List<String> popData = Collections.unmodifiableList(Arrays.asList(lines));

        return popData.stream().map(s -> {

                    String[] result = s.split(";");
                    try {
                        if (result.length >= 4) {
                            PopulationData populationData = new PopulationData();
                            populationData.setCityName(result[4]);
                            populationData.setPopulation(Integer.valueOf(result[6]));
                            return populationData;
                        } else {
                            return null;
                        }
                    } catch (Exception ex) {
                        return null;
                    }
                }
        ).filter(Objects::nonNull).collect(Collectors.toList());

    }
}

@@ -761,16 +761,112 @@
 u2.csv%22;
+%0A    private static final int INDEX_CITY = 4;%0A    private static final int INDEX_POPULATION = 6;
 %0A%0A    @O
@@ -1627,17 +1627,26 @@
 (result%5B
-4
+INDEX_CITY
 %5D);%0A    
@@ -1721,17 +1721,32 @@
 (result%5B
-6
+INDEX_POPULATION
 %5D));%0A   

remove some magic numbers