import io.fourfinanceit.setup.Contracts
import io.fourfinanceit.setup.Custom
import io.fourfinanceit.setup.Maven
import io.fourfinanceit.setup.MicroInfra
import io.fourfinanceit.setup.Options
import io.fourfinanceit.setup.ConsoleNotPresentException

task setup doLast {
    println file('src/main/resources/banner.txt').text
    Console console = System.console()
    if (console) {
        Options options = new Options(maven(project, console),
                custom(project, console),
                contracts(project, console),
                microInfra(project, console))
        logger.info "Running setup with [${options}]"
        options.process(project)
    } else {
        logger.error "Cannot get console."
        throw new ConsoleNotPresentException()
    }
}

Maven maven(Project project, Console console) {
    console.println(">>> Maven configuration <<<")
    console.println(">>> Please put your maven user in a prop called: [mavenUser] and maven password in a prop called: [mavenPassword] in your [gradle.properties] <<<")
    console.println(">>> Current values are mavenUser: [${mavenUser}], mavenPassword: [${mavenPassword}] <<<\n")
    String passedMavenRepoUrl = console.readLine("> Please enter the Nexus URL. Current [${mavenRepoUrl}]: ") ?: mavenRepoUrl
    String passedMavenProjectGroupId = console.readLine("> Please enter your Maven Group Id. Current [${mavenProjectGroupId}]: ") ?: mavenProjectGroupId
    String passedMavenArtifactId = console.readLine("> Please enter your Maven Artifact Id. We will rename your project with this name (note that you'll have to rerun gradle to see the difference). Current [${project.name}]: ") ?: project.name
    return new Maven(project, mavenUser, mavenPassword, passedMavenRepoUrl, passedMavenProjectGroupId, passedMavenArtifactId)
}

Custom custom(Project project, Console console) {
    console.println("\n>>> Custom configuration <<<\n")
    String rootPackage = console.readLine("> Please enter the root packages that you want to have for your project. Current [${mavenProjectGroupId}]: ") ?: mavenProjectGroupId
    return new Custom(project, rootPackage, rootPackage != mavenProjectGroupId)
}

Contracts contracts(Project project, Console console) {
    console.println("\n>>> Spring Cloud Contract configuration <<<\n")
    String passedContractsRoot = console.readLine("> Please enter your relative path to the Contracts. Current [${contractsRoot}]: ") ?: contractsRoot
    String passedContractsBaseClass = console.readLine("> Please enter your base class for your Contract tests. Note! If you have changed the current directory structure you have to reflect that in the FQN name of your base class. Current [${contractsBaseClass}]: ") ?: contractsBaseClass
    return new Contracts(project, passedContractsRoot, passedContractsBaseClass, passedContractsRoot != contractsRoot)
}

MicroInfra microInfra(Project project, Console console) {
    console.println("\n>>> MicroInfra configuration <<<\n")
    String passedMicroInfraGraphiteHost = console.readLine("> Please enter the URL to Graphite. Current [${microInfraGraphiteHost}]: ") ?: microInfraGraphiteHost
    String passedMicroInfraMetricsPathApp = console.readLine("> Please enter the name of your app to present in Graphite. Current [${project.name}]: ") ?: project.name
    String passedMicroInfraStubrunnerStubsRepositoryRoot = console.readLine("> Please enter the URL of your Nexus where the stubs should be taken from. Current [${mavenRepoUrl}]: ") ?: mavenRepoUrl
    boolean passedMicroInfraStubrunnerUseMicroserviceDefinitions = console.readLine("> Please state whether you want to use the approach with having stubs in separate jars. Current [${microInfraStubrunnerUseMicroserviceDefinitions}]: ") ?: microInfraStubrunnerUseMicroserviceDefinitions
    return new MicroInfra(
            passedMicroInfraGraphiteHost,
            passedMicroInfraMetricsPathApp,
            passedMicroInfraStubrunnerStubsRepositoryRoot,
            passedMicroInfraStubrunnerUseMicroserviceDefinitions)
}
