/**
 * Copyright (c) 2016 Xtivia, Inc. All rights reserved.
 *
 * This file is part of the Xtivia Services Framework (XSF) library.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */
package com.xtivia.speedray.gogo.deploy

import org.apache.maven.repository.internal.MavenRepositorySystemUtils
import org.eclipse.aether.DefaultRepositorySystemSession
import org.eclipse.aether.RepositorySystem
import org.eclipse.aether.connector.basic.BasicRepositoryConnectorFactory
import org.eclipse.aether.impl.DefaultServiceLocator
import org.eclipse.aether.repository.Authentication
import org.eclipse.aether.repository.LocalRepository
import org.eclipse.aether.repository.RemoteRepository
import org.eclipse.aether.spi.connector.RepositoryConnectorFactory
import org.eclipse.aether.spi.connector.transport.TransporterFactory
import org.eclipse.aether.transport.file.FileTransporterFactory
import org.eclipse.aether.transport.http.HttpTransporterFactory
import org.eclipse.aether.util.repository.AuthenticationBuilder


class AetherUtil
{

    static DefaultRepositorySystemSession newRepositorySystemSession(RepositorySystem system, LocalRepository localRepo, ConsoleTransferListener consoleTransferListener )
    {
        DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession()

        session.setLocalRepositoryManager( system.newLocalRepositoryManager( session, localRepo ) )

        session.setTransferListener( consoleTransferListener )
        session.setRepositoryListener( new ConsoleRepositoryListener() )

        // uncomment to generate dirty trees
        // session.setDependencyGraphTransformer( null );

        return session
    }

    static DefaultRepositorySystemSession newRepositorySystemSession(RepositorySystem system, LocalRepository localRepo )
    {
        DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession()

        session.setLocalRepositoryManager( system.newLocalRepositoryManager( session, localRepo ) )

        session.setTransferListener( new ConsoleTransferListener() )
        // session.setRepositoryListener( new ConsoleRepositoryListener() )

        // uncomment to generate dirty trees
        // session.setDependencyGraphTransformer( null );

        return session
    }

    static List<RemoteRepository> newRepositories(RemoteRepository remoteRepository)
    {
        return new ArrayList<RemoteRepository>( Arrays.asList( remoteRepository != null ? remoteRepository : newCentralRepository() ) )
    }

    static RemoteRepository newCentralRepository()
    {
        return new RemoteRepository.Builder( "central", "default", "https://repo1.maven.org/maven2/" ).build()
    }

    static RemoteRepository newRemoteRepository(String url, String user, String pass){

        RemoteRepository.Builder builder = new RemoteRepository.Builder( "custom", "default", url )

        if(user != null && pass != null){
            Authentication auth = new AuthenticationBuilder().addUsername(user).addPassword(pass).build()
            builder.setAuthentication(auth)
        }

        return builder.build()

    }

    static RepositorySystem newRepositorySystem()
    {
        /*
         * Aether's components implement org.eclipse.aether.spi.locator.Service to ease manual wiring and using the
         * prepopulated DefaultServiceLocator, we only need to register the repository connector and transporter
         * factories.
         */
        DefaultServiceLocator locator = MavenRepositorySystemUtils.newServiceLocator()
        locator.addService( RepositoryConnectorFactory.class, BasicRepositoryConnectorFactory.class )
        locator.addService( TransporterFactory.class, FileTransporterFactory.class )
        locator.addService( TransporterFactory.class, HttpTransporterFactory.class )

        locator.setErrorHandler( new DefaultServiceLocator.ErrorHandler()
        {
            @Override
            void serviceCreationFailed(Class<?> type, Class<?> impl, Throwable exception )
            {
                exception.printStackTrace()
            }
        } )

        return locator.getService( RepositorySystem.class )
    }
}