package org.jboss.da.reports.backend.impl;

import org.jboss.da.common.version.OSGiVersionParser;
import org.jboss.da.communication.CommunicationException;
import org.jboss.da.communication.aprox.api.AproxConnector;
import org.jboss.da.communication.model.GAV;
import org.jboss.da.reports.backend.api.VersionFinder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

import java.lang.invoke.MethodHandles;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * Performs single lookups for the built artifacts
 * 
 * @author Jakub Bartecek <jbartece@redhat.com>
 *
 */
@ApplicationScoped
public class VersionFinderImpl implements VersionFinder {

    private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

    @Inject
    private AproxConnector aproxConnector;

    @Inject
    private OSGiVersionParser osgiParser;

    @Override
    public List<String> getVersionsFor(GAV gav) throws CommunicationException {
        Pattern pattern = Pattern.compile(".*[\\.-]redhat-(\\d+)\\D*");
        List<String> allVersions = aproxConnector.getVersionsOfGA(gav.getGA());

        if (allVersions == null) {
            return null;
        } else {
            List<String> redhatVersions = allVersions.stream()
                    .filter(version -> pattern.matcher(version).matches())
                    .collect(Collectors.toList());

            return redhatVersions.isEmpty() ? null : redhatVersions;
        }
    }

    @Override
    public String getBestMatchVersionFor(GAV gav) throws CommunicationException {
        List<String> obtainedVersions = aproxConnector.getVersionsOfGA(gav.getGA());
        return findBiggestMatchingVersion(gav, obtainedVersions);
    }

    @Override
    public String getBestMatchVersionFor(GAV gav, List<String> availableVersions) {
        return findBiggestMatchingVersion(gav, availableVersions);
    }

    private String findBiggestMatchingVersion(GAV gav, List<String> obtainedVersions) {
        if (obtainedVersions == null)
            return null;

        final String patternSuffix = "[.-]redhat-(\\d+)\\D*";
        String origVersion = gav.getVersion();
        Pattern pattern = Pattern.compile(origVersion + patternSuffix);

        String bestMatchVersion = findBiggestMatchingVersion(obtainedVersions, pattern);
        if (bestMatchVersion == null) {
            String osgiVersion = osgiParser.getOSGiVersion(origVersion);
            pattern = Pattern.compile(osgiVersion + patternSuffix);
            bestMatchVersion = findBiggestMatchingVersion(obtainedVersions, pattern);
        }

        return bestMatchVersion;
    }

    private String findBiggestMatchingVersion(List<String> obtainedVersions, Pattern pattern) {
        String bestMatchVersion = null;
        int biggestBuildNumber = 0;

        for (String ver : obtainedVersions) {
            Matcher matcher = pattern.matcher(ver);
            if (matcher.matches()) {
                int foundBuildNumber = Integer.parseInt(matcher.group(1));
                if (foundBuildNumber > biggestBuildNumber) {
                    bestMatchVersion = ver;
                    biggestBuildNumber = foundBuildNumber;
                }
            }
        }

        return bestMatchVersion;
    }

}

@@ -900,24 +900,110 @@
 pClass());%0A%0A
+    private final String PATTERN_SUFFIX_BUILT_VERSION = %22%5B.-%5Dredhat-(%5C%5Cd+)%5C%5CD*%22;%0A    %0A
     @Inject%0A
@@ -994,32 +994,32 @@
    %0A    @Inject%0A
-
     private Apro
@@ -1099,16 +1099,21 @@
 iParser;
+%0A    
 %0A%0A    @O
@@ -1246,35 +1246,36 @@
 ile(
-%22.*%5B%5C%5C.-%5Dredhat-(%5C%5Cd+)%5C%5CD*%22
+PATTERN_SUFFIX_BUILT_VERSION
 );%0A 
@@ -2289,70 +2289,8 @@
 l;%0A%0A
-        final String patternSuffix = %22%5B.-%5Dredhat-(%5C%5Cd+)%5C%5CD*%22;%0A
     
@@ -2388,29 +2388,44 @@
 rsion + 
-patternSuffix
+PATTERN_SUFFIX_BUILT_VERSION
 );%0A%0A    
@@ -2678,21 +2678,36 @@
 n + 
-patternSuffix
+PATTERN_SUFFIX_BUILT_VERSION
 );%0A 

Fix pattern for built versions