import java.util.regex.Matcher
import java.util.regex.Pattern

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'androidx.navigation.safeargs.kotlin'
}
def flavor
android {
    compileSdk 33

    defaultConfig {
        minSdk 21
        targetSdk 33
        versionCode 491
        versionName "3.22.1"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    flavorDimensions "default"
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
        debug {
            applicationIdSuffix '.debug'
            pseudoLocalesEnabled true
        }
    }
    compileOptions {
        coreLibraryDesugaringEnabled true
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    productFlavors {
        fdroid {
            applicationId "fr.gouv.etalab.mastodon"
            buildConfigField "boolean", "DONATIONS", "true"
            buildConfigField "boolean", "push", "false"
            flavor = "fdroid"
        }
        playstore {
            applicationId "app.fedilab.android"
            buildConfigField "boolean", "DONATIONS", "false"
            buildConfigField "boolean", "push", "true"
            flavor = "playstore"
        }
    }
    lintOptions {
        checkReleaseBuilds false
        abortOnError false
    }
    buildFeatures {
        viewBinding true
    }
    sourceSets {
        playstore {
            manifest.srcFile "src/playstore/AndroidManifest.xml"
            java.srcDirs = ['src/main/java', 'src/playstore/java']
            res.srcDirs = ['src/main/res', 'src/playstore/res']
        }
        fdroid {
            java.srcDirs = ['src/main/java', 'src/fdroid/java']
            res.srcDirs = ['src/main/res', 'src/fdroid/res']
        }
        main {
            res.srcDirs = [
                    'src/main/res/layouts/mastodon',
                    'src/main/res/layouts/peertube',
                    'src/main/res/layouts',

                    'src/main/res/drawables/mastodon',
                    'src/main/res/drawables/peertube',
                    'src/main/res/drawables',

                    'src/main/res/menus/mastodon',
                    'src/main/res/menus/peertube',
                    'src/main/res/menus',

                    'src/main/res/values',

                    'src/main/res'
            ]
        }
    }
    configurations {
        all {
            exclude group: 'androidx.lifecycle', module: 'lifecycle-viewmodel-ktx'
        }
    }
}
allprojects {
    repositories {
        mavenLocal()
        maven { url "https://jitpack.io" }
    }
}
dependencies {

    implementation 'androidx.appcompat:appcompat:1.6.1'

    implementation 'com.google.android.material:material:1.9.0'


    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation "com.google.code.gson:gson:2.9.1"
    implementation 'com.squareup.retrofit2:retrofit:2.9.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
    implementation 'com.squareup.retrofit2:converter-simplexml:2.9.0'
    implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0'
    implementation 'androidx.preference:preference:1.2.0'
    implementation "org.conscrypt:conscrypt-android:2.5.2"
    implementation 'com.vanniktech:emoji-one:0.6.0'
    implementation 'com.github.GrenderG:Toasty:1.5.2'
    implementation "com.github.bumptech.glide:glide:4.14.2"
    implementation "com.github.bumptech.glide:okhttp3-integration:4.14.2"
    implementation("com.github.bumptech.glide:recyclerview-integration:4.14.2") {
        // Excludes the support library because it's already included by Glide.
        transitive = false
    }

    implementation "org.jsoup:jsoup:1.15.1"
    implementation 'com.github.mergehez:ArgPlayer:v3.1'

    implementation project(':autoimageslider')
    implementation project(path: ':mytransl')
    implementation project(path: ':ratethisapp')
    implementation project(path: ':sparkbutton')
    implementation project(path: ':colorPicker')
    implementation project(path: ':mathjaxandroid')


    implementation 'com.burhanrashid52:photoeditor:1.5.1'
    implementation("com.vanniktech:android-image-cropper:4.3.3")
    annotationProcessor "com.github.bumptech.glide:compiler:4.12.0"
    implementation 'jp.wasabeef:glide-transformations:4.3.0'
    implementation 'com.github.penfeizhou.android.animation:glide-plugin:2.23.0'
    implementation 'com.google.android.exoplayer:exoplayer:2.18.4'
    implementation "androidx.viewpager2:viewpager2:1.0.0"
    implementation 'com.github.piasy:rxandroidaudio:1.7.0'
    implementation 'com.github.piasy:AudioProcessor:1.7.0'
    implementation "androidx.work:work-runtime:2.7.1"
    implementation 'app.futured.hauler:hauler:5.0.0'
    implementation "com.github.chrisbanes:PhotoView:2.3.0"
    implementation "ch.acra:acra-mail:5.9.6"
    implementation "ch.acra:acra-limiter:5.9.3"
    implementation "ch.acra:acra-dialog:5.9.6"
    implementation "com.madgag.spongycastle:bctls-jdk15on:1.58.0.0"
    implementation 'com.github.UnifiedPush:android-connector:2.1.1'
   // implementation 'com.github.UnifiedPush:android-foss_embedded_fcm_distributor:1.0.0-beta1'
    playstoreImplementation('com.github.UnifiedPush:android-embedded_fcm_distributor:2.2.0') {
        exclude group: 'com.google.firebase', module: 'firebase-core'
        exclude group: 'com.google.firebase', module: 'firebase-analytics'
        exclude group: 'com.google.firebase', module: 'firebase-measurement-connector'
    }
    implementation 'com.burhanrashid52:photoeditor:1.5.1'
    implementation 'androidx.multidex:multidex:2.0.1'
    implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0'
    implementation 'androidx.lifecycle:lifecycle-livedata:2.5.1'
    implementation 'androidx.lifecycle:lifecycle-viewmodel:2.5.1'
    implementation 'androidx.navigation:navigation-fragment:2.5.3'
    implementation 'androidx.navigation:navigation-ui:2.5.3'
    testImplementation 'junit:junit:'
    androidTestImplementation 'androidx.test.ext:junit:1.1.4'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0'
    // debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.8.1'
    implementation 'com.r0adkll:slidableactivity:2.1.0'
    coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5'

    implementation 'androidx.vectordrawable:vectordrawable:1.1.0'
    implementation "androidx.fragment:fragment:1.5.5"
    implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
    implementation 'androidx.browser:browser:1.5.0'
    implementation 'androidx.documentfile:documentfile:1.0.1'
    implementation 'com.github.amoskorir:avatarimagegenerator:1.5.0'
    implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.1.0'
    implementation 'com.google.android.exoplayer:extension-mediasession:2.18.4'
    implementation "com.github.mabbas007:TagsEditText:1.0.5"
    implementation "net.gotev:uploadservice:4.7.0"
    implementation "net.gotev:uploadservice-okhttp:4.7.0"
    implementation 'androidx.media:media:1.6.0'
    implementation 'com.github.mancj:MaterialSearchBar:0.8.5'

    implementation 'com.github.vkay94:DoubleTapPlayerView:1.0.0'


    //************ CAST **************///

    //---> Google libs (google_full)
    playstoreImplementation "com.google.android.gms:play-services-cast-tv:19.0.1"
    playstoreImplementation "com.google.android.gms:play-services-cast:21.0.1"
    playstoreImplementation "androidx.mediarouter:mediarouter:1.3.0"
    playstoreImplementation 'com.google.android.gms:play-services-cast-framework:21.0.1'


    //----> Other flavors
    fdroidImplementation 'su.litvak.chromecast:api-v2:0.11.3'
    fdroidImplementation 'com.fasterxml.jackson.core:jackson-core:2.12.0'
    fdroidImplementation 'org.slf4j:slf4j-simple:1.7.30'

}
def getCurrentFlavor() {
    Gradle gradle = getGradle()
    String  tskReqStr = gradle.getStartParameter().getTaskRequests().toString()
    println("tskReqStr:" +tskReqStr)
    Pattern pattern

    if( tskReqStr.contains( "assemble" ) ) // to run ./gradlew assembleRelease to build APK
        pattern = Pattern.compile("assemble(\\w+)(Release|Debug)")
    else if( tskReqStr.contains( "bundle" ) ) // to run ./gradlew bundleRelease to build .aab
        pattern = Pattern.compile("assemble(\\w+)(Release|Debug)")
    else
        pattern = Pattern.compile("generate(\\w+)(Release|Debug)")

    Matcher matcher = pattern.matcher( tskReqStr )
    println(tskReqStr)
    if( matcher.find() )
        return matcher.group(1).toLowerCase()
    else
    {
        println "NO MATCH FOUND"
        return ""
    }
}
println("Flavor: ${getCurrentFlavor()}")
if ( getCurrentFlavor() == "playstore" ){
    apply plugin: 'com.google.gms.google-services'
}