/*
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 */
package jdk.incubator.jpackage.internal;

import java.io.IOException;
import java.nio.file.Path;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.Stream;


public final class ToolValidator {

    ToolValidator(String tool) {
        this(Path.of(tool));
    }

    ToolValidator(Path toolPath) {
        this.toolPath = toolPath;
        args = new ArrayList<>();

        if (Platform.getPlatform() == Platform.LINUX) {
            setCommandLine("--version");
        }

        setToolNotFoundErrorHandler(null);
        setToolOldVersionErrorHandler(null);
    }

    ToolValidator setCommandLine(String... args) {
        this.args = List.of(args);
        return this;
    }

    ToolValidator setMinimalVersion(Comparable<String> v) {
        minimalVersion = v;
        return this;
    }

    ToolValidator setVersionParser(Function<Stream<String>, String> v) {
        versionParser = v;
        return this;
    }

    ToolValidator setToolNotFoundErrorHandler(
            BiFunction<String, IOException, ConfigException> v) {
        toolNotFoundErrorHandler = v;
        return this;
    }

    ToolValidator setToolOldVersionErrorHandler(BiFunction<String, String, ConfigException> v) {
        toolOldVersionErrorHandler = v;
        return this;
    }

    ConfigException validate() {
        List<String> cmdline = new ArrayList<>();
        cmdline.add(toolPath.toString());
        cmdline.addAll(args);

        String name = toolPath.getFileName().toString();
        try {
            ProcessBuilder pb = new ProcessBuilder(cmdline);
            AtomicBoolean canUseTool = new AtomicBoolean();
            if (minimalVersion == null) {
                // No version check.
                canUseTool.setPlain(true);
            }

            String[] version = new String[1];
            Executor.of(pb).setOutputConsumer(lines -> {
                if (versionParser != null && minimalVersion != null) {
                    version[0] = versionParser.apply(lines);
                    if (minimalVersion.compareTo(version[0]) < 0) {
                        canUseTool.setPlain(true);
                    }
                }
            }).execute();

            if (!canUseTool.getPlain()) {
                if (toolOldVersionErrorHandler != null) {
                    return toolOldVersionErrorHandler.apply(name, version[0]);
                }
                return new ConfigException(MessageFormat.format(I18N.getString(
                        "error.tool-old-version"), name, minimalVersion),
                        MessageFormat.format(I18N.getString(
                                "error.tool-old-version.advice"), name,
                                minimalVersion));
            }
        } catch (IOException e) {
            if (toolNotFoundErrorHandler != null) {
                return toolNotFoundErrorHandler.apply(name, e);
            }
            return new ConfigException(MessageFormat.format(I18N.getString(
                    "error.tool-not-found"), name, e.getMessage()),
                    MessageFormat.format(I18N.getString(
                            "error.tool-not-found.advice"), name), e);
        }

        // All good. Tool can be used.
        return null;
    }

    private final Path toolPath;
    private List<String> args;
    private Comparable<String> minimalVersion;
    private Function<Stream<String>, String> versionParser;
    private BiFunction<String, IOException, ConfigException> toolNotFoundErrorHandler;
    private BiFunction<String, String, ConfigException> toolOldVersionErrorHandler;
}
