buildscript {
    ext.production = (findProperty('prod') ?: 'false') == 'true'

    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-serialization:${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 "io.spring.gradle:dependency-management-plugin:${dependencyManagementPluginVersion}"
    }
}

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

configure(allprojects - project(':kvision-modules')) {
    repositories {
        jcenter()
        maven { url = "https://dl.bintray.com/kotlin/kotlin-eap" }
        maven { url = 'https://kotlin.bintray.com/kotlinx' }
        maven { url = 'https://dl.bintray.com/gbaldeck/kotlin' }
        maven { url = 'https://dl.bintray.com/rjaros/kotlin' }
        mavenLocal()
    }
}

configure(allprojects - project(':kvision-modules') - project(":kvision-modules:kvision-base")) {
    apply plugin: 'io.gitlab.arturbosch.detekt'
    apply plugin: 'com.jfrog.bintray'
    apply plugin: 'maven'
    apply plugin: 'maven-publish'
    apply from: rootProject.file('pom.gradle')
    apply from: rootProject.file('bintray.gradle')

    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'])

    detekt {
        config = files(rootProject.projectDir.path + "/detekt.yml")
        filters = ".*test.*,.*/resources/.*,.*/tmp/.*"
    }

    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'
    apply plugin: 'org.jetbrains.kotlin.frontend'
} else {
    apply plugin: 'kotlin'
}
apply plugin: 'org.jetbrains.dokka'
apply plugin: 'kotlinx-serialization'

dependencies {
    if (!project.gradle.startParameter.taskNames.contains("dokka")) {
        expectedBy project('kvision-modules:kvision-common-types')
        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.3"
    compile "pl.treksoft:jquery-kotlin:0.0.4"
    compile "pl.treksoft:kotlin-observable-js:0.0.4"
}

if (!project.gradle.startParameter.taskNames.contains("dokka")) {
    kotlinFrontend {
        npm {
            dependency("css-loader", "2.0.1")
            dependency("style-loader", "0.23.1")
            dependency("less", "3.9.0")
            dependency("less-loader", "4.1.0")
            dependency("imports-loader", "0.8.0")
            dependency("uglifyjs-webpack-plugin", "2.0.1")
            dependency("file-loader", "2.0.0")
            dependency("url-loader", "1.1.2")
            dependency("jquery", "3.2.1")
            dependency("fecha", "2.3.2")
            dependency("snabbdom", "0.7.3")
            dependency("snabbdom-virtualize", "0.7.0")
            dependency("jquery-resizable-dom", "0.32.0")
            dependency("element-resize-event", "3.0.3")
            dependency("navigo", "7.0.0")
            devDependency("karma", "3.1.4")
            devDependency("qunit", "2.8.0")
        }

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

        define "PRODUCTION", production

    }
}

dokka {
    includes = ['Module.md']
    sourceDirs = files('kvision-modules/kvision-bootstrap/src/main/kotlin',
            'kvision-modules/kvision-select/src/main/kotlin',
            'kvision-modules/kvision-datetime/src/main/kotlin',
            'kvision-modules/kvision-spinner/src/main/kotlin',
            'kvision-modules/kvision-richtext/src/main/kotlin',
            'kvision-modules/kvision-upload/src/main/kotlin',
            'kvision-modules/kvision-handlebars/src/main/kotlin',
            'kvision-modules/kvision-i18n/src/main/kotlin',
            'kvision-modules/kvision-remote/src/main/kotlin',
            'kvision-modules/kvision-select-remote/src/main/kotlin',
            'kvision-modules/kvision-common/src/main/kotlin',
            'kvision-modules/kvision-common-types/src/main/kotlin',
            'kvision-modules/kvision-server-jooby/src/main/kotlin',
            'kvision-modules/kvision-server-spring-boot/src/main/kotlin')
    classpath = [new File("dokka/kvision-dokka-helper.jar")]
    outputFormat = 'html'
    outputDirectory = "$buildDir/kdoc"
    reportUndocumented = false
}

task testJar(type: Jar, dependsOn: testClasses) {
    baseName = "${project.archivesBaseName}-test"
    from sourceSets.test.output
}

configurations {
    tests
}

artifacts {
    tests testJar
}

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

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

jar {
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE
}

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) }
    }
}