import groovy.swing.SwingBuilder

apply plugin: 'com.android.application'
apply plugin: 'findbugs'

System.setProperty('java.awt.headless', 'false')

android {
    compileSdkVersion 25
    buildToolsVersion '26.0.2'

    defaultConfig {
        applicationId PACKAGE_NAME
        minSdkVersion 10
        targetSdkVersion 25
        versionCode VERSION_CODE.toInteger()
        versionName VERSION_NAME
    }

    signingConfigs {
        release {
            storeFile file("keys/ReleaseKeys.jks")
            keyAlias 'dtfReleaseKeys'
            storePassword "X"
            keyPassword "X"
        }
    }

    buildTypes {
        release {
            debuggable true
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
            signingConfig signingConfigs.release

            applicationVariants.all { variant ->
                variant.outputs.all { output ->

                    outputFileName = "${PACKAGE_NAME}-${variant.versionName}-${variant.versionCode}.apk"
                }
            }

        }
    }

    lintOptions {
          abortOnError true
    }

    task findBugs(type: FindBugs, dependsOn: "assembleDebug") {

        description 'Run findbugs'
        group 'verification'

        classes = fileTree('build/intermediates/classes/debug/')
        source = fileTree('com/dtf/client')
        classpath = files()

        effort = 'max'
		reportLevel = 'high'

        reports {
			html.enabled = true
			xml.enabled = false
        }
    }
}

task askForPasswords << {

    def console = System.console()
    def storePw
    def keyPw

    if (console) {
        storePw = new String(console.readPassword("\nKeystore password: "))
        keyPw = new String(console.readPassword("Key password: "))
    } else {
        // Gradle is running as a daemon - prompt user to enter passwords via popup UI (#770)
        new SwingBuilder().edt {
            dialog(modal: true, title: 'Enter credentials', alwaysOnTop: true, resizable: true,
                    locationRelativeTo: null, pack: true, show: true
            ) {
                vbox {
                    label(text: "Keystore passphrase:")
                    textField id: "storeText", input = passwordField()
                    label(text: "Key passphrase:")
                    textField id: "keyText", input = passwordField()
                    button(defaultButton: true, text: 'OK', actionPerformed: {
                        storePw = storeText.text;
                        keyPw = keyText.text;
                        dispose();
                    })
                }
            }
        }
    }

	android.signingConfigs.release.storePassword = storePw
	android.signingConfigs.release.keyPassword = keyPw
}

// Tricks for password prompt
tasks.whenTaskAdded { theTask ->
    if (theTask.name.matches("packageRelease")) {
        theTask.dependsOn "askForPasswords"
    }
}

dependencies {
    compile "com.android.support:support-core-utils:25.4.0"
}

afterEvaluate {
    assembleRelease.dependsOn findBugs
}
