/*
 * This file was generated by the Gradle 'init' task.
 *
 * This generated file contains a sample Kotlin application project to get you started.
 * For more details take a look at the 'Building Java & JVM projects' chapter in the Gradle
 * User Manual available at https://docs.gradle.org/7.5/userguide/building_java_projects.html
 */
buildscript {
  ext.kotlin_version = "2.0.20"

  dependencies {
    classpath "com.google.protobuf:protobuf-gradle-plugin:0.9.4"
  }
}

plugins {
    // Apply the org.jetbrains.kotlin.jvm Plugin to add support for Kotlin.
    id "org.jetbrains.kotlin.jvm" version "2.0.20"
    id "org.jetbrains.kotlin.plugin.allopen" version "2.0.20"


    id "application"

    id "com.github.johnrengelman.shadow" version "8.1.1"


    id "nu.studer.jooq" version "8.0"

    id "java"
}

apply plugin: "kotlin"
apply plugin: "com.google.protobuf"

repositories {
    maven {
        url = "https://plugins.gradle.org/m2/"
    }

    mavenCentral()
    mavenLocal()
}

version = "0.0.1"
ext {
  vertxVersion = "4.5.12"
  protoVersion = "3.22.5"
  grpcVersion = "1.61.0"
}
def mainVerticleName = "golf.handicap.vertx.MainVerticle"
def launcherClassName = "io.vertx.core.Launcher"
def junitJupiterVersion = "5.7.0"
def watchForChange = "src/main/**/*"
def doOnChange = "${projectDir}/gradlew classes"
def OS = System.getProperty("os.name").toLowerCase();
def javaTargetVersion = 17

application {
  getMainClass().set(launcherClassName)
}

version = "3.3.0"

java {
    sourceCompatibility = JavaVersion.VERSION_17
    targetCompatibility = JavaVersion.VERSION_17
}

tasks.register('copyDatabaseConfig', Copy) {
    dependsOn gradle.includedBuild("generate").task(":build")
}
/*
  Using a java class to generate jooq objects - allows for multiple db's - see ./generate directory
  Composite build - implementation("dmo.fs.utils:generate:0.0.1") - this avoids the circular dependancy error
*/
tasks.register('jooqGenerate', JavaExec) {
    group = "Execution"
    doFirst {
        classpath = sourceSets.main.runtimeClasspath
    }
    // Needs work on setting up Jooq Code Generator for "mariadb"
    def defaultValue = environment.get("DEFAULT_DB")
    if("mariadb" == defaultValue) {
        environment.remove("DEFAULT_DB")
    }
    environment "VERTXWEB_ENVIRONMENT", "dev"
    mainClass = "dmo.fs.utils.JooqGenerate"
    args = ["dev"]
    dependsOn copyDatabaseConfig
}

run {
  doFirst {
     if (System.getProperty("DEBUG", "false") == "true") {
        debug = "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005"
    }

    environment "VERTXWEB_ENVIRONMENT", "dev"

    args = ['run', mainVerticleName, "--redeploy=$watchForChange", "--launcher-class=$launcherClassName", 
      "--on-redeploy=$doOnChange", "--conf=src/main/resources/application-conf.json",
      "--java-opts=-Dvertx.disableFileCaching=true", "-PcompileOnly", debug]
  }
  if (project.hasProperty("compileOnly")) {
    dependsOn jooqGenerate
  }
}

kotlin {
  // change if out of memory
  jvmToolchain(javaTargetVersion)
  kotlinDaemonJvmArgs = ["-Xmx512m", "-Xms256m", "-XX:+UseParallelGC"]
}

compileKotlin {
   if (project.hasProperty("compileOnly")) {
    dependsOn jooqGenerate
   }
}

distZip {
    dependsOn("shadowJar")
}

// The standard protobuf block, same as normal gRPC Java projects
protobuf {
  protoc { artifact = "com.google.protobuf:protoc:${protoVersion}" }
    plugins {
      grpc { artifact = "io.grpc:protoc-gen-grpc-java:${grpcVersion}" }
      vertx {
        artifact = "io.vertx:vertx-grpc-protoc-plugin"
      }
    }
    generateProtoTasks {
      all()*.plugins { grpc {} }
    }
}

dependencies {
    implementation(platform("io.vertx:vertx-stack-depchain:$vertxVersion"))
    shadow gradleApi()
    implementation("io.vertx:vertx-core:$vertxVersion")
    implementation("io.vertx:vertx-rx-java3:$vertxVersion")
    implementation("io.vertx:vertx-config:$vertxVersion")
    implementation("io.vertx:vertx-web:$vertxVersion")
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
    implementation "io.vertx:vertx-web-api-contract:$vertxVersion"
    implementation("io.vertx:vertx-jdbc-client:$vertxVersion")
    implementation("io.vertx:vertx-web-templ-thymeleaf:$vertxVersion")
    implementation("io.vertx:vertx-grpc:$vertxVersion")
    implementation("io.vertx:vertx-grpc-client:$vertxVersion")
    implementation("io.agroal:agroal-pool:2.5")
    implementation group: 'com.h2database', name: 'h2', version: '2.2.224'
    implementation("org.xerial:sqlite-jdbc:3.46.0.0")
    implementation("io.vertx:vertx-mysql-client:$vertxVersion")
    implementation("io.vertx:vertx-pg-client:$vertxVersion")
    implementation('org.mariadb.jdbc:mariadb-java-client:3.4.1')
    implementation('org.jooq:jooq')
    implementation('org.jooq:jooq-codegen')
    implementation('org.jooq:jooq-meta')
    implementation("javax.annotation:javax.annotation-api:1.3.2")
    runtimeOnly("org.postgresql:postgresql:42.7.3")

    implementation "io.vertx:vertx-grpc-server:$vertxVersion"

    implementation 'io.github.microutils:kotlin-logging-jvm:2.0.11'

    testImplementation("io.vertx:vertx-lang-kotlin:$kotlin_version")
    testImplementation("io.vertx:vertx-lang-kotlin-coroutines:$kotlin_version")
    testImplementation("io.vertx:vertx-junit5:$vertxVersion")
    testImplementation("org.junit.jupiter:junit-jupiter:$junitJupiterVersion")
    testImplementation("junit:junit:4.13.2")

    implementation("dmo.fs.utils:generate:0.0.1")
}

apply plugin: "nu.studer.jooq"

shadowJar {
  zip64 = true

  archiveClassifier = "fat"

  mergeServiceFiles {
    include "META-INF/services/io.vertx.core.spi.VerticleFactory"
  }

  manifest{
    attributes 'Main-Verticle': mainVerticleName
  }
}

test {
    // Use junit platform for unit tests
    environment "VERTXWEB_ENVIRONMENT", "test"
    useJUnitPlatform()
    testLogging {
        events "PASSED", "FAILED", "SKIPPED"
    }
}
