apply plugin: 'com.android.application'
apply plugin: 'com.google.firebase.crashlytics'

android {
    compileSdk 35
    useLibrary 'org.apache.http.legacy'

    defaultConfig {
        applicationId "org.glpi.inventory.agent"
        minSdkVersion 21
        targetSdkVersion 35

        android.buildFeatures.buildConfig = true
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
    }

    testOptions {
        unitTests.returnDefaultValues = true
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
            manifestPlaceholders = [crashlyticsEnabled: true]
        }
        debug {
            testCoverageEnabled true
            manifestPlaceholders = [crashlyticsEnabled: false]
        }
    }

    namespace 'org.glpi.inventory.agent'
    lint {
        abortOnError false
    }
}

dependencies {
    implementation fileTree(include: ['*.jar', '*.aar'], dir: 'libs')
    implementation 'info.hoang8f:fbutton:1.0.5'

    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'

    androidTestImplementation 'androidx.test.ext:junit:1.2.1'
    androidTestImplementation 'androidx.test:rules:1.6.1'
    androidTestImplementation 'androidx.test:runner:1.6.2'

    /* app restriction */
    implementation 'androidx.enterprise:enterprise-feedback:1.1.0'

    androidTestImplementation('androidx.test.espresso:espresso-core:3.1.0', {
        exclude group: 'com.androidx', module: 'support-annotations'
    })
    androidTestImplementation 'tools.fastlane:screengrab:2.1.1'

    androidTestImplementation 'tools.fastlane:screengrab:2.1.1'

    testImplementation 'org.mockito:mockito-android:5.5.0' // Use the latest version
    androidTestImplementation 'org.mockito:mockito-android:5.5.0'

    implementation 'androidx.test.espresso:espresso-idling-resource:3.6.1'
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'com.google.android.material:material:1.12.0'
    implementation 'com.orhanobut:logger:2.2.0'
    implementation 'androidx.multidex:multidex:2.0.1'
    implementation 'androidx.preference:preference:1.2.1'
    implementation 'org.dom4j:dom4j:2.1.3'


    // Add the Firebase Crashlytics SDK.
    implementation 'com.google.firebase:firebase-crashlytics:19.1.0'

    // Recommended: Add the Google Analytics SDK.
    implementation 'com.google.firebase:firebase-analytics:22.1.0'


    implementation 'com.google.zxing:core:3.3.0'

    testImplementation 'junit:junit:4.13.2'
}

import groovy.json.JsonSlurper

import java.util.regex.Pattern

task javadoc(type: Javadoc) {
    doFirst {
        configurations.implementation
                .filter { it.name.endsWith('.aar') }
                .each { aar ->
                    copy {
                        from zipTree(aar)
                        include "**/classes.jar"
                        into "$buildDir/tmp/aarsToJars/${aar.name.replace('.aar', '')}/"
                    }
                }
    }

    configurations.implementation.setCanBeResolved(true)
    source = android.sourceSets.main.java.srcDirs
    classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
    classpath += configurations.implementation
    classpath += fileTree(dir: "$buildDir/tmp/aarsToJars/")
    exclude '**BuildConfig**'
    exclude '**/R**'
    exclude '**/R.*'
    exclude '**/R\$**'
    exclude '**/inventory**'

    if (!project.hasProperty("customDestination")) {
        def customDestination = "development"
    }

    //println "Destination for javadoc is "+customDestination
    //destinationDir = file("../"+customDestination)
    failOnError true
}

task updateVersionCode {
    doLast {
        def manifestFile = file("src/main/AndroidManifest.xml")
        def pattern = Pattern.compile("versionCode=\"(\\d+)\"")
        def manifestText = manifestFile.getText()
        def matcher = pattern.matcher(manifestText)
        matcher.find()
        def versionCode = Integer.parseInt(matcher.group(1))

        if(project.hasProperty("vVersion")) {
            versionCode = vVersion
        } else {
            ++versionCode
        }

        def manifestContent = matcher.replaceAll("versionCode=\"" + ++versionCode + "\"")
        println "Version Code: " + versionCode
        manifestFile.write(manifestContent)
    }
}

task updateVersionName {
    doLast {

        // Usage example:
        // set manual version:
        // gradlew updateVersionName -P vName=1.0.0
        // or get the version from package.json:
        // gradlew updateVersionName

        def versionName

        if (project.hasProperty("vName")) {
            versionName = vName
        } else {
            def packageFile = file('../package.json')
            def json = new JsonSlurper().parseText(packageFile.text)
            versionName = json.version
        }

        def manifestFile = file("src/main/AndroidManifest.xml")
        def patternVersionNumber = Pattern.compile("versionName=\"(\\d+)\\.(\\d+)\\.(\\d+)(?:\\S*)\"")
        def manifestText = manifestFile.getText()
        def matcherVersionNumber = patternVersionNumber.matcher(manifestText)
        matcherVersionNumber.find()

        def manifestContent = matcherVersionNumber.replaceAll("versionName=\"" + versionName + "\"")
        manifestFile.write(manifestContent)

        println "Version name: " + versionName
    }
}

apply plugin: 'com.google.gms.google-services'
