/*-
 * Copyright (c) 2014-2024 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.maven;

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

import org.apache.maven.AbstractMavenLifecycleParticipant;
import org.apache.maven.MavenExecutionException;
import org.apache.maven.execution.MavenExecutionRequest;
import org.apache.maven.execution.MavenSession;
import org.eclipse.sisu.Nullable;

import org.fedoraproject.xmvn.logging.Logger;

/**
 * Installs some of XMvn extensions for Maven.
 * 
 * @author Mikolaj Izdebski
 */
@Named
@Singleton
public class XMvnMavenLifecycleParticipant
    extends AbstractMavenLifecycleParticipant
{
    @Inject
    private Logger logger;

    @Inject
    @Nullable
    @Named( "ide" )
    private XMvnWorkspaceReader workspaceReader;

    @Inject
    private XMvnMojoExecutionListener mojoExecutionListener;

    @Inject
    private XMvnToolchainManager toolchainManager;

    @Override
    public void afterSessionStart( MavenSession session )
        throws MavenExecutionException
    {
        MavenExecutionRequest request = session.getRequest();

        DependencyVersionReportGenerator reportGenerator = new DependencyVersionReportGenerator( logger );

        if ( workspaceReader != null )
        {
            workspaceReader.addResolutionListener( mojoExecutionListener );
            workspaceReader.addResolutionListener( reportGenerator );
        }

        ChainedExecutionListener chainedListener = new ChainedExecutionListener();
        chainedListener.addExecutionListener( request.getExecutionListener() );
        chainedListener.addExecutionListener( reportGenerator );
        request.setExecutionListener( chainedListener );
    }

    @Override
    public void afterProjectsRead( MavenSession session )
        throws MavenExecutionException
    {
        toolchainManager.activate( session );
    }
}
