/*
 * Copyright 2015 dc-square GmbH
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hivemq.plugin.configuration;

import com.amazonaws.regions.Regions;
import com.google.inject.Inject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Properties;

/**
 * @author Christian Götz
 */
public class Configuration {

    private static final Logger log = LoggerFactory.getLogger(Configuration.class);

    private final Properties properties;

    @Inject
    public Configuration(PluginReader pluginReader) {
        properties = pluginReader.getProperties();
    }


    public AuthenticationType getAuthenticationType() {

        try {
            final String property = getProperty("credentials-type");
            if (property == null) {
                return null;
            }

            return AuthenticationType.fromName(property);
        } catch (IllegalArgumentException e) {
            log.error("Not able to initialize S3 Plugin", e);
            return null;
        }
    }

    public Regions getRegion() {
        try {
            final String property = getProperty("s3-bucket-region");
            if (property == null) {
                return null;
            }

            return Regions.fromName(property);
        } catch (IllegalArgumentException e) {
            log.error("Not able to initialize S3 Plugin", e);
            return null;
        }
    }

    public String getFilePrefix() {
        final String property = getProperty("file-prefix");
        if (property == null) {
            return "";
        }
        return property;
    }

    public long getExpirationMinutes() {
        final String property = getProperty("file-expiration");
        if (property == null) {
            return 0L;
        }

        try {
            final long value = Long.parseLong(property);
            if (value < 0) {
                log.error("Value for S3 expiration configuration must be positive or zero, disabling expiration");
                return 0;
            }
            return value;
        } catch (NumberFormatException e) {
            log.error("Not able to parse S3 expiration configuration, disabling expiration");
            return 0L;
        }
    }

    public long getOwnInformationUpdateInterval() {
        final String property = getProperty("update-interval");
        if (property == null) {
            return 0L;
        }

        try {
            final long value = Long.parseLong(property);
            if (value < 0) {
                log.error("Value for S3 update interval configuration must be positive or zero, disabling update interval");
                return 0;
            }
            return value;
        } catch (NumberFormatException e) {
            log.error("Not able to parse S3 update interval configuration, disabling update interval");
            return 0L;
        }
    }

    public String getBucketName() {
        return getProperty("s3-bucket-name");
    }

    public String getAccessKeyId() {
        return getProperty("credentials-access-key-id");
    }

    public String getSecretAccessKey() {
        return getProperty("credentials-secret-access-key");
    }

    public String getSessionToken() {
        return getProperty("credentials-session-token");
    }

    private String getProperty(final String key) {
        if (properties == null) {
            return null;
        }

        return properties.getProperty(key);
    }
}

@@ -1991,35 +1991,185 @@
  String property
- = 
+;%0A%0A        if (System.getenv(%22S3_DISCOVERY_FILE_PREFIX%22) != null) %7B%0A            property = System.getenv(%22S3_DISCOVERY_FILE_PREFIX%22);%0A        %7D else if (
 getProperty(%22fil
@@ -2178,17 +2178,27 @@
 prefix%22)
-;
+ != null) %7B
 %0A       
@@ -2190,36 +2190,36 @@
 null) %7B%0A        
-if (
+    
 property == null
@@ -2204,39 +2204,75 @@
       property =
-= null)
+ getProperty(%22file-prefix%22);%0A        %7D else
  %7B%0A            r
@@ -2270,22 +2270,26 @@
         
-return
+property =
  %22%22;%0A   
@@ -2287,32 +2287,41 @@
 = %22%22;%0A        %7D%0A
+        %0A
         return p

Allow file-prefix configuration property to be set by an environment variable