/*
 * Copyright (c) 2016 Network New Technologies Inc.
 *
 * 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.networknt.schema;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.JsonNode;

public class AdditionalPropertiesValidator extends BaseJsonValidator implements JsonValidator {
    private static final Logger logger = LoggerFactory.getLogger(AdditionalPropertiesValidator.class);

    private boolean allowAdditionalProperties;
    private JsonSchema additionalPropertiesSchema;
    private List<String> allowedProperties;
    private List<Pattern> patternProperties = new ArrayList<Pattern>();

    public AdditionalPropertiesValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema,
            ValidationContext validationContext) {
        super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.ADDITIONAL_PROPERTIES, validationContext);
        allowAdditionalProperties = false;
        if (schemaNode.isBoolean()) {
            allowAdditionalProperties = schemaNode.booleanValue();
        }
        if (schemaNode.isObject()) {
            allowAdditionalProperties = true;
            additionalPropertiesSchema = new JsonSchema(validationContext, getValidatorType().getValue(), schemaNode, parentSchema);
        }

        allowedProperties = new ArrayList<String>();
        JsonNode propertiesNode = parentSchema.getSchemaNode().get(PropertiesValidator.PROPERTY);
        if (propertiesNode != null) {
            for (Iterator<String> it = propertiesNode.fieldNames(); it.hasNext(); ) {
                allowedProperties.add(it.next());
            }
        }

        JsonNode patternPropertiesNode = parentSchema.getSchemaNode().get(PatternPropertiesValidator.PROPERTY);
        if (patternPropertiesNode != null) {
            for (Iterator<String> it = patternPropertiesNode.fieldNames(); it.hasNext(); ) {
                patternProperties.add(Pattern.compile(it.next()));
            }
        }

        parseErrorCode(getValidatorType().getErrorCodeKey());
    }

    public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String at) {
        if (logger.isDebugEnabled()) debug(logger, node, rootNode, at);

        Set<ValidationMessage> errors = new LinkedHashSet<ValidationMessage>();
        if (!node.isObject()) {
            // ignore no object
            return errors;
        }

        for (Iterator<String> it = node.fieldNames(); it.hasNext(); ) {
            String pname = it.next();
            // skip the context items
            if (pname.startsWith("#")) {
                continue;
            }
            boolean handledByPatternProperties = false;
            for (Pattern pattern : patternProperties) {
                Matcher m = pattern.matcher(pname);
                if (m.find()) {
                    handledByPatternProperties = true;
                    break;
                }
            }

            if (!allowedProperties.contains(pname) && !handledByPatternProperties) {
                if (!allowAdditionalProperties) {
                    errors.add(buildValidationMessage(at, pname));
                } else {
                    if (additionalPropertiesSchema != null) {
                        errors.addAll(additionalPropertiesSchema.validate(node.get(pname), rootNode, at + "." + pname));
                    }
                }
            }
        }
        return Collections.unmodifiableSet(errors);
    }

}

@@ -721,16 +721,43 @@
 tions;%0D%0A
+import java.util.HashSet;%0D%0A
 import j
@@ -1251,16 +1251,22 @@
 private 
+final 
 boolean 
@@ -1305,16 +1305,22 @@
 private 
+final 
 JsonSche
@@ -1363,19 +1363,24 @@
 private 
-Lis
+final Se
 t%3CString
@@ -1413,16 +1413,22 @@
 private 
+final 
 List%3CPat
@@ -1759,52 +1759,8 @@
 );%0D%0A
-        allowAdditionalProperties = false;%0D%0A
     
@@ -1862,33 +1862,70 @@
 lue();%0D%0A        
-%7D
+    additionalPropertiesSchema = null;
 %0D%0A        if (sc
@@ -1910,32 +1910,39 @@
  null;%0D%0A        
+%7D else 
 if (schemaNode.i
@@ -2137,32 +2137,146 @@
 ema);%0D%0A        %7D
+ else %7B%0D%0A            allowAdditionalProperties = false;%0D%0A            additionalPropertiesSchema = null;%0D%0A        %7D
 %0D%0A%0D%0A        allo
@@ -2295,24 +2295,22 @@
 s = new 
-ArrayLis
+HashSe
 t%3CString

Optimize AdditionalPropertiesValidator