// stb_image url for download
String stbiUrl = 'http://www.nothings.org/stb_image.c'
String stbiDownloadTarget = 'stb_image.c'

// stb_image is not downloaded.  The single source file is included in the repo
String stbiFolder = 'stb_image'

//Working directories for the ndk build.
String stbiBuildDir = "${buildDir}" + File.separator + 'stb_image'
String stbiBuildJniDir = stbiBuildDir + File.separator + 'jni'
String stbiBuildLibsDir = stbiBuildDir + File.separator + 'libs'

//Pre-compiled libs directory
String stbiPreCompiledLibsDir = 'libs' + File.separator + 'stb_image'

// jME Android Native source files path
String stbiJmeAndroidPath = 'src/native/jme_stbi'

// Download external source files if not available
task downloadStbImage(type: MyDownload) {
    sourceUrl = stbiUrl
    target = file(stbiFolder + File.separator + stbiDownloadTarget)
}

// Copy stb_image files to jni directory
task copyStbiFiles(type: Copy) {
    def sourceDir = file(stbiFolder)
    def outputDir = file(stbiBuildJniDir)
//    println "copyStbiFiles sourceDir: " + sourceDir
//    println "copyStbiFiles outputDir: " + outputDir

    from sourceDir
    into outputDir
}
copyStbiFiles.dependsOn {
    def stbiFilePath = project.projectDir.absolutePath + stbiFolder + File.separator + stbiDownloadTarget
    def stbiFile = new File(stbiFilePath)
//    println "zipFile path: " + zipFile.absolutePath
//    println "zipFile exists: " + zipFile.exists()
    if (!stbiFile.exists()) {
        downloadStbImage
    }
}


// Copy jME Android native files to jni directory
task copyStbiJmeFiles(type: Copy, dependsOn:copyStbiFiles) {
    def sourceDir = file(stbiJmeAndroidPath)
    def outputDir = file(stbiBuildJniDir)
//    println "copyStbiJmeFiles sourceDir: " + sourceDir
//    println "copyStbiJmeFiles outputDir: " + outputDir

    from sourceDir
    into outputDir
}

task generateStbiHeaders(dependsOn: copyStbiJmeFiles) << {
    String destDirPath = stbiBuildJniDir
    String classes = ""
            .concat("com.jme3.texture.plugins.AndroidNativeImageLoader, ")

//    println "stb_image classes = " + classes
//    println "stb_image destDir = " + destDir
//    println "stb_image classpath = " + project.projectClassPath

    ant.javah(
        classpath: project.projectClassPath,
        destdir: destDirPath,
        class: classes
    )

}

task buildStbiNativeLib(type: Exec, dependsOn: generateStbiHeaders) {
//    println "stb_image build dir: " + buildLibDir
//    println "ndkCommandPath: " + project.ndkCommandPath
    args 'TARGET_PLATFORM=android-9'
    workingDir stbiBuildDir
    executable rootProject.ndkCommandPath
}

task updatePreCompiledStbiLibs(type: Copy, dependsOn: buildStbiNativeLib) {
    def sourceDir = new File(stbiBuildLibsDir)
    def outputDir = new File(stbiPreCompiledLibsDir)
//    println "updatePreCompiledStbiLibs sourceDir: " + sourceDir
//    println "updatePreCompiledStbiLibs outputDir: " + outputDir

    from sourceDir
    into outputDir
}

// Copy pre-compiled libs to build directory (when not building new libs)
task copyPreCompiledStbiLibs(type: Copy) {
    def sourceDir = file(stbiPreCompiledLibsDir)
    def outputDir = file(stbiBuildLibsDir)
//    println "copyStbiJmeFiles sourceDir: " + sourceDir
//    println "copyStbiJmeFiles outputDir: " + outputDir

    from sourceDir
    into outputDir
}

if (rootProject.ndkExists) {
    // build native libs and update stored pre-compiled libs to commit
    compileJava.dependsOn { updatePreCompiledStbiLibs }
} else {
    // use pre-compiled native libs (not building new ones)
    compileJava.dependsOn { copyPreCompiledStbiLibs }
}

jar.into("lib") { from stbiBuildLibsDir }

// Helper class to wrap ant dowload task
class MyDownload extends DefaultTask {
    @Input
    String sourceUrl

    @OutputFile
    File target

    @TaskAction
    void download() {
       ant.get(src: sourceUrl, dest: target)
    }
}