buildscript {
    repositories {
        mavenCentral()
        gradlePluginPortal()
    }
    dependencies {
        classpath 'org.jreleaser:jreleaser-gradle-plugin:1.18.0'
    }
}

ext {
    libgdxVersion = { ->
        def propsFile = new File(project.projectDir, 'gradle.properties')
        if (!propsFile.exists()) {
            throw new GradleException("gradle.properties file not found at ${propsFile.absolutePath}")
        }
        def lines = propsFile.readLines()
        for (line in lines) {
            if (line.startsWith('libgdx_version=')) {
                return line.split('=')[1].trim()
            }
        }
        throw new GradleException("libgdx_version property not found in gradle.properties")
    }()

    isReleaseBuild = {
        return project.hasProperty("RELEASE")
    }

    getSnapshotRepositoryUrl = {
        return project.hasProperty('SNAPSHOT_REPOSITORY_URL') ? SNAPSHOT_REPOSITORY_URL
                : "https://central.sonatype.com/repository/maven-snapshots/"
    }

    getRepositoryUsername = {
        return project.hasProperty('MAVEN_USERNAME') ? MAVEN_USERNAME : ""
    }

    getRepositoryPassword = {
        return project.hasProperty('MAVEN_PASSWORD') ? MAVEN_PASSWORD : ""
    }
}

allprojects {
    apply plugin: "java"

    group = 'com.esotericsoftware.spine'
    version = project.getProperty('version')

    sourceSets.main.java.srcDirs = ["src"]

    repositories {
        mavenLocal()
        maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
        maven { url "https://central.sonatype.com/repository/maven-snapshots/" }
        mavenCentral()
    }

    tasks.withType(JavaCompile).configureEach {
        sourceCompatibility = '1.8'
        targetCompatibility = '1.8'
        options.release.set(8)
    }
}

project("spine-libgdx") {
    apply plugin: "java-library"
    apply plugin: "maven-publish"
    apply plugin: "signing"

    sourceSets {
        main {
            resources {
                srcDirs = ["src"]  // Add this line to include non-Java files from src directory
                include "**/*.gwt.xml"  // Add this to specifically include GWT module files
            }
        }
    }

    dependencies {
        implementation "com.badlogicgames.gdx:gdx:$libgdxVersion"
    }
}
apply from: 'publishing.gradle'

project("spine-skeletonviewer") {
    jar {
        duplicatesStrategy = DuplicatesStrategy.INCLUDE

        manifest {
            attributes "Main-Class": "com.esotericsoftware.spine.SkeletonViewer"
        }

        from {
            configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
        }

        dependsOn(project(":spine-libgdx").tasks.named('jar'))
    }

    tasks.named('build').configure {
        dependsOn(tasks.named('jar'))
    }

    tasks.withType(JavaCompile).configureEach {
        sourceCompatibility = '1.8'
        targetCompatibility = '1.8'
        options.release.set(8)  // Ensures Java 8 bytecode is produced
    }
}

configure(subprojects - project("spine-libgdx")) {
    sourceSets.main.resources.srcDirs = ["assets"]

    dependencies {
        implementation project(":spine-libgdx")
        implementation "com.badlogicgames.gdx:gdx:$libgdxVersion"
        implementation "com.badlogicgames.gdx:gdx-platform:$libgdxVersion:natives-desktop"
        implementation "com.badlogicgames.gdx:gdx-backend-lwjgl3:$libgdxVersion"
        implementation "com.badlogicgames.gdx:gdx-box2d:$libgdxVersion"
        implementation "com.badlogicgames.gdx:gdx-box2d-platform:$libgdxVersion:natives-desktop"
    }
}

tasks.withType(JavaCompile).configureEach {
    println "Building with sourceCompatibility = ${sourceCompatibility}, targetCompatibility = ${targetCompatibility}"
}
