package cz.senslog.connector.fetch.azure;

import cz.senslog.connector.model.config.DefaultConfig;
import cz.senslog.connector.model.config.HostConfig;
import cz.senslog.connector.fetch.azure.auth.AzureAuthConfig;

import java.time.LocalDateTime;

import static cz.senslog.connector.tools.json.BasicJson.objectToJson;

/**
 * The class {@code AzureConfig} represents a configuration class for the {@link AzureFetcher}.
 *
 * @author Lukas Cerny
 * @version 1.0
 * @since 1.0
 */
public class AzureConfig {

    private final Integer limitPerSensor;
    private final HostConfig sensorInfoHost;
    private final HostConfig sensorDataHost;
    private final AzureAuthConfig authentication;
    private final AzureSessionProxyConfig sessionProxy;
    private final AllowedStation allowedStation;

    private LocalDateTime startDate;
    private LocalDateTime endDate;

    /**
     * Constructor sets class attributes from default input configuration class {@link DefaultConfig}.
     * From the default configuration are dynamically get properties for attributes.
     * @param config - default configuration
     */
    public AzureConfig(DefaultConfig config) {
        this.limitPerSensor = config.getIntegerProperty("limitPerSensor");
        this.sensorInfoHost = new HostConfig(config.getPropertyConfig("sensorInfoHost"));
        this.sensorDataHost = new HostConfig(config.getPropertyConfig("sensorDataHost"));
        this.authentication = new AzureAuthConfig(config.getPropertyConfig("authentication"));
        this.allowedStation = new AllowedStation(config.getPropertyConfig("allowedStation"));

        this.startDate = config.getLocalDateTimeProperty("startDate");
        this.endDate = config.getOptionalLocalDateTimeProperty("endDate").orElse(null);

        this.sessionProxy = config.containsProperty("sessionProxy") ?
                new AzureSessionProxyConfig(config.getPropertyConfig("sessionProxy")) : null;
    }

    public LocalDateTime getStartDate() {
        return startDate;
    }

    public Integer getLimitPerSensor() {
        return limitPerSensor;
    }

    public HostConfig getSensorInfoHost() {
        return sensorInfoHost;
    }

    public HostConfig getSensorDataHost() {
        return sensorDataHost;
    }

    public AzureAuthConfig getAuthentication() {
        return authentication;
    }

    public LocalDateTime getEndDate() {
        return endDate;
    }

    public AzureSessionProxyConfig getSessionProxy() {
        return sessionProxy;
    }

    public AllowedStation getAllowedStation() {
        return allowedStation;
    }

    @Override
    public String toString() {
        return objectToJson(this);
    }
}
