package fr.synchrotron.soleil.ica.ci.service.legacymavenproxy.pommetadata;

import fr.synchrotron.soleil.ica.ci.lib.mongodb.latestversionrresolver.repository.LatestArtifactRepository;
import fr.synchrotron.soleil.ica.ci.lib.mongodb.latestversionrresolver.service.ArtifactVersionResolverService;
import fr.synchrotron.soleil.ica.ci.lib.mongodb.latestversionrresolver.service.MavenVersionResolverService;
import org.apache.maven.model.*;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;

import java.io.IOException;
import java.io.StringReader;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * @author Gregory Boissinot
 */
public class MavenPomFixer {

    private final LatestArtifactRepository artifactRepository;

    public MavenPomFixer(LatestArtifactRepository artifactRepository) {
        this.artifactRepository = artifactRepository;
    }

    public Model getModelWithResolvedParent(String originalPomContent) {

        StringReader pomReader = new StringReader(originalPomContent);
        final MavenXpp3Reader mavenXpp3Reader = new MavenXpp3Reader();
        Model model;
        try {
            model = mavenXpp3Reader.read(pomReader);
        } catch (IOException xe) {
            throw new RuntimeException(xe);
        } catch (XmlPullParserException xe) {
            throw new RuntimeException(xe);
        }
        pomReader.close();

        final Parent parent = model.getParent();
        if (parent != null && "RELEASE".equals(parent.getVersion())) {
            MavenVersionResolverService versionResolverService
                    = new MavenVersionResolverService(new ArtifactVersionResolverService(artifactRepository));
            final String resolvedParentVersion = versionResolverService.getLatestArtifact(parent.getGroupId(), parent.getArtifactId());
            parent.setVersion(resolvedParentVersion);
        }

        List profiles = model.getProfiles();
        if (profiles != null) {
            for (Object curProfile : profiles) {
                Profile profile = (Profile) curProfile;
                if (profile != null) {
                    String profilName = profile.getId();
                    if ("release".equals(profilName) || ("stable".equals(profilName))) {
                        Properties properties = profile.getProperties();
                        if (properties != null) {
                            for (Map.Entry<Object, Object> entry : properties.entrySet()) {
                                model.addProperty((String) entry.getKey(), (String) entry.getValue());
                            }
                        }
                    }
                }
            }
        }

        DependencyManagement dependencyManagement = model.getDependencyManagement();
        if (dependencyManagement != null) {
            List dependencies = dependencyManagement.getDependencies();
            if (dependencies != null) {
                for (int k = 0; k < dependencies.size(); k++) {
                    Dependency dependency = (Dependency) dependencies.get(k);
                    String artifactId = dependency.getArtifactId();
                    if ("C-CPP-Devices".equals(artifactId)) {
                        dependencies.remove(k);
                    }
                }
            }
        }


        return model;
    }
}

@@ -603,24 +603,52 @@
 ringReader;%0A
+import java.util.ArrayList;%0A
 import java.
@@ -654,24 +654,24 @@
 .util.List;%0A
-
 import java.
@@ -2049,32 +2049,83 @@
 iles != null) %7B%0A
+            List newProfileList = new ArrayList();%0A
             for 
@@ -2305,24 +2305,167 @@
 le.getId();%0A
+                    //For release and stable profile, we don't conserve them (for compatibility with Gradle %3C 1.12 but we add their properties%0A
             
@@ -2907,32 +2907,263 @@
               %7D%0A
+                        //We don't conserve unstable profile%0A                    %7D else if ((!%22unstable%22.equals(profilName)) && (!%22continuous-integration%22.equals(profilName))) %7B%0A                        newProfileList.add(profile);%0A
                 
@@ -3192,32 +3192,79 @@
 %7D%0A            %7D%0A
+            model.setProfiles(newProfileList);%0A
         %7D%0A%0A     
@@ -3851,32 +3851,32 @@
               %7D%0A
-
             %7D%0A  
@@ -3884,16 +3884,249 @@
      %7D%0A%0A
+        List dependencies = model.getDependencies();%0A        for (Object dependencyObj : dependencies) %7B%0A            Dependency dependency = (Dependency) dependencyObj;%0A            dependency.setVersion(%22latest.release%22);%0A        %7D%0A%0A
 %0A       

Fix Profiles