package com.lazerycode.jmeter.properties;

import com.lazerycode.jmeter.JMeterMojo;

import java.util.Map;
import java.util.Properties;

/**
 * Handler that can merge Properties objects and Maps
 *
 * @author Arne Franken, Mark Collin
 */
public class PropertyFileMerger extends JMeterMojo {

    private Properties baseProperties;

    public PropertyFileMerger(Properties baseProperties) {
        this.baseProperties = stripReservedProperties(baseProperties);
    }

    /**
     * Merge given Map into given Properties object
     *
     * @param customProperties Map to merge into the Properties object
     * @return merged Properties object
     */
    public Properties mergeProperties(Map<String, String> customProperties) {
        if (customProperties != null && !customProperties.isEmpty()) {
            for (String key : customProperties.keySet()) {
                if (!isReservedProperty(key)) {
                    this.baseProperties.setProperty(key, customProperties.get(key));
                } else {
                    getLog().warn("Unable to set '" + key + "', it is a reserved property in the jmeter-maven-plugin");
                }
                warnUserOfPossibleErrors(key);
            }
        }
        return this.baseProperties;
    }

    //==================================================================================================================

    /**
     * This will strip all reserved properties from a Properties object.
     * (Used to ensure that restricted properties haven't been set in custom properties files)
     *
     * @param propertyFile
     * @return
     */
    private Properties stripReservedProperties(Properties propertyFile) {
        for (ReservedProperties reservedProperty : ReservedProperties.values()) {
            if (propertyFile.containsKey(reservedProperty.getPropertyKey())) {
                propertyFile.remove(reservedProperty.getPropertyKey());
                getLog().warn("Unable to set '" + reservedProperty.getPropertyKey() + "', it is a reserved property in the jmeter-maven-plugin");
            }
        }
        return propertyFile;
    }

    /**
     * Check to see if a property is restricted or not.
     * (Used to check if properties set via the POM are restricted before merging them into the final properties file)
     *
     * @param value
     * @return
     */
    private boolean isReservedProperty(String value) {
        for (ReservedProperties reservedProperty : ReservedProperties.values()) {
            if (reservedProperty.getPropertyKey().equals(value)) {
                return true;
            }
        }
        return false;
    }

    /**
     * Print a warning out to the user to highlight potential typos in the properties they have set.
     *
     * @param value
     */
    private void warnUserOfPossibleErrors(String value) {
        for (String key : this.baseProperties.stringPropertyNames()) {
            if (!key.equals(value) && key.toLowerCase().equals(value.toLowerCase())) {
                getLog().warn("You have set a property called '" + value + "' which is very similar to '" + key + "'!");
            }
        }
    }
}


@@ -436,32 +436,8 @@
 s = 
-stripReservedProperties(
 base
@@ -446,17 +446,16 @@
 operties
-)
 ;%0D%0A    %7D
@@ -865,61 +865,8 @@
  %7B%0D%0A
-                if (!isReservedProperty(key)) %7B%0D%0A    
     
@@ -963,135 +963,36 @@
     
-%7D else %7B%0D%0A                    getLog().warn(%22Unable to set '%22 + key + %22', it is a reserved property in the jmeter-maven-plugin%22
+warnUserOfPossibleErrors(key
 );%0D%0A
@@ -995,33 +995,40 @@
 );%0D%0A            
+%7D%0D%0A    
  
-
    %7D%0D%0A          
@@ -1029,72 +1029,75 @@
     
-        warnUserOfPossibleErrors(key);%0D%0A            %7D%0D%0A        %7D
+this.baseProperties = stripReservedProperties(this.baseProperties);
 %0D%0A  
@@ -2031,543 +2031,8 @@
 %0D%0A%0D%0A
-    /**%0D%0A     * Check to see if a property is restricted or not.%0D%0A     * (Used to check if properties set via the POM are restricted before merging them into the final properties file)%0D%0A     *%0D%0A     * @param value%0D%0A     * @return%0D%0A     */%0D%0A    private boolean isReservedProperty(String value) %7B%0D%0A        for (ReservedProperties reservedProperty : ReservedProperties.values()) %7B%0D%0A            if (reservedProperty.getPropertyKey().equals(value)) %7B%0D%0A                return true;%0D%0A            %7D%0D%0A        %7D%0D%0A        return false;%0D%0A    %7D%0D%0A%0D%0A
     
@@ -2551,12 +2551,8 @@
     %7D%0D%0A%7D
-%0D%0A%0D%0A

Refactor code to ensure reserved properties are only stripped out once, previous implementation was slightly inefficient