buildscript {
    repositories {
        mavenCentral()
        mavenLocal()
    }
    dependencies {
        classpath 'net.sf.proguard:proguard-gradle:5.0'
        classpath 'org.ajoberstar:gradle-git:1.2.0'
        classpath 'com.badlogicgames.gdx:gdx-tools:1.6.5'
        classpath 'org.kt3k.gradle.plugin:coveralls-gradle-plugin:2.0.1'
    }
}


allprojects {
    apply plugin: "eclipse"
    apply plugin: "idea"

    version = '0.1.3'
    ext {
        appName = 'Overlap2D'
        gdxVersion = '1.9.8'
        box2DLightsVersion = '1.4'
        visuiVersion = '1.4.1'

        packMain = ["overlap2d/art/textures", "overlap2d/assets/style/", "uiskin"]
        packSplash = ["overlap2d/art/splash_textures", "overlap2d/assets/splash/", "splash"]
    }

    repositories {
        mavenLocal()
        mavenCentral()
        maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
        maven { url "https://oss.sonatype.org/content/repositories/releases/" }
        jcenter()
    }
}

apply plugin: "java"
apply plugin: 'jacoco'
apply plugin: 'com.github.kt3k.coveralls'

import com.badlogic.gdx.tools.texturepacker.TexturePacker
import org.ajoberstar.grgit.Grgit

//def spineDestination = file("spine-runtimes")
//if (!spineDestination.exists()) Grgit.clone(dir: spineDestination, uri: 'https://github.com/EsotericSoftware/spine-runtimes.git')
def o2dRuntimeDestination = file("overlap2d-runtime-libgdx")
if (!o2dRuntimeDestination.exists()) 
	Grgit.clone(dir: o2dRuntimeDestination, uri: 'https://github.com/taluks/overlap2d-runtime-libgdx.git')

dependencies {
    compile "com.kotcrab.vis:vis-ui:$visuiVersion"
    compile project(":overlap2d-common-api")
    compile project(":overlap2d-runtime-libgdx")
    //compile project(":spine-runtime-libgdx")
    compile group: 'com.esotericsoftware.spine', name: 'spine-libgdx', version: '3.5.51.1'
    compile project(":spine-o2d-extension")
    compile fileTree(dir: 'libs', include: '*.jar')
    compile "org.apache.commons:commons-io:1.3.2"
    compile "org.apache.commons:commons-lang3:3.3.2"
    compile "com.badlogicgames.gdx:gdx-backend-lwjgl:$gdxVersion"
    compile "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop"
    compile "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-desktop"
    compile "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-desktop"
    compile "com.badlogicgames.gdx:gdx-tools:$gdxVersion"
    compile "com.badlogicgames.gdx:gdx:$gdxVersion"
    compile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"
    compile "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
    compile "com.badlogicgames.box2dlights:box2dlights:$box2DLightsVersion"
    compile "com.mortennobel:java-image-scaling:0.8.5"
    testCompile 'junit:junit:4.12'
    testCompile 'com.google.guava:guava:18.0'
    testCompile 'org.hamcrest:hamcrest-library:1.3'
    testCompile 'org.mockito:mockito-core:1.10.19'
    testCompile 'org.powermock:powermock-module-junit4:1.6.2'
    testCompile 'org.powermock:powermock-api-mockito:1.6.2'
}

task packTextures << {
    if (project.ext.has('packMain')) {
        logger.info "Calling TexturePacker: " + packMain
        TexturePacker.processIfModified(file(packMain[0]).absolutePath, file(packMain[1]).absolutePath, packMain[2])
    }
    if (project.ext.has('packSplash')) {
        logger.info "Calling TexturePacker: " + packSplash
        TexturePacker.processIfModified(file(packSplash[0]).absolutePath, file(packSplash[1]).absolutePath, packSplash[2])
    }
}
build.dependsOn packTextures

project(":overlap2d-runtime-libgdx") {
    apply plugin: "java"
    sourceSets.main.java.srcDirs = ["src/"]
    sourceSets.test.java.srcDirs = ["test/"]

    dependencies {
        compile "com.badlogicgames.gdx:gdx:$gdxVersion"
        compile fileTree(dir: 'libs', include: '*.jar')
    }
}


project(":spine-o2d-extension") {
    apply plugin: "java"
    sourceSets.main.java.srcDirs = ["src/"]

    dependencies {
        //compile project(":spine-runtime-libgdx");
        compile group: 'com.esotericsoftware.spine', name: 'spine-libgdx', version: '3.5.51.1'
        compile project(":overlap2d-runtime-libgdx");
        compile fileTree(dir: 'libs', include: '*.jar')
    }
}

/*project(":spine-runtime-libgdx") {
    apply plugin: "java"
    sourceSets.main.java.srcDirs = ["src/"]

    dependencies {
        compile "com.badlogicgames.gdx:gdx:$gdxVersion"
    }
}*/

project(":overlap2d-common-api") {
    apply plugin: "java"
    sourceSets.main.java.srcDirs = ["src/"]

    dependencies {
        compile "com.badlogicgames.gdx:gdx:$gdxVersion"
    }
}


sourceCompatibility = 1.8
sourceSets {
    main {
        java {
            srcDir 'overlap2d/src/'
        }
    }
    test {
        java {
            srcDir 'overlap2d/test/java'
        }
        resources {
            srcDir 'overlap2d/test/resources'
        }
    }
}

project.ext.mainClassName = "com.uwsoft.editor.Main"
project.ext.assetsDir = new File("overlap2d/assets/");


task run(dependsOn: classes, type: JavaExec) {
    main = project.mainClassName
    classpath = sourceSets.main.runtimeClasspath
    standardInput = System.in
    workingDir = project.assetsDir
    ignoreExitValue = true
}

task dist(type: Jar) {
    from files(sourceSets.main.output.classesDir)
    from files(sourceSets.main.output.resourcesDir)
    from { configurations.compile.collect { zipTree(it) } }
    from files(project.assetsDir);

    manifest {
        attributes 'Main-Class': project.mainClassName
    }
}

idea {
    project {
        languageLevel = sourceCompatibility
    }
}

task afterEclipseImport(description: "Post processing after project generation", group: "IDE") {
    doLast {
        def classpath = new XmlParser().parse(file(".classpath"))
        new Node(classpath, "classpathentry", [kind: 'src', path: 'overlap2d/assets']);
        def writer = new FileWriter(file(".classpath"))
        def printer = new XmlNodePrinter(new PrintWriter(writer))
        printer.setPreserveWhitespace(true)
        printer.print(classpath)
    }
}

tasks.eclipse.doLast {
    delete ".project"
}

if (JavaVersion.current().isJava8Compatible()) {
    allprojects {
        tasks.withType(Javadoc) {
            options.addStringOption('Xdoclint:none', '-quiet')
        }
    }
}

task wrapper(type: Wrapper) {
    gradleVersion = '2.6'
}

jacocoTestReport {
    reports {
        xml.enabled = true // coveralls plugin depends on xml format report
        html.enabled = true
    }
}

task checkTest() << {
    def fails = 0;
    def count = 0;
    fileTree('build/test-results') {
        include '*.xml'
    }.each { File file ->
        def testResult = (new XmlParser()).parse(file)
        count += Integer.valueOf(testResult.@tests);
        fails += Integer.valueOf(testResult.@failures);
        fails += Integer.valueOf(testResult.@errors);
        if (Integer.valueOf(testResult.@failures) + Integer.valueOf(testResult.@errors) > 0) {
            println "Test ${testResult.@name} failed";
        }
    }
    println "All test:" + count;
    println "Failed test:" + fails;
    if (fails > 0) {
        throw new Exception("${fails} test failed");
    } else if (count == 0) {
        throw new Exception("Test don't finish successful");
    }
}

check.dependsOn jacocoTestReport
test.dependsOn packTextures