buildscript {
    ext.production = (findProperty('prod') ?: 'false') == 'true'
    ext.npmdeps = new File("npm.dependencies").getText()

    repositories {
        jcenter()
        maven { url = "https://dl.bintray.com/kotlin/kotlin-eap" }
        maven { url = "https://plugins.gradle.org/m2/" }
        maven { url = "https://bintray.com/kotlin/kotlin-eap/dokka" }
        maven { url = "https://kotlin.bintray.com/kotlinx" }
    }

    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}"
        classpath "org.jetbrains.kotlin:kotlin-frontend-plugin:${frontendPluginVersion}"
        classpath "gradle.plugin.io.gitlab.arturbosch.detekt:detekt-gradle-plugin:${detektVersion}"
        classpath "org.jetbrains.dokka:dokka-gradle-plugin:${dokkaVersion}"
        classpath "org.jetbrains.kotlinx:kotlinx-gradle-serialization-plugin:${serializationVersion}"
        classpath "io.spring.gradle:dependency-management-plugin:${dependencyManagementPluginVersion}"
    }
}

plugins {
    id "com.jfrog.bintray" version "1.7.3"
}

allprojects {
    apply plugin: 'com.jfrog.bintray'
    apply plugin: 'maven'
    apply plugin: 'maven-publish'
    apply from: rootProject.file('pom.gradle')
    apply from: rootProject.file('bintray.gradle')

    repositories {
        jcenter()
        maven { url = 'https://kotlin.bintray.com/kotlinx' }
        maven { url = 'https://dl.bintray.com/gbaldeck/kotlin' }
        maven { url = 'https://dl.bintray.com/rjaros/kotlin' }
    }

    afterEvaluate {
        task sourceJar(type: Jar, dependsOn: classes) {
            classifier 'sources'
            from sourceSets.main.kotlin
            duplicatesStrategy = "exclude"
        }
    }

    task emptyJar(type: Jar) {
        classifier 'javadoc'
    }

    tasks.build.dependsOn(['sourceJar', 'emptyJar'])

    publishing {
        publications {
            mavenProject(MavenPublication) {
                from components.java
                groupId project.group
                artifactId project.name
                version project.version

                artifact sourceJar {
                    classifier 'sources'
                }

                artifact emptyJar {
                    classifier 'javadoc'
                }

                withPom(pom)
            }
        }
    }

    model {
        tasks.generatePomFileForMavenProjectPublication {
            destination = file("$buildDir/libs/${project.name}-${version}.pom")
        }
    }
}

if (!project.gradle.startParameter.taskNames.contains("dokka")) {
    apply plugin: 'kotlin-platform-js'
} else {
    apply plugin: 'kotlin'
}
apply plugin: 'org.jetbrains.kotlin.frontend'
apply plugin: 'io.gitlab.arturbosch.detekt'
apply plugin: 'org.jetbrains.dokka'
apply plugin: 'kotlinx-serialization'

dependencies {
    if (!project.gradle.startParameter.taskNames.contains("dokka")) {
        expectedBy project(':kvision-common')
        compile "org.jetbrains.kotlin:kotlin-stdlib-js:$kotlinVersion"
        compile "org.jetbrains.kotlinx:kotlinx-coroutines-core-js:$coroutinesVersion"
        compile "org.jetbrains.kotlinx:kotlinx-serialization-runtime-js:$serializationVersion"
        if (!project.gradle.startParameter.taskNames.contains("generatePomFileForMavenProjectPublication")) {
            compile "org.jetbrains.kotlin:kotlin-test-js:$kotlinVersion"
        }
        testCompile "org.jetbrains.kotlin:kotlin-test-js:$kotlinVersion"
    } else {
        compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
        compile "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion"
        compile "org.jetbrains.kotlinx:kotlinx-serialization-runtime:$serializationVersion"
        testCompile "org.jetbrains.kotlin:kotlin-test:$kotlinVersion"
    }
    compile "com.github.snabbdom:snabbdom-kotlin:0.1.1"
    compile "pl.treksoft:navigo-kotlin:0.0.2"
    compile "pl.treksoft:jquery-kotlin:0.0.3"
    compile "pl.treksoft:kotlin-observable-js:0.0.3"
}

kotlinFrontend {
    npm {
        dependency("css-loader")
        dependency("style-loader")
        dependency("less")
        dependency("less-loader")
        dependency("imports-loader")
        dependency("uglifyjs-webpack-plugin")
        dependency("file-loader")
        dependency("url-loader")
        dependency("jquery", "3.2.1")
        dependency("fecha", "2.3.2")
        dependency("snabbdom", "0.7.1")
        dependency("snabbdom-virtualize", "0.7.0")
        dependency("navigo", "7.0.0")
        npmdeps.eachLine { line ->
            def (name, version) = line.tokenize(" ")
            dependency(name, version)
        }
        devDependency("karma")
        devDependency("qunit")
    }

    webpackBundle {
        bundleName = "main"
        contentPath = file('src/main/web')
        mode = production ? "production" : "development"
    }

    define "PRODUCTION", production

}

detekt {
    version = "${detektVersion}"
    profile("main") {
        input = "$projectDir/src/main/kotlin"
        config = "$projectDir/detekt.yml"
        filters = ".*test.*,.*/resources/.*,.*/tmp/.*"
    }
}

dokka {
    includes = ['Module.md']
    classpath = [new File("dokka/kvision-dokka-helper.jar")]
    outputFormat = 'html'
    outputDirectory = "$buildDir/kdoc"
    reportUndocumented = false
}

task cleanLibs(type: Delete) {
    delete 'build/js', 'build/libs'
}

if (project.gradle.startParameter.taskNames.contains("jar")) {
    compileKotlin2Js.dependsOn 'cleanLibs'
}

jar {
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE
    excludes = ["package.json"]
}

if (!project.gradle.startParameter.taskNames.contains("dokka")) {

    compileKotlin2Js {
        kotlinOptions.metaInfo = true
        kotlinOptions.outputFile = "$project.buildDir.path/js/${project.name}.js"
        kotlinOptions.sourceMap = !production
        kotlinOptions.moduleKind = 'umd'
    }

    compileTestKotlin2Js {
        kotlinOptions.metaInfo = true
        kotlinOptions.outputFile = "$project.buildDir.path/js-tests/${project.name}-tests.js"
        kotlinOptions.sourceMap = !production
        kotlinOptions.moduleKind = 'umd'
    }

}

task copyResources(type: Copy) {
    from "src/main/resources"
    into file(buildDir.path + "/js")
}

task copyResourcesForTests(type: Copy) {
    from "src/main/resources"
    into file(buildDir.path + "/js-tests/")
}

if (!project.gradle.startParameter.taskNames.contains("dokka")) {
    afterEvaluate {
        tasks.getByName("webpack-bundle") { dependsOn(copyResources) }
        tasks.getByName("webpack-run") { dependsOn(copyResources) }
        tasks.getByName("karma-start") { dependsOn(copyResources, copyResourcesForTests) }
    }
}