//noinspection GradleDependency
buildscript {
    ext.min_sdk_version = "10.0.0"
}

plugins {
    id "com.github.node-gradle.node" version "2.2.4"
}
apply plugin: "com.android.library"
apply plugin: "maven-publish"
apply plugin: "checkstyle"
apply plugin: "kotlin-android"

android {
    namespace = "com.acmerobotics.dashboard"

    compileSdkVersion 28

    defaultConfig {
        minSdkVersion 23
        //noinspection ExpiredTargetSdkVersion
        targetSdkVersion 26
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    lintOptions {
        abortOnError false
    }
}

node {
    version = "18.12.1"
    download = true
    nodeModulesDir = file("${project.projectDir}/../client")
}

yarn_build.dependsOn yarn_install

task cleanDashAssets(type: Delete) {
    delete "${android.sourceSets.getByName("main").assets.srcDirs[0]}/dash"
}

clean.dependsOn cleanDashAssets

task copyDashAssets(type: Copy) {
    from "${project.projectDir}/../client/dist"
    into "${android.sourceSets.getByName("main").assets.srcDirs[0]}/dash"
}

copyDashAssets.dependsOn cleanDashAssets
copyDashAssets.dependsOn yarn_build

android.libraryVariants.all { variant ->
    variant.preBuildProvider.get().dependsOn copyDashAssets
}

// adapted from https://github.com/Genymobile/gnirehtet/blob/master/config/android-checkstyle.gradle
check.dependsOn "checkstyle"

checkstyle {
    toolVersion = checkstyle_version
}

task checkstyle(type: Checkstyle) {
    description = "Check Java style with Checkstyle"
    configFile = rootProject.file("config/checkstyle/checkstyle.xml")
    source = javaSources()
    classpath = files()
    ignoreFailures = true
}

// I"d like to replace each() with collectMany() if only I knew how
// Also, the static suggestion here is wrong but I lack the incantation to suppress it
def javaSources() {
    def files = []
    android.sourceSets.each { sourceSet ->
        sourceSet.java.each { javaSource ->
            javaSource.getSrcDirs().each {
                if (it.exists()) {
                    files.add(it)
                }
            }
        }
    }
    return files
}

repositories {
    files("../libs")
    mavenCentral()
}

// Probably not worth combining with addPomDependencies()
// Keep them in sync manually
dependencies {
    api(project(":DashboardCore")) {
        transitive = false
    }

    api "org.firstinspires.ftc:RobotCore:$min_sdk_version"
    api "org.firstinspires.ftc:Hardware:$min_sdk_version"
    api "org.firstinspires.ftc:RobotServer:$min_sdk_version"
    api "org.firstinspires.ftc:FtcCommon:$min_sdk_version"

    implementation "androidx.annotation:annotation:1.1.0"

    implementation("org.nanohttpd:nanohttpd-websocket:2.3.1") {
        exclude module: "nanohttpd"
    }
}

def addPomDependencies(root) {
    def depsNode = root.appendNode("dependencies")

    // Apparently you can make closures but not "inner methods"
    def addDep = { groupId, artifactId, version ->
        def n = depsNode.appendNode("dependency")
        n.appendNode("groupId", groupId)
        n.appendNode("artifactId", artifactId)
        n.appendNode("version", version)
        return n
    }

    addDep("com.acmerobotics.dashboard", "core", dashboard_version)

    ["RobotCore", "Hardware", "RobotServer", "FtcCommon"].forEach {
        addDep("org.firstinspires.ftc", it, min_sdk_version)
    }

    addDep("androidx.annotation", "annotation", "1.1.0")

    addDep("org.nanohttpd", "nanohttpd-websocket", "2.3.1")
            .appendNode("exclusions")
            .appendNode("exclusion")
            .with {
                appendNode("groupId", "org.nanohttpd")
                appendNode("artifactId", "nanohttpd")
            }
}

task androidSourcesJar(type: Jar) {
    archiveClassifier = "sources"
    from android.sourceSets.main.java.srcDirs
}

task androidJavadocs(type: Javadoc) {
    failOnError = false
    source = android.sourceSets.main.java.srcDirs
    ext.androidJar = "${android.sdkDirectory}/platforms/${android.compileSdkVersion}/android.jar"
    classpath += files(ext.androidJar)
}

task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) {
    archiveClassifier = "javadoc"
    from androidJavadocs.destinationDir
}

project.afterEvaluate {
    publishing {
        publications {
            maven(MavenPublication) {
                artifact bundleReleaseAar
                artifact androidSourcesJar
                artifact androidJavadocsJar

                groupId = "com.acmerobotics.dashboard"
                artifactId = "dashboard"
                version dashboard_version

                pom {
                    description = "Web dashboard designed for FTC"
                    name = "FTC Dashboard"
                    url = "https://github.com/acmerobotics/ftc-dashboard"

                    licenses {
                        license {
                            name = "The MIT License"
                            url = "https://opensource.org/licenses/MIT"
                            distribution = "repo"
                        }
                    }

                    developers {
                        developer {
                            id = "rbrott"
                            name = "Ryan Brott"
                            email = "rcbrott@gmail.com"
                        }
                    }

                    scm {
                        url = "https://github.com/acmerobotics/ftc-dashboard"
                    }
                }

                // Seems like you can"t define ad hoc software components without a plugin.
                // Remind me never to read the Gradle docs again.
                pom.withXml {
                    // Android Studio complains here, but passing null straight up doesn't work
                    //noinspection GroovyImplicitNullArgumentCall
                    def root = asNode()
                    addPomDependencies(root)
                }
            }
        }
    }
}
