package org.jboss.wolf.validator.impl.bom;

import static org.jboss.wolf.validator.internal.Utils.gav;
import static org.jboss.wolf.validator.internal.ValidatorSupport.listPomFiles;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;

import org.apache.commons.io.filefilter.IOFileFilter;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.Model;
import org.apache.maven.model.building.ModelBuildingResult;
import org.jboss.wolf.validator.Validator;
import org.jboss.wolf.validator.ValidatorContext;
import org.jboss.wolf.validator.internal.ValidatorSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Named
public class BomVersionPropertyValidator implements Validator {
    
    private static final Logger logger = LoggerFactory.getLogger(BomVersionPropertyValidator.class);
    
    @Inject @Named("bomVersionPropertyValidatorFilter")
    private IOFileFilter fileFilter;
    @Inject
    private BomFilter bomFilter;
    @Inject
    private ValidatorSupport validatorSupport;

    @Override
    public void validate(ValidatorContext ctx) {
        Collection<File> pomFiles = listPomFiles(ctx.getValidatedRepository(), fileFilter);
        for (File pomFile : pomFiles) {
            if (!ctx.getErrors(pomFile).isEmpty()) {
                logger.debug("skipping `{}`, because already contains exceptions", pomFile);
                continue;
            }
            
            ModelBuildingResult result = validatorSupport.buildModel(pomFile);
            Model rawModel = result.getRawModel();
            Model effectiveModel = result.getEffectiveModel();
            if (effectiveModel.getPackaging().equals("pom")) {
                if (bomFilter.isBom(effectiveModel)) {
                    validateBomDependenciesVersionProperty(ctx, pomFile, rawModel, effectiveModel);
                }
            }        
        }
    }

    private void validateBomDependenciesVersionProperty(ValidatorContext ctx, File pomFile, Model rawModel, Model effectiveModel) {
        String bomGav = gav(effectiveModel);
        
        List<String> bomDependenciesWithoutVersionProperty = new ArrayList<String>();
        for (Dependency bomDependency : rawModel.getDependencyManagement().getDependencies()) {
            if (!(bomDependency.getVersion() != null && bomDependency.getVersion().startsWith("${") && bomDependency.getVersion().endsWith("}"))) {
                String bomDependencyGav = gav(bomDependency);
                bomDependenciesWithoutVersionProperty.add(bomDependencyGav);
            }                        
        }
        
        if( !bomDependenciesWithoutVersionProperty.isEmpty() ) {
            ctx.addError(this, pomFile, new BomVersionPropertyException(bomGav, bomDependenciesWithoutVersionProperty.toArray(new String[]{})));
        }
    }

}
@@ -1708,75 +1708,8 @@
 ();%0A
-            if (effectiveModel.getPackaging().equals(%22pom%22)) %7B%0A    
     
@@ -1751,28 +1751,24 @@
 veModel)) %7B%0A
-    
             
@@ -1867,35 +1867,9 @@
     
-    %7D%0A            %7D        
+%7D
 %0A   
@@ -2150,24 +2150,151 @@
 %3CString%3E();%0A
+        if( rawModel.getDependencyManagement() != null && rawModel.getDependencyManagement().getDependencies() != null ) %7B%0A    
         for 
@@ -2373,24 +2373,28 @@
 encies()) %7B%0A
+    
             
@@ -2537,32 +2537,36 @@
                 
+  
+  
 String bomDepend
@@ -2595,16 +2595,20 @@
 dency);%0A
+    
         
@@ -2683,22 +2683,16 @@
 %0A       
-     %7D
         
@@ -2684,32 +2684,34 @@
                 
+%7D%0A
                %0A
@@ -2698,35 +2698,33 @@
   %7D%0A            
-   
+%7D
 %0A        %7D%0A     

fix bom gav when version defined in parent