/*-
 * Copyright (c) 2014-2015 Red Hat, 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 org.fedoraproject.xmvn.connector.aether;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;

import org.apache.maven.execution.AbstractExecutionListener;
import org.apache.maven.execution.ExecutionEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.fedoraproject.xmvn.artifact.Artifact;
import org.fedoraproject.xmvn.resolver.ResolutionRequest;
import org.fedoraproject.xmvn.resolver.ResolutionResult;

/**
 * Generates dependency version report after Maven session ends.
 * 
 * @author Mikolaj Izdebski
 */
class DependencyVersionReportGenerator
    extends AbstractExecutionListener
    implements ResolutionListener
{
    private final Logger logger = LoggerFactory.getLogger( DependencyVersionReportGenerator.class );

    private final Map<Artifact, ResolutionResult> data = new LinkedHashMap<>();

    @Override
    public void resolutionRequested( ResolutionRequest request )
    {
        if ( logger.isDebugEnabled() )
            request.setProviderNeeded( true );
    }

    @Override
    public void resolutionCompleted( ResolutionRequest request, ResolutionResult result )
    {
        Artifact artifact = request.getArtifact();
        data.put( artifact, result );
    }

    @Override
    public void sessionEnded( ExecutionEvent event )
    {
        Map<String, Set<String>> shortReport = new TreeMap<>();

        logger.debug( "Full XMvn dependency report:" );
        logger.debug( "<gId>:<aId>:<ext>[:<classifier>:]<version> => <compat-version>, provided by <pkg-name> (<rpm-version>)" );
        for ( Entry<Artifact, ResolutionResult> entry : data.entrySet() )
        {
            Artifact artifact = entry.getKey();
            ResolutionResult result = entry.getValue();

            String provider = result.getProvider();
            if ( provider == null )
                provider = "(none)";
            Set<String> requestedVersions = shortReport.get( provider );
            if ( requestedVersions == null )
            {
                requestedVersions = new TreeSet<>();
                shortReport.put( provider, requestedVersions );
            }
            requestedVersions.add( artifact.getVersion() );

            logger.debug( "  {} => {}, provided by {}", artifact, result.getCompatVersion(), provider );
        }

        logger.debug( "Simplified XMvn dependency report:" );
        logger.debug( "<pkg-name> (<rpm-version>): <requested-versions>" );
        StringBuilder sb = new StringBuilder();
        for ( Entry<String, Set<String>> entry : shortReport.entrySet() )
        {
            sb.setLength( 0 );
            sb.append( "  " );
            sb.append( entry.getKey() );
            sb.append( ": " );

            Iterator<String> it = entry.getValue().iterator();
            sb.append( it.next() );
            while ( it.hasNext() )
            {
                sb.append( ", " );
                sb.append( it.next() );
            }

            logger.debug( "{}", sb );
        }
    }
}

@@ -654,35 +654,8 @@
 r;%0A%0A
-import java.util.Iterator;%0A
 impo
@@ -708,36 +708,8 @@
 ap;%0A
-import java.util.Map.Entry;%0A
 impo
@@ -777,16 +777,52 @@
 TreeSet;
+%0Aimport java.util.stream.Collectors;
 %0A%0Aimport
@@ -2270,188 +2270,47 @@
     
-for ( Entry%3CArtifact, ResolutionResult%3E entry : data.entrySet() )%0A        %7B%0A            Artifact artifact = entry.getKey();%0A            ResolutionResult result = entry.getValue();%0A
+data.forEach( ( artifact, result ) -%3E %7B
 %0A   
@@ -2861,32 +2861,35 @@
 der );%0A        %7D
+ );
 %0A%0A        logger
@@ -3024,485 +3024,159 @@
     
-StringBuilder sb = new StringBuilder();%0A        for ( Entry%3CString, Set%3CString%3E%3E entry : shortReport.entrySet() )%0A        %7B%0A            sb.setLength( 0 );%0A            sb.append( %22  %22 );%0A            sb.append( entry.getKey() );%0A            sb.append( %22: %22 );%0A%0A            Iterator%3CString%3E it = entry.getValue().iterator();%0A            sb.append( it.next() );%0A            while ( it.hasNext() )%0A            %7B%0A                sb.append( %22, %22 );%0A                sb.append( it.next(
+shortReport.forEach( ( provider, versions ) -%3E %7B%0A            logger.debug( %22  %7B%7D: %7B%7D%22, provider, versions.stream().collect( Collectors.joining( %22, %22 ) 
 ) );
@@ -3188,62 +3188,12 @@
     
-   
- %7D%0A%0A            logger.debug( %22%7B%7D%22, sb );%0A        %7D
+%7D );
 %0A   

Use Java 8 language features in DependencyVersionReportGenerator