apply plugin: 'com.android.library'
apply plugin: 'maven-publish'

android {
    compileSdkVersion rootProject.ext.compileSdkVersion

    defaultConfig {
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
    }

    buildTypes {
        debug {}
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])

    compile 'com.android.support:appcompat-v7:27.0.2'
    compile 'com.facebook.react:react-native:+'

    compile project(':react-native-background-timer')
    compile project(':react-native-fetch-blob')
    compile project(':react-native-immersive')
    compile project(':react-native-keep-awake')
    compile project(':react-native-locale-detector')
    compile project(':react-native-sound')
    compile project(':react-native-vector-icons')
    compile project(':react-native-webrtc')
    compile project(':react-native-calendar-events')

    testCompile 'junit:junit:4.12'
}

// Build process helpers
//

void runBefore(String dependentTaskName, Task task) {
    Task dependentTask = tasks.findByPath(dependentTaskName);
    if (dependentTask != null) {
        dependentTask.dependsOn task
    }
}

gradle.projectsEvaluated {
    android.buildTypes.all { buildType ->
        def buildNameCapitalized = "${buildType.name.capitalize()}"
        def bundlePath = "${buildDir}/intermediates/bundles/${buildType.name}"

        // Bundle fonts in react-native-vector-icons.
        //

        def currentFontTask = tasks.create(
                name: "copy${buildNameCapitalized}Fonts",
                type: Copy) {
            from("${projectDir}/../../fonts/jitsi.ttf")
            from("${projectDir}/../../node_modules/react-native-vector-icons/Fonts/")
            into("${bundlePath}/assets/fonts")
        }

        currentFontTask.dependsOn("merge${buildNameCapitalized}Resources")
        currentFontTask.dependsOn("merge${buildNameCapitalized}Assets")

        runBefore("processArmeabi-v7a${buildNameCapitalized}Resources", currentFontTask)
        runBefore("processX86${buildNameCapitalized}Resources", currentFontTask)
        runBefore("processUniversal${buildNameCapitalized}Resources", currentFontTask)
        runBefore("process${buildNameCapitalized}Resources", currentFontTask)

        def currentSoundsTask = tasks.create(
                name: "copy${buildNameCapitalized}Sounds",
                type: Copy) {
             from("${projectDir}/../../sounds/joined.wav")
             from("${projectDir}/../../sounds/left.wav")
             into("${bundlePath}/assets/sounds")
        }

        currentSoundsTask.dependsOn("merge${buildNameCapitalized}Resources")
        currentSoundsTask.dependsOn("merge${buildNameCapitalized}Assets")

        runBefore("processArmeabi-v7a${buildNameCapitalized}Resources", currentSoundsTask)
        runBefore("processX86${buildNameCapitalized}Resources", currentSoundsTask)
        runBefore("processUniversal${buildNameCapitalized}Resources", currentSoundsTask)
        runBefore("process${buildNameCapitalized}Resources", currentSoundsTask)

        // Bundle JavaScript and React resources.
        // (adapted from react-native/react.gradle)
        //

        // React JS bundle directories
        def jsBundleDir = file("${bundlePath}/assets")
        def resourcesDir = file("${bundlePath}/res/merged")
        def jsBundleFile = file("${jsBundleDir}/index.android.bundle")

        // Bundle task name for variant.
        def bundleJsAndAssetsTaskName = "bundle${buildNameCapitalized}JsAndAssets"

        def currentBundleTask = tasks.create(
                name: bundleJsAndAssetsTaskName,
                type: Exec) {
            // Set up inputs and outputs so gradle can cache the result.
            def reactRoot = file("${projectDir}/../../")
            inputs.files fileTree(dir: reactRoot, excludes: ['android/**', 'ios/**'])
            outputs.dir jsBundleDir
            outputs.dir resourcesDir

            // Set up the call to the react-native cli.
            workingDir reactRoot

            // Create JS bundle
            def devEnabled = !buildNameCapitalized.toLowerCase().contains('release')
            commandLine(
                'node',
                'node_modules/react-native/local-cli/cli.js',
                'bundle',
                '--assets-dest', resourcesDir,
                '--bundle-output', jsBundleFile,
                '--dev', "${devEnabled}",
                '--entry-file', 'index.android.js',
                '--platform', 'android',
                '--reset-cache')

            // Disable bundling on dev builds
            enabled !devEnabled
        }

        // Hook bundle${productFlavor}${buildType}JsAndAssets into the android build process
        currentBundleTask.dependsOn("merge${buildNameCapitalized}Resources")
        currentBundleTask.dependsOn("merge${buildNameCapitalized}Assets")

        runBefore("processArmeabi-v7a${buildNameCapitalized}Resources", currentBundleTask)
        runBefore("processX86${buildNameCapitalized}Resources", currentBundleTask)
        runBefore("processUniversal${buildNameCapitalized}Resources", currentBundleTask)
        runBefore("process${buildNameCapitalized}Resources", currentBundleTask)
    }
}

publishing {
    publications {
        aarArchive(MavenPublication) {
            groupId 'org.jitsi.react'
            artifactId 'jitsi-meet-sdk'
            version '1.9.0'

            artifact("${project.buildDir}/outputs/aar/${project.name}-release.aar") {
                extension "aar"
            }
            pom.withXml {
                def pomXml = asNode()
                pomXml.appendNode('name', 'jitsi-meet-sdk')
                pomXml.appendNode('description', 'Jitsi Meet SDK for Android')
                def dependencies = pomXml.appendNode('dependencies')
                configurations.getByName('releaseCompileClasspath').getResolvedConfiguration().getFirstLevelModuleDependencies().each {
                    // The (third-party) React Native modules that we depend on
                    // are in source code form and do not have groupId. That is
                    // why we have a dedicated groupId for them. But the other
                    // dependencies come through Maven and, consequently, have
                    // groupId.
                    def groupId = it.moduleGroup
                    def artifactId = it.moduleName

                    if (artifactId.startsWith('react-native-')
                            && groupId.equals('jitsi-meet')) {
                        groupId = rootProject.ext.moduleGroupId
                    }

                    def dependency = dependencies.appendNode('dependency')
                    dependency.appendNode('groupId', groupId)
                    dependency.appendNode('artifactId', artifactId)
                    dependency.appendNode('version', it.moduleVersion)
                }
            }
        }

    }
    repositories {
        maven { url "file:${rootProject.projectDir}/../../../jitsi/jitsi-maven-repository/releases" }
    }
}
