import com.android.build.api.dsl.ManagedVirtualDevice

plugins {
    id("com.android.application")
    id("com.google.dagger.hilt.android")
    id("kotlin-android")
    id("jacoco")
    id("kotlin-parcelize")
    id("com.google.devtools.ksp")
}

android {

    namespace 'org.pixeldroid.app'
    compileSdk 34

    compileOptions {
        coreLibraryDesugaringEnabled true
    }

    androidResources {
        generateLocaleConfig true
    }

    kotlin {
        jvmToolchain(17)
    }
    kotlinOptions {
        freeCompilerArgs += ["-opt-in=kotlin.RequiresOptIn"]
    }
    defaultConfig {
        minSdkVersion 23
        targetSdkVersion 34
        versionCode 32
        versionName "1.0.beta" + versionCode

        //TODO add resConfigs("en", "fr", "ja",...) ?

        testInstrumentationRunner "org.pixeldroid.app.testUtility.TestRunner"
        testInstrumentationRunnerArguments clearPackageData: 'true'
    }
    sourceSets {
        main.java.srcDirs += 'src/main/java'
        test.java.srcDirs += 'src/test/java'
        staging.res.srcDirs += 'src/debug/res'
        androidTest.java.srcDirs += 'src/androidTest/java'
    }
    testBuildType "staging"

    buildTypes {
        debug {
            applicationIdSuffix '.debug'
            versionNameSuffix "-debug"
        }
        staging {
            initWith debug
            testCoverageEnabled true

            // These values are first looked for in the env variables, and, if not found there,
            // in the local.properties file (which is not checked into version control of course!).

            // If you are not running the integration tests, you can just set them to dummy values
            // in local.properties or comment the buildConfigField lines

            def localProperties = new Properties()
            if (rootProject.file("local.properties").exists()) {
                localProperties.load(new FileInputStream(rootProject.file("local.properties")))
            }

            buildConfigField "String", "USER_ID", System.getenv("USER_ID") ?: localProperties['USER_ID'] ?: '""'
            buildConfigField "String", "INSTANCE_URI", System.getenv("INSTANCE_URI") ?: localProperties['INSTANCE_URI'] ?: '""'
            buildConfigField "String", "ACCESS_TOKEN", System.getenv("ACCESS_TOKEN") ?: localProperties['ACCESS_TOKEN'] ?: '""'
            buildConfigField "String", "REFRESH_TOKEN", System.getenv("REFRESH_TOKEN") ?: localProperties['REFRESH_TOKEN'] ?: '""'
            buildConfigField "String", "CLIENT_ID", System.getenv("CLIENT_ID") ?: localProperties['CLIENT_ID'] ?: '""'
            buildConfigField "String", "CLIENT_SECRET", System.getenv("CLIENT_SECRET") ?: localProperties['CLIENT_SECRET'] ?: '""'
        }
        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles 'proguard-rules.pro'
        }
    }

    splits {
        // Configures multiple APKs based on ABI.
        abi {

            // Enables building multiple APKs per ABI.
            enable true

            // By default all ABIs are included, so use reset() and include to specify that we only
            // want APKs for "x86", "x86_64", "arm64-v8a" and "armeabi-v7a".

            // Resets the list of ABIs for Gradle to create APKs for to none.
            reset()

            // Specifies a list of ABIs for Gradle to create APKs for.
            include "x86", "x86_64", "arm64-v8a", "armeabi-v7a"

            // Specifies that we don't want to also generate a universal APK that includes all ABIs.
            universalApk false
        }

    }

    /**
     * Make a string with the application_id (available in xml etc)
     */
    android.applicationVariants.configureEach { variant ->
        variant.resValue 'string', 'application_id', variant.applicationId
        variant.resValue "string", "versionName", variant.versionName
    }

    testOptions {
        animationsDisabled true
        managedDevices {
            devices {
                pixel2api30 (ManagedVirtualDevice) {
                    // Use device profiles you typically see in Android Studio.
                    device = "Pixel 2"
                    // Use only API levels 27 and higher.
                    apiLevel = 30
                    // To include Google services, use "google".
                    systemImageSource = "aosp-atd"
                    // Whether the image must be a 64 bit image. Defaults to false,
                    // in which case the managed device will use a 32 bit image.
                    // Not applicable to arm64 machines.
                    require64Bit = false
                }
            }
        }

    }
    buildFeatures {
        viewBinding true
        buildConfig = true
    }

    lint {
        //We can't expect translators to always keep up immediately:
        // don't fail if a a string is untranslated
        disable 'MissingTranslation'
        // This lint indicates a wrong translation:
        // Remove this exception once https://github.com/WeblateOrg/weblate/issues/7520 is solved
        disable 'MissingQuantity'
    }
}

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

    coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.0.4'

    /**
     * AndroidX dependencies:
     */
    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'androidx.core:core-splashscreen:1.0.1'
    implementation 'androidx.core:core-ktx:1.12.0'
    implementation 'androidx.preference:preference-ktx:1.2.1'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation 'androidx.navigation:navigation-fragment-ktx:2.7.7'
    implementation 'androidx.navigation:navigation-ui-ktx:2.7.7'
    implementation "androidx.browser:browser:1.7.0"
    implementation 'androidx.recyclerview:recyclerview:1.3.2'
    implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0"
    implementation 'androidx.navigation:navigation-fragment-ktx:2.7.7'
    implementation 'androidx.navigation:navigation-ui-ktx:2.7.7'
    implementation 'androidx.paging:paging-runtime-ktx:3.2.1'
    implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.7.0'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.7.0'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-savedstate:2.7.0'
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.7.0'
    implementation "androidx.lifecycle:lifecycle-common-java8:2.7.0"
    implementation "androidx.annotation:annotation:1.7.1"
    implementation 'androidx.gridlayout:gridlayout:1.0.0'
    implementation "androidx.activity:activity-ktx:1.8.2"
    implementation 'androidx.fragment:fragment-ktx:1.6.2'
    implementation 'androidx.work:work-runtime-ktx:2.9.0'
    implementation 'androidx.media2:media2-widget:1.3.0'
    implementation 'androidx.media2:media2-player:1.3.0'


    // Use the most recent version of CameraX
    def cameraX_version = '1.3.1'
    implementation "androidx.camera:camera-core:$cameraX_version"
    implementation "androidx.camera:camera-camera2:$cameraX_version"
    // CameraX Lifecycle library
    implementation "androidx.camera:camera-lifecycle:$cameraX_version"

    // CameraX View class
    implementation "androidx.camera:camera-view:$cameraX_version"

    def room_version = "2.6.1"
    implementation "androidx.room:room-runtime:$room_version"
    ksp "androidx.room:room-compiler:$room_version"
    implementation "androidx.room:room-ktx:$room_version"
    implementation "androidx.room:room-paging:$room_version"

    /**
     * End of AndroidX section
     * ----------------------------------------------------------
     */

    implementation 'com.davemorrissey.labs:subsampling-scale-image-view-androidx:3.10.0'

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

    //Dagger (dependency injection)
    implementation 'com.google.dagger:dagger:2.50'
    ksp 'com.google.dagger:dagger-compiler:2.50'

    implementation("com.google.dagger:hilt-android:2.50")
    ksp "com.google.dagger:hilt-compiler:2.50"

    implementation 'com.squareup.okhttp3:okhttp:4.12.0'
    implementation 'com.squareup.retrofit2:retrofit:2.9.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
    implementation 'com.squareup.retrofit2:adapter-rxjava3:2.9.0'
    implementation 'io.reactivex.rxjava3:rxjava:3.1.8'
    implementation 'io.reactivex.rxjava3:rxandroid:3.0.2'
    implementation 'com.github.connyduck:sparkbutton:4.1.0'

    implementation 'org.pixeldroid.pixeldroid:android-media-editor:1.7'
    implementation project(path: ':scrambler')
    implementation project(path: ':pixel_common')

    implementation('com.github.bumptech.glide:glide:4.16.0') {
        exclude group: "com.android.support"
    }

    implementation 'com.github.bumptech.glide:okhttp3-integration:4.16.0'
    implementation('com.github.bumptech.glide:recyclerview-integration:4.16.0') {
        // Excludes the support library because it's already included by Glide.
        transitive = false
    }
    implementation 'com.github.bumptech.glide:annotations:4.16.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.16.0'
    ksp 'com.github.bumptech.glide:ksp:4.16.0'

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

    implementation 'com.mikepenz:materialdrawer:9.0.2'
    // Add for NavController support
    implementation 'com.mikepenz:materialdrawer-nav:9.0.2'

    //iconics
    implementation 'com.mikepenz:iconics-core:5.4.0'
    implementation 'com.mikepenz:materialdrawer-iconics:9.0.2'
    implementation 'com.mikepenz:iconics-views:5.4.0'
    implementation 'com.mikepenz:google-material-typeface:4.0.0.2-kotlin@aar'

    implementation 'com.github.ligi:tracedroid:4.1'

    implementation 'me.relex:circleindicator:2.1.6'

    /**
     * Not in release, so not mentioned in licenses list
     */

    //stagingImplementation 'com.squareup.leakcanary:leakcanary-android:2.7'
    androidTestImplementation 'com.android.support.test.espresso:espresso-idling-resource:3.0.2'

    androidTestImplementation 'com.linkedin.testbutler:test-butler-library:2.2.1'
    androidTestUtil 'com.linkedin.testbutler:test-butler-app:2.2.1'

    androidTestImplementation 'androidx.work:work-testing:2.9.0'
    testImplementation 'com.github.tomakehurst:wiremock-jre8:2.34.0'
    testImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0"
    testImplementation 'junit:junit:4.13.2'
    testImplementation "androidx.room:room-testing:$room_version"


    androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0'
    androidTestImplementation 'androidx.test:runner:1.5.2'
    androidTestImplementation 'androidx.test:rules:1.5.0'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test:runner:1.5.2'
    androidTestImplementation 'androidx.test:rules:1.5.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
    androidTestImplementation 'androidx.test.espresso:espresso-intents:3.5.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-contrib:3.0.2'
    androidTestImplementation 'com.squareup.okhttp3:mockwebserver:4.12.0'

}

tasks.withType(Test).configureEach {
    jacoco.includeNoLocationClasses = true
    jacoco.excludes = ['jdk.internal.*']
}


task jacocoTestReport(type: JacocoReport, dependsOn: ['connectedStagingAndroidTest', 'testStagingUnitTest', 'createStagingCoverageReport']) {

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

    def fileFilter = ['**/R.class', '**/R$*.class', '**/BuildConfig.*', '**/Manifest*.*', '**/*Test*.*', 'android/**/*.*']
    def kotlinTree = fileTree(dir: "$project.buildDir/tmp/kotlin-classes/staging", excludes: fileFilter)
    def mainSrc = "$project.projectDir/src/main/java"
    getSourceDirectories().from(files([mainSrc]))
    getClassDirectories().from(files([kotlinTree]))
    getExecutionData().from(fileTree(dir: project.buildDir, includes: [

            'outputs/code_coverage/stagingAndroidTest/connected/*coverage.ec',

            'jacoco/testStagingUnitTest.exec'

    ]))
}
