apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

repositories {
    if (gradle.ext.useLocalLibrary) {
        flatDir {
            dirs gradle.ext.localLibraryDir
        }
    }

    if (gradle.ext.useRemoteLibrary) {
        flatDir {
            dirs gradle.ext.remoteLibraryDir
        }
    }

    google()
}

android {
    compileSdkVersion 31
    flavorDimensions "default"
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId 'com.ezored.sample'
        minSdkVersion 16
        targetSdkVersion 31
        versionCode 17
        versionName '1.7.0'
        multiDexEnabled true
        vectorDrawables.useSupportLibrary = true
        testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
    }

    compileOptions {
        targetCompatibility JavaVersion.VERSION_1_8
        sourceCompatibility JavaVersion.VERSION_1_8
    }

    signingConfigs {
        config {
            keyAlias '[YOUR-KEY-ALIAS]'
            keyPassword '[YOUR-KEY-PASSWORD]'
            storeFile file('keystore.release.jks')
            storePassword '[YOUR-STORE-PASSWORD]'
        }
    }

    android {
        lintOptions {
            checkReleaseBuilds false
            abortOnError false
        }
    }

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

}

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

    if (gradle.ext.useLocalLibrary || gradle.ext.useRemoteLibrary) {
        if (gradle.ext.isDebuggable) {
            implementation(name: "library-debug", ext: "aar")
        } else {
            implementation(name: "library-release", ext: "aar")
        }
    } else {
        implementation project(':ezored-library')
    }

    implementation 'androidx.appcompat:appcompat:1.4.0'
    implementation 'androidx.multidex:multidex:2.0.1'
    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.vectordrawable:vectordrawable:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.2'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
    implementation 'androidx.recyclerview:recyclerview:1.2.1'
    implementation 'androidx.lifecycle:lifecycle-common-java8:2.4.0'

    annotationProcessor 'androidx.annotation:annotation:1.3.0'

    implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.6.0'

    implementation 'org.greenrobot:eventbus:3.2.0'
    implementation 'com.google.android.material:material:1.4.0'

    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.2'
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.2'

    implementation 'com.google.android.instantapps:instantapps:1.1.0'

    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test:runner:1.4.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

// custom SDK download task
if (gradle.ext.useRemoteLibrary) {
    apply plugin: "de.undercouch.download"

    task downloadEzoredSdkFile(type: Download) {
        src "https://ezored.s3.amazonaws.com/dist/android/${gradle.ext.ezoredSdkVersion}/dist.tar.gz"
        dest new File(gradle.ext.ezoredSdkCacheDir, "ezored-sdk/sdk-${gradle.ext.ezoredSdkVersion}.tar.gz")
        tempAndMove true
        onlyIfModified true
        overwrite false
        downloadTaskDir gradle.ext.ezoredSdkCacheDir
    }

    task getEzoredSDK(dependsOn: downloadEzoredSdkFile, type: Copy) {
        from tarTree(downloadEzoredSdkFile.dest)
        into new File(buildDir, "ezored-sdk")
    }

    preBuild.dependsOn getEzoredSDK
}

// copy webapp assets
task copyWebAppFiles(type: Copy)

copyWebAppFiles {
    description = 'Copy webapp assets'
    from '../../../../projects/web/Sample/dist'
    into 'src/main/assets/webapp'
    include('**/*')
}

preBuild.dependsOn copyWebAppFiles
