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

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;

/**
 *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;

    @Override
    public List<String> getVersionsFor(GAV gav) throws CommunicationException {
        return aproxConnector.getVersionsOfGA(gav.getGA());
    }

    @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) {
        String bestMatchVersion = null;
        int biggestBuildNumber = 0;

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

        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;
    }

}

@@ -518,16 +518,52 @@
 Pattern;
+%0Aimport java.util.stream.Collectors;
 %0A%0A/**%0A *
@@ -1011,59 +1011,498 @@
     
-return aproxConnector.getVersionsOfGA(gav.getGA());
+Pattern pattern = Pattern.compile(%22.*%5B%5C%5C.-%5Dredhat-(%5C%5Cd+)%5C%5CD*%22);%0A        List%3CString%3E allVersions = aproxConnector.getVersionsOfGA(gav.getGA());%0A%0A        if (allVersions == null) %7B%0A            return null;%0A        %7D else %7B%0A            List%3CString%3E redhatVersions = allVersions.stream()%0A                    .filter(version -%3E pattern.matcher(version).matches())%0A                    .collect(Collectors.toList());%0A%0A            return redhatVersions.isEmpty() ? null : redhatVersions;%0A        %7D
 %0A   
@@ -2011,32 +2011,109 @@
 inedVersions) %7B%0A
+%0A        if (obtainedVersions == null) %7B%0A            return null;%0A        %7D%0A%0A
         String b
@@ -2279,19 +2279,22 @@
 on + %22.*
+%5B
 %5C%5C.
+-%5D
 redhat-(

Fix VersionFinderImpl so that tests can pass