package com.studiowannabe.trafalert.control;

import com.studiowannabe.trafalert.domain.Warning;
import com.studiowannabe.trafalert.domain.WeatherStationData;
import com.studiowannabe.trafalert.wsdl.RoadWeatherResponse;
import com.studiowannabe.trafalert.wsdl.RoadWeatherType;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;

/**
 * Created by Tomi on 31/01/16.
 */
@Component
public class DataFetcher {

    private static Logger log = Logger.getLogger(DataFetcher.class);

    private static final String WEATHER_URL = "http://tie.digitraffic.fi/sujuvuus/ws/roadWeather";

    private final WeatherDataCache cache;
    private final WeatherClient weatherClient;
    private final WarningIssuer warningIssuer;

    @Autowired
    public DataFetcher(final WeatherDataCache cache, final WeatherClient weatherClient, final WarningIssuer warningIssuer) {
        this.cache = cache;
        this.weatherClient = weatherClient;
        this.warningIssuer = warningIssuer;
    }

    @Scheduled(fixedDelay = 60 * 1000) // 30 seconds
    private void runDataUpdate(){
        log.info("Run update...");
        final RoadWeatherResponse response = weatherClient.getRoadWeather();
        if(response != null) {
            updateCache(response);
        }
    }

    private void updateCache(final RoadWeatherResponse response) {
        final HashMap<Long, WeatherStationData> map = new HashMap<>(response.getRoadweatherdata().getRoadweather().size());
        for (RoadWeatherType data : response.getRoadweatherdata().getRoadweather()) {
            final WeatherStationData wsd = new WeatherStationData(data.getAirtemperature1(), data.getRoadsurfacetemperature1(),
                    data.getAveragewindspeed(), data.getMaxwindspeed(), data.getVisibilitymeters(), data.getDewpoint(),
                    data.getRoaddewpointdifference(), data.getHumidity(), data.getWinddirection(), data.getPrecipitation(), data.getRoadsurfaceconditions1(),
                    data.getPrecipitationtype());
            map.put(data.getStationid().longValue(), wsd);

            final List<Warning> warnings = warningIssuer.calculateWarnings(data);
            log.info("Warnings: " + warnings);
        }
        cache.setCacheData(map);
    }
}

@@ -812,24 +812,69 @@
 ache cache;%0A
+    private final WarningCache warningCache;%0A
     private 
@@ -1058,16 +1058,39 @@
 rClient,
+%0A                      
  final W
@@ -1115,16 +1115,49 @@
 ngIssuer
+, final WarningCache warningCache
 ) %7B%0A    
@@ -1268,16 +1268,58 @@
 Issuer;%0A
+        this.warningCache = warningCache;%0A
     %7D%0A%0A 
@@ -1570,16 +1570,17 @@
 ateCache
+s
 (respons
@@ -1588,24 +1588,184 @@
 );%0A        %7D
+%0A%0A        log.info(%22Cached weather stations %22 + cache.getCacheData().size());%0A        log.info(%22Cached warning stations %22 + warningCache.getCacheData().size());
 %0A    %7D%0A%0A    
@@ -1788,16 +1788,17 @@
 ateCache
+s
 (final R
@@ -1944,32 +1944,105 @@
 ther().size());%0A
+        final HashMap%3CLong, List%3CWarning%3E%3E warningMap = new HashMap%3C%3E();%0A
         for (Roa
@@ -2643,38 +2643,55 @@
     
-final List%3CWarning%3E warnings =
+warningMap.put(data.getStationid().longValue(),
  war
@@ -2724,16 +2724,17 @@
 gs(data)
+)
 ;%0A      
@@ -2739,66 +2739,59 @@
     
+%7D%0A
     
-log.info(%22Warnings: %22 + warnings);%0A        %7D%0A        c
+    cache.setCacheData(map);%0A        warningC
 ache
@@ -2796,30 +2796,37 @@
 he.setCacheData(
-m
+warningM
 ap);%0A    %7D%0A%7D%0A

Update both station weather info and warning caches