/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.apache.maven.archetype.test;

import javax.inject.Inject;

import java.io.File;
import java.util.Properties;

import org.apache.maven.archetype.ArchetypeGenerationRequest;
import org.apache.maven.archetype.ArchetypeGenerationResult;
import org.apache.maven.archetype.ArchetypeManager;
import org.apache.maven.archetype.catalog.Archetype;
import org.apache.maven.archetype.catalog.ArchetypeCatalog;
import org.codehaus.plexus.testing.PlexusTest;
import org.codehaus.plexus.util.FileUtils;
import org.eclipse.aether.DefaultRepositorySystemSession;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.repository.LocalRepository;
import org.eclipse.aether.repository.LocalRepositoryManager;
import org.junit.jupiter.api.Test;

import static org.codehaus.plexus.testing.PlexusExtension.getBasedir;
import static org.junit.jupiter.api.Assertions.fail;

/** @author Jason van Zyl */
@PlexusTest
public class ArchetypeGenerationTest {

    @Inject
    private ArchetypeManager archetype;

    @Inject
    private RepositorySystem repositorySystem;

    @Test
    public void testProjectGenerationFromAnArchetype() throws Exception {

        DefaultRepositorySystemSession repositorySession = new DefaultRepositorySystemSession();
        LocalRepositoryManager localRepositoryManager = repositorySystem.newLocalRepositoryManager(
                repositorySession, new LocalRepository("target/test-classes/repositories/central"));
        repositorySession.setLocalRepositoryManager(localRepositoryManager);

        ArchetypeCatalog catalog = archetype.getLocalCatalog(repositorySession);

        // Here I am just grabbing a OldArchetype but in a UI you would take the OldArchetype objects and present
        // them to the user.

        Archetype selection =
                catalog.getArchetypes().get(catalog.getArchetypes().size() - 1);

        // Now you will present a dialog, or whatever, and grab the following values.

        String groupId = "com.mycompany";

        String artifactId = "app";

        String version = "1.0.0";

        String packageName = "org.mycompany.app";

        // With the selected OldArchetype and the parameters you can create a generation request as follows:
        File outputDirectory = new File(getBasedir(), "target/test-classes/projects/archetyper-generate-1");
        FileUtils.forceDelete(outputDirectory);

        ArchetypeGenerationRequest agr = new ArchetypeGenerationRequest(selection)
                .setOutputDirectory(outputDirectory.getAbsolutePath())
                .setGroupId(groupId)
                .setArtifactId(artifactId)
                .setVersion(version)
                .setPackage(packageName)
                .setRepositorySession(repositorySession);

        Properties archetypeRequiredProperties = new Properties();
        archetypeRequiredProperties.setProperty("property-with-default-1", "value-1");
        archetypeRequiredProperties.setProperty("property-with-default-2", "value-2");
        archetypeRequiredProperties.setProperty("property-with-default-3", "value-3");
        archetypeRequiredProperties.setProperty("property-with-default-4", "value-4");
        archetypeRequiredProperties.setProperty("property-without-default-1", "some-value-1");
        archetypeRequiredProperties.setProperty("property-without-default-2", "some-value-2");
        archetypeRequiredProperties.setProperty("property-without-default-3", "some-value-3");
        archetypeRequiredProperties.setProperty("property-without-default-4", "some-value-4");
        archetypeRequiredProperties.setProperty("property_underscored_1", "prop1");
        archetypeRequiredProperties.setProperty("property_underscored-2", "prop2");
        agr.setProperties(archetypeRequiredProperties);

        // Then generate away!

        ArchetypeGenerationResult result = archetype.generateProjectFromArchetype(agr);

        if (result.getCause() != null) {
            result.getCause().printStackTrace(System.err);
            fail(result.getCause().getMessage());
        }
    }
}
