package io.quarkus.bootstrap.utils;

import static io.quarkus.bootstrap.util.QuarkusModelHelper.DEVMODE_REQUIRED_TASKS;

import io.quarkus.bootstrap.resolver.AppModelResolverException;
import io.quarkus.bootstrap.resolver.QuarkusGradleModelFactory;
import io.quarkus.bootstrap.resolver.model.QuarkusModel;
import io.quarkus.bootstrap.util.QuarkusModelHelper;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

/**
 * Helper class used to expose build tool used by the project
 */
public class BuildToolHelper {

    public enum BuildTool {
        MAVEN("pom.xml"),
        GRADLE("build.gradle");

        private final String buildFile;

        BuildTool(String buildFile) {
            this.buildFile = buildFile;
        }

        public String getBuildFile() {
            return buildFile;
        }
    }

    private BuildToolHelper() {

    }

    public static boolean isMavenProject(Path project) {
        Path currentPath = project;
        while (currentPath != null) {
            if (Files.exists(currentPath.resolve(BuildTool.MAVEN.getBuildFile()))) {
                return true;
            }
            if (Files.exists(currentPath.resolve(BuildTool.GRADLE.getBuildFile()))) {
                return false;
            }
            currentPath = currentPath.getParent();
        }
        return false;
    }

    public static Path getBuildFile(Path project, BuildTool tool) {
        Path currentPath = project;
        while (currentPath != null) {
            if (Files.exists(currentPath.resolve(tool.getBuildFile()))) {
                return currentPath;
            }
            currentPath = currentPath.getParent();
        }
        return null;
    }

    public static QuarkusModel enableGradleAppModel(Path projectRoot, String mode, String... tasks)
            throws IOException, AppModelResolverException {
        if (isMavenProject(projectRoot)) {
            return null;
        }
        final QuarkusModel model = QuarkusGradleModelFactory.create(getBuildFile(projectRoot, BuildTool.GRADLE).toFile(),
                mode, tasks);
        QuarkusModelHelper.exportModel(model);
        return model;
    }

    public static QuarkusModel enableGradleAppModelForDevMode(Path projectRoot) throws IOException, AppModelResolverException {
        if (isMavenProject(projectRoot)) {
            return null;
        }
        final QuarkusModel model = QuarkusGradleModelFactory
                .createForTasks(getBuildFile(projectRoot, BuildTool.GRADLE).toFile(), DEVMODE_REQUIRED_TASKS);
        QuarkusModelHelper.exportModel(model);
        return model;
    }

}

@@ -621,16 +621,36 @@
 .gradle%22
+, %22build.gradle.kts%22
 );%0A%0A    
@@ -669,24 +669,26 @@
 final String
+%5B%5D
  buildFile;%0A
@@ -685,16 +685,17 @@
 uildFile
+s
 ;%0A%0A     
@@ -713,16 +713,19 @@
 l(String
+...
  buildFi
@@ -756,16 +756,17 @@
 uildFile
+s
  = build
@@ -803,16 +803,18 @@
 c String
+%5B%5D
  getBuil
@@ -818,16 +818,17 @@
 uildFile
+s
 () %7B%0A   
@@ -844,32 +844,289 @@
 return buildFile
+s;%0A        %7D%0A%0A        public boolean exists(Path root) %7B%0A            for (String buildFile : buildFiles) %7B%0A                if (Files.exists(root.resolve(buildFile))) %7B%0A                    return true;%0A                %7D%0A            %7D%0A            return false
 ;%0A        %7D%0A    
@@ -1307,37 +1307,47 @@
             if (
-Files
+BuildTool.MAVEN
 .exists(currentP
@@ -1353,48 +1353,8 @@
 Path
-.resolve(BuildTool.MAVEN.getBuildFile())
 )) %7B
@@ -1417,73 +1417,43 @@
 if (
-Files.exists(currentPath.resolve(BuildTool.GRADLE.getBuildFile())
+BuildTool.GRADLE.exists(currentPath
 )) %7B
@@ -1737,37 +1737,36 @@
             if (
-Files
+tool
 .exists(currentP
@@ -1772,37 +1772,8 @@
 Path
-.resolve(tool.getBuildFile())
 )) %7B

support kotlin dsl for gradle in model generation