package cz.senslog.connector.fetch.metno;

import cz.senslog.connector.model.config.DefaultConfig;
import cz.senslog.connector.model.config.PropertyConfig;

import java.util.*;

public class MetnoConfig {

    private final String url;
    private final TimeZone timeZone;
    private final List<Station> stations;
    private final String allowedStationURL;

    MetnoConfig(DefaultConfig defaultConfig) {
        this.url = defaultConfig.getStringProperty("baseUrl");
        this.timeZone = TimeZone.getTimeZone(defaultConfig.getStringProperty("timeZone"));

        this.allowedStationURL = defaultConfig.containsProperty("allowedStationURL") ? defaultConfig.getStringProperty("allowedStationURL") : null;
        Set<PropertyConfig> stations = defaultConfig.containsProperty("allowedStations") ? defaultConfig.getSetProperty("allowedStations") : Collections.emptySet();
        this.stations = new ArrayList<>(stations.size());
        for (PropertyConfig station : stations) {
            List<Double> locations = station.getListProperty("location", Double.class);
            this.stations.add(new Station(locations.get(0), locations.get(1)));
        }

        if (allowedStationURL == null && this.stations.isEmpty()) {
            throw new IllegalStateException("Metno config requires at least one station");
        }
    }

    public String getUrl() {
        return url;
    }

    public TimeZone getTimeZone() {
        return timeZone;
    }

    public List<Station> getStations() {
        return stations;
    }

    public String getAllowedStationURL() {
        return allowedStationURL;
    }
}
