apply plugin: 'com.android.application'

import groovy.swing.SwingBuilder

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

android {
    signingConfigs {
        google {
            storePassword ''
            keyPassword ''
        }
        free {
            storePassword ''
            keyPassword ''
        }
    }

    compileSdkVersion 28
    defaultConfig {
        applicationId "org.tvbrowser.tvbrowser"
        minSdkVersion 14
        targetSdkVersion 28
        versionCode 4274
        versionName "0.7.0.10beta3"
        vectorDrawables.useSupportLibrary = true
    }

    flavorDimensions "default"
    productFlavors {
        free {
            dimension "default"
            applicationIdSuffix ""
            signingConfig signingConfigs.free
        }
        google {
            dimension "default"
            versionNameSuffix "vP"
            applicationIdSuffix ".play"
            signingConfig signingConfigs.google
        }
    }

    buildTypes {
        debug {
            debuggable true
            minifyEnabled false
            // proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt'
            versionNameSuffix "-debug"
            applicationIdSuffix ".debug"
        }
        release {
          /*  def password = ""
            def passwordKeyStore = ""

            if (System.console() == null) {
                new SwingBuilder().edt {
                    dialog(modal: true,
                            title: "Enter password",
                            alwaysOnTop: true,
                            resizable: false,
                            locationRelativeTo: null,
                            pack: true,
                            show: true
                    ) {
                        vbox {
                            label(text: "Enter store password: ")
                            input1 = passwordField()
                            label(text: "Enter key password: ")
                            input = passwordField()
                            button(defaultButton: true,
                                    text: 'OK',
                                    actionPerformed: {
                                        passwordKeyStore = new String(input1.password);
                                        password = new String(input.password);
                                        dispose();
                                    })
                        }
                    }
                }
            } else {
                password = System.console().readPassword("\nEnter keystore password: ")
                password = new String(password)
                passwordKeyStore = System.console().readPassword("\nEnter key password: ")
                passwordKeyStore = new String(passwordKeyStore)
            }

            if (password.size() <= 0 || passwordKeyStore.size() <= 0) {
                throw new InvalidUserDataException("Empty password")
            }

            // set signing config key passwords
            android.signingConfigs.google.storePassword = passwordKeyStore
            android.signingConfigs.google.keyPassword = password

            android.signingConfigs.free.storePassword = passwordKeyStore
            android.signingConfigs.free.keyPassword = password

            productFlavors.google.signingConfig = signingConfigs.google
            productFlavors.free.signingConfig = signingConfigs.free*/

            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt'
        }
    }

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }

        free {
            manifest.srcFile 'free/AndroidManifest.xml'
            java.srcDirs = ['free/src']
            resources.srcDirs = ['free/src']
            aidl.srcDirs = ['free/src']
            renderscript.srcDirs = ['free/src']
            res.srcDirs = ['free/res']
            assets.srcDirs = ['free/assets']
        }

        google {
            manifest.srcFile 'google/AndroidManifest.xml'
            java.srcDirs = ['google/src']
            resources.srcDirs = ['google/src']
            aidl.srcDirs = ['google/src']
            renderscript.srcDirs = ['google/src']
            res.srcDirs = ['google/res']
            assets.srcDirs = ['google/assets']
        }

        androidTest.setRoot('androidTest')
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    lintOptions {
        checkReleaseBuilds true
        // Or, if you prefer, you can continue to check for errors in release builds,
        // but continue the build even when errors are found:
        abortOnError false
    }
}

if (project.hasProperty("AndroidProject.signing")
        && new File(project.property("AndroidProject.signing").toString()).exists()) {
    Properties props = new Properties()
    def propFile = new File(project.property("AndroidProject.signing").toString())
    if (propFile.canRead()) {
        props.load(new FileInputStream(propFile))
        if (props != null && props.containsKey('STORE_FILE_GOOGLE') && props.containsKey('KEY_ALIAS_GOOGLE') &&
                props.containsKey('STORE_FILE_FREE') && props.containsKey('KEY_ALIAS_FREE')) {
            android.signingConfigs.google.storeFile = file(props['STORE_FILE_GOOGLE'])
            android.signingConfigs.google.keyAlias = props['KEY_ALIAS_GOOGLE']
            android.signingConfigs.free.storeFile = file(props['STORE_FILE_FREE'])
            android.signingConfigs.free.keyAlias = props['KEY_ALIAS_FREE']
        } else {
            println 'androidproject.properties found but some entries are missing'
            android.buildTypes.release.signingConfig = null
        }
    } else {
        println 'androidproject.properties file not found'
        android.buildTypes.release.signingConfig = null
    }
}

dependencies {
    implementation 'android.arch.work:work-runtime:1.0.1'
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta2'
    implementation 'androidx.legacy:legacy-preference-v14:1.0.0'
    implementation 'androidx.vectordrawable:vectordrawable:1.0.1'
    implementation 'com.google.android.material:material:1.1.0-alpha09'
    implementation 'me.drakeet.support:toastcompat:1.1.0'
    implementation 'com.google.android.gms:play-services-auth:20.7.0'
}