apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'

apply plugin: 'com.google.devtools.ksp'

apply plugin: 'kotlin-kapt'

apply plugin: 'kotlin-allopen'

apply plugin: 'jacoco'

apply plugin: 'com.github.kt3k.coveralls'

jacoco {
    version = '0.8.1'
}

allOpen {
    annotation 'com.allVideoBrowserXmaster.OpenForTesting'
}
def splitApks = System.getenv("SPLITS_INCLUDE")?.toBoolean() ?: true
def abiFilterList = (properties["ABI_FILTERS"] as String).split(';')
def abiCodes = ["armeabi-v7a": 1, "arm64-v8a": 2, "x86": 3, "x86_64": 4]

android {
    dependenciesInfo {
        // Disables dependency metadata when building APKs.
        includeInApk = false
        // Disables dependency metadata when building Android App Bundles.
        includeInBundle = false
    }

    packagingOptions {
        resources {
            excludes += ['mozilla/public-suffix-list.txt',
                         'META-INF/DEPENDENCIES',
                         'META-INF/LICENSE',
                         'META-INF/LICENSE.txt',
                         'META-INF/license.txt',
                         'META-INF/NOTICE',
                         'META-INF/NOTICE.txt',
                         'META-INF/notice.txt',
                         'META-INF/ASL2.0',
                         'META-INF/*.kotlin_module']
        }
        jniLibs {
            useLegacyPackaging = true
        }
    }
    compileSdkVersion build_versions.target_sdk
    signingConfigs {
        release {
            storeFile file(System.getenv("KEYSTORE_PATH") ?: "keystore.jks")
            storePassword System.getenv("KEYSTORE_PASSWORD")
            keyAlias System.getenv("KEY_ALIAS")
            keyPassword System.getenv("KEY_PASSWORD")
        }
    }
    defaultConfig {
        applicationId "com.myAllVideoBrowser"
        minSdkVersion build_versions.min_sdk
        targetSdkVersion build_versions.target_sdk
        versionCode 45
        versionName "0.6.1"
        testInstrumentationRunner "util.TestRunner"

        if (splitApks) {
            splits {
                abi {
                    enable true
                    reset()
                    include 'x86', 'x86_64', 'armeabi-v7a', 'arm64-v8a'
                    universalApk true
                }
            }
        } else {
            ndk { abiFilters.addAll(abiFilterList) }
        }
    }
    buildTypes {
        debug {
            minifyEnabled false
            shrinkResources false
            testCoverageEnabled true
        }
        release {
            signingConfig signingConfigs.release
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    dataBinding {
        enabled = true
    }
    buildFeatures {
        viewBinding true
        buildConfig true
    }
    testOptions {
        unitTests {
            includeAndroidResources = true
            returnDefaultValues = true
        }
    }
    // Assign a unique version code for each architecture
    applicationVariants.configureEach { variant ->
        variant.outputs.each { output ->
            def name
            if (splitApks) {
                name = output.getFilter(com.android.build.OutputFile.ABI)
            } else {
                name = abiFilterList[0]
            }

            def baseAbiCode = abiCodes[name]

            if (baseAbiCode != null) {
                output.versionCodeOverride = output.versionCode + baseAbiCode
            }
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = "1.8"
    }
    namespace 'com.myAllVideoBrowser'
    lint {
        abortOnError false
    }
    sourceSets {
        main {
            jniLibs.srcDirs = ['libs']
        }
    }
}

dependencies {
    // Android support
    implementation 'androidx.appcompat:appcompat:1.7.0'
    implementation 'com.google.android.material:material:1.12.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.2.1'
    implementation 'androidx.recyclerview:recyclerview:1.4.0'
    // Kotlin + coroutines
    implementation('androidx.work:work-runtime-ktx:2.10.0')

    // optional - RxJava2 support
    implementation('androidx.work:work-rxjava3:2.10.0')

    // optional - GCMNetworkManager support
    implementation 'androidx.fragment:fragment-ktx:1.8.6'
    // optional - Test helpers
    androidTestImplementation('androidx.work:work-testing:2.10.0')

    // optional - Multiprocess support
    implementation 'androidx.work:work-multiprocess:2.10.0'

    implementation 'androidx.legacy:legacy-support-v4:1.0.0'

    implementation('androidx.concurrent:concurrent-futures-ktx:1.2.0')

    // Testing
    testImplementation deps.testing.junit
    testImplementation deps.testing.mockito_core
    testImplementation deps.testing.mockito_kotlin
    androidTestImplementation deps.testing.mockito_android
    androidTestImplementation deps.testing.mockito_kotlin
    androidTestImplementation 'androidx.test.ext:junit:1.2.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.6.1'
    androidTestImplementation 'androidx.test.espresso:espresso-intents:3.6.1'

    // Kotlin
    implementation deps.kotlin.stdlib

    // Life cycle
    implementation deps.lifecycle.extensions
    implementation 'androidx.lifecycle:lifecycle-common-java8:2.8.7'
    implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.8.7'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.8.7'
    testImplementation 'androidx.arch.core:core-testing:2.2.0'
    androidTestImplementation 'androidx.arch.core:core-testing:2.2.0'

    // Room

    implementation('androidx.room:room-runtime:2.6.1')
    annotationProcessor('androidx.room:room-compiler:2.6.1')

    ksp "androidx.room:room-compiler:2.6.1"

    // optional - Kotlin Extensions and Coroutines support for Room
    implementation('androidx.room:room-ktx:2.6.1')

    // optional - RxJava3 support for Room
    implementation('androidx.room:room-rxjava3:2.6.1')

    // optional - Guava support for Room, including Optional and ListenableFuture
    implementation('androidx.room:room-guava:2.6.1')

    // optional - Test helpers
    testImplementation('androidx.room:room-testing:2.6.1')

    // Dagger2
    implementation deps.dagger.runtime
    implementation deps.dagger.android
    implementation deps.dagger.android_support
    ksp deps.dagger.compiler
    ksp deps.dagger.android_processor

    // OkHttp and Retrofit
    implementation deps.okhttp.runtime
    implementation deps.okhttp.logging
    implementation deps.retrofit.runtime
    implementation 'com.squareup.retrofit2:adapter-rxjava3:2.11.0'
    implementation deps.retrofit.gson

    implementation 'io.reactivex.rxjava3:rxandroid:3.0.2'
    // Because RxAndroid releases are few and far between, it is recommended you also
    // explicitly depend on RxJava's latest version for bug fixes and new features.
    // (see https://github.com/ReactiveX/RxJava/releases for latest 3.x.x version)
    implementation 'io.reactivex.rxjava3:rxjava:3.1.10'

    implementation('io.github.junkfood02.youtubedl-android:library:0.17.4')
    implementation('io.github.junkfood02.youtubedl-android:ffmpeg:0.17.4')

    implementation 'com.github.bumptech.glide:glide:4.16.0'

    implementation 'com.github.bumptech.glide:glide:4.16.0'

    implementation('androidx.webkit:webkit:1.12.1')

    implementation 'androidx.core:core-ktx:1.15.0'

    implementation 'org.jetbrains.kotlinx:kotlinx-serialization-json:1.8.1'

    implementation 'org.jsoup:jsoup:1.19.1'

    implementation 'com.github.marlonlom:timeago:4.1.0'

    var media3_version = '1.6.0'

    // For media playback using ExoPlayer
    implementation "androidx.media3:media3-exoplayer:$media3_version"

    // For DASH playback support with ExoPlayer
    implementation "androidx.media3:media3-exoplayer-dash:$media3_version"
    // For HLS playback support with ExoPlayer
    implementation "androidx.media3:media3-exoplayer-hls:$media3_version"
    // For RTSP playback support with ExoPlayer
    implementation "androidx.media3:media3-exoplayer-rtsp:$media3_version"

    // For building media playback UIs
    implementation "androidx.media3:media3-ui:$media3_version"

    // For exposing and controlling media sessions
    implementation "androidx.media3:media3-session:$media3_version"

    // For extracting data from media containers
    implementation "androidx.media3:media3-extractor:$media3_version"

    // Common functionality for media database components
    implementation "androidx.media3:media3-database:$media3_version"
    // Common functionality for media decoders
    implementation "androidx.media3:media3-decoder:$media3_version"
    // Common functionality for loading data
    implementation "androidx.media3:media3-datasource:$media3_version"
    // Common functionality used across multiple media libraries
    implementation "androidx.media3:media3-common:$media3_version"

    // https://firebase.google.com/docs/android/setup#available-libraries

    implementation 'androidx.core:core-splashscreen:1.0.1'
}

ksp {
    arg("room.schemaLocation", "$projectDir/schemas".toString())
    arg("room.incremental", "true")
    arg("room.expandProjection", "true")
}

tasks.coveralls {
    dependsOn 'check'
    onlyIf { System.getenv("COVERALLS_REPO_TOKEN") }
}

tasks.register('jacocoTestReport', JacocoReport) {
    dependsOn 'testDebugUnitTest'
    group = 'Reporting'
    description = 'Generate Jacoco coverage reports after running tests.'

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

    def fileFilter = ['**/R.class', '**/R$*.class', '**/BuildConfig.*',
                      '**/Manifest*.*', '**/*Test*.*', 'android/**/*.*',

                      '**/Lambda$*.class', '**/Lambda.class', '**/*Lambda.class',
                      '**/*Lambda*.class', '**/*Builder.*',

                      '**/*_Injector.class', '**/*_Injector*.*', '**/*_*Factory*.*',
                      '**/*Component*.*', '**/*Module*.*',

                      '**/component/widget/*.*', '**/component/binding/*.*', '**/util/ext/*.*',
                      '**/util/scheduler/*.*', '**/util/fragment/*.*', '**/DLApplication.*',
                      '**/ViewModelFactory.*', '**/RoomConverter.*', '**/AppDatabase.*']

    def javaClasses = fileTree(dir: "$getLayout().getBuildDirectory()/intermediates/javac/debug", excludes: fileFilter)
    def kotlinClasses = fileTree(dir: "$getLayout().getBuildDirectory()/tmp/kotlin-classes/debug", excludes: fileFilter)

    sourceDirectories.from files(android.sourceSets.main.java.srcDirs)
    classDirectories.from files([javaClasses, kotlinClasses])
    executionData.from fileTree(dir: getLayout().getBuildDirectory(), includes: [
            'jacoco/testDebugUnitTest.exec', 'outputs/code-coverage/connected/*coverage.ec'
    ])
}
