// Copyright (c) 2020 UWB & LESP.
// The UWB & LESP license this file to you under the MIT license.

package cz.senslog.connector.fetch.senslog.v1;

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

import java.net.URI;
import java.net.URISyntaxException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.util.TimeZone;

public class SenslogConfig {

    public static class AuthConfig {

        private final String username;
        private final String password;

        private AuthConfig(PropertyConfig config) {
            this.username = config.getStringProperty("username");
            this.password = config.getStringProperty("password");
        }

        public String getUsername() {
            return username;
        }

        public String getPassword() {
            return password;
        }
    }

    private final TimeZone timeZone;
    private final LocalDateTime startDate;
    private final String user;
    private final Integer interval;
    private final AllowedStation allowedStation;
    private final SenslogSessionProxyConfig sessionProxy;
    private final AuthConfig auth;
    private final String baseUrl;
    private final URI baseUri;

    SenslogConfig(DefaultConfig defaultConfig) {
        this.timeZone = TimeZone.getTimeZone(defaultConfig.getStringProperty("timeZone"));
        String startDate = defaultConfig.getStringProperty("startDate");

        LocalDateTime tempStartDate = null;
        if (startDate.equalsIgnoreCase("now")) {
            tempStartDate = LocalDateTime.ofInstant(Instant.now(), timeZone.toZoneId());
        } else {
            tempStartDate = defaultConfig.getLocalDateTimeProperty("startDate");
        }

        if (defaultConfig.containsProperty("delayStartDate")) {
            tempStartDate = tempStartDate.minusMinutes(defaultConfig.getIntegerProperty("delayStartDate"));
        }

        this.startDate = tempStartDate;
        this.user = defaultConfig.getStringProperty("user");
        this.interval = defaultConfig.getIntegerProperty("interval");
        this.allowedStation = new AllowedStation(defaultConfig.getPropertyConfig("allowedStation"));
        this.auth = defaultConfig.containsProperty("auth") ? new AuthConfig(defaultConfig.getPropertyConfig("auth")) : null;
        this.baseUrl = defaultConfig.getStringProperty("baseUrl");
        try {
            this.baseUri = new URI(baseUrl);
        } catch (URISyntaxException e) {
            throw new RuntimeException("The config is in invalid format. " + e.getMessage());
        }

        this.sessionProxy = defaultConfig.containsProperty("sessionProxy") ?
                new SenslogSessionProxyConfig(defaultConfig.getPropertyConfig("sessionProxy")) : null;
    }

    public LocalDateTime getStartDate() {
        return startDate;
    }

    public TimeZone getTimeZone() {
        return timeZone;
    }

    public String getUser() {
        return user;
    }

    public Integer getInterval() {
        return interval;
    }

    AllowedStation getAllowedStation() {
        return allowedStation;
    }

    public SenslogSessionProxyConfig getSessionProxy() {
        return sessionProxy;
    }

    public AuthConfig getAuth() {
        return auth;
    }

    public String getBaseUrl() {
        return baseUrl;
    }

    public URI getBaseURI() {
        return baseUri;
    }
}
