package com.synopsys.integration.detect.lifecycle.boot.product.version;

import java.util.Optional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.synopsys.integration.blackduck.version.BlackDuckVersion;
import com.synopsys.integration.detect.configuration.DetectPropertyConfiguration;

public class BlackDuckVersionChecker {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    private final BlackDuckVersionParser parser;
    private final BlackDuckMinimumVersionChecks blackDuckMinimumVersionChecks;
    private final BlackDuckVersionSensitiveOptions blackDuckVersionSensitiveOptions;

    public BlackDuckVersionChecker(
        BlackDuckVersionParser parser,
        BlackDuckMinimumVersionChecks blackDuckMinimumVersionChecks,
        BlackDuckVersionSensitiveOptions blackDuckVersionSensitiveOptions
    ) {
        this.parser = parser;
        this.blackDuckMinimumVersionChecks = blackDuckMinimumVersionChecks;
        this.blackDuckVersionSensitiveOptions = blackDuckVersionSensitiveOptions;
    }

    public BlackDuckVersionCheckerResult check(String actualBlackDuckVersionString) {
        Optional<BlackDuckVersion> actualBlackDuckVersion = parser.parse(actualBlackDuckVersionString);
        if (!actualBlackDuckVersion.isPresent()) {
            logger.debug("Unable to parse Black Duck version string {}, so unable to perform version compatibility check", actualBlackDuckVersion);
            return BlackDuckVersionCheckerResult.passed();
        }
        for (BlackDuckMinimumVersionCheck blackDuckMinimumVersionCheck : blackDuckMinimumVersionChecks.get()) {
            if (blackDuckMinimumVersionCheck.getTest().test(blackDuckVersionSensitiveOptions) && !actualBlackDuckVersion.get()
                .isAtLeast(blackDuckMinimumVersionCheck.getMinimumBlackDuckVersion())) {
                String msg = String.format("%s requires at least Black Duck version %s; the connected server is only %s",
                    blackDuckMinimumVersionCheck.getDescription(), blackDuckMinimumVersionCheck.getMinimumBlackDuckVersion(),
                    actualBlackDuckVersion.get()
                );
                logger.debug(msg);
                return BlackDuckVersionCheckerResult.failed(msg);
            }
        }
        logger.trace("Version compatibility check passed");
        return BlackDuckVersionCheckerResult.passed();
    }
}
