/*
 * Copyright (C) 2013-2025 Federico Iosue (developer@omninotes.app)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

plugins {
    id "kotlin-android"
    id "com.android.application"
}

boolean signConfigExists = project.hasProperty("SIGN_PROPS") && new File(project.property("SIGN_PROPS")).exists()
if (signConfigExists) {
    Properties props = new Properties()
    props.load(new FileInputStream(file(project.property("SIGN_PROPS"))))

    android {
        signingConfigs {
            release {
                storeFile file(props['keystore'])
                storePassword props['keystore.password']
                keyAlias props['keystore.alias']
                keyPassword props['keystore.alias.password']
            }
        }
    }
}

android {
    compileSdk = Integer.parseInt(project.TARGET_SDK)

    defaultConfig {
        applicationId = project.PACKAGE
        minSdkVersion = project.MIN_SDK
        targetSdkVersion = project.TARGET_SDK
        compileSdkVersion = Integer.parseInt(project.COMPILE_SDK)
        versionName = project.VERSION_NAME
        versionCode = Integer.parseInt(project.VERSION_CODE)
        vectorDrawables.useSupportLibrary = true
        testInstrumentationRunner = 'androidx.test.runner.AndroidJUnitRunner'
        multiDexEnabled = true
    }

    buildFeatures {
        viewBinding = true
        buildConfig = true
    }

    testOptions {
        unitTests.returnDefaultValues = true
        animationsDisabled = true
    }
    packagingOptions {
        resources {
            excludes += ['META-INF/LICENSE.txt', 'META-INF/NOTICE.txt']
        }
    }


    buildTypes {
        release {
            minifyEnabled = true
            shrinkResources = false
            multiDexEnabled = false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
            enableUnitTestCoverage = false
            enableAndroidTestCoverage = false
            android.applicationVariants.all { variant ->
                variant.outputs.all {
                    outputFileName = "OmniNotes-${variant.name}-${variant.versionName}.apk"
                }
            }
            if (signConfigExists) {
                signingConfig = signingConfigs.release
            }
        }
        debug {
            enableUnitTestCoverage = true
            enableAndroidTestCoverage = true
        }
    }

    flavorDimensions = ["standard"]
    productFlavors {
        play {
            dimension "standard"
        }
        foss {
            dimension "standard"
            applicationIdSuffix ".foss"
        }
        beta {
            dimension "standard"
        }
        alpha {
            dimension "standard"
            applicationIdSuffix ".alpha"
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
    }

    kotlinOptions {
        jvmTarget = JavaVersion.VERSION_17
    }
    lint {
        abortOnError = false
        disable = ['MissingTranslation']
    }
    namespace = 'it.feio.android.omninotes'
}

dependencies {
    testImplementation 'junit:junit:4.13.2'
    testImplementation 'org.mockito:mockito-core:5.6.0'

    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    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') {
        exclude group: 'com.android.support', module: 'support-annotations'
    }
    androidTestImplementation('com.android.support.test.espresso:espresso-contrib:3.0.2') {
        exclude group: 'com.android.support', module: 'appcompat'
        exclude group: 'com.android.support', module: 'support-v4'
        exclude module: 'recyclerview-v7'
    }

    debugImplementation('com.squareup.leakcanary:leakcanary-android:2.12') {
        exclude group: "com.android.support"
    }

    compileOnly 'org.projectlombok:lombok:1.18.30'
    annotationProcessor 'org.projectlombok:lombok:1.18.30'

    annotationProcessor 'org.androidannotations:androidannotations:4.8.0'
    implementation 'org.androidannotations:androidannotations-api:4.8.0'

    implementation 'androidx.core:core-ktx:1.10.1'
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.8.10"
    implementation 'com.larswerkman:HoloColorPicker:1.5'
    implementation 'de.keyboardsurfer.android.widget:crouton:1.8.5@aar'
    implementation 'com.google.android.apps.dashclock:dashclock-api:2.0.0'
    implementation 'ch.acra:acra-http:5.11.2'
    implementation 'ch.acra:acra-toast:5.11.2'
    implementation 'commons-io:commons-io:2.16.1'
    implementation 'com.github.gabrielemariotti.changeloglib:changelog:2.1.0'
    implementation 'com.github.vikramkakkar:SublimePicker:8c4de22db1'
    implementation 'androidx.preference:preference-ktx:1.2.1'
    implementation 'androidx.recyclerview:recyclerview:1.3.2'
    implementation 'com.android.support:multidex:2.0.1'
    implementation 'com.google.android.material:material:1.9.0'
    implementation 'de.greenrobot:eventbus:2.4.0'
    implementation 'com.pushbullet:android-extensions:1.0.4@aar'
    implementation 'com.getbase:floatingactionbutton:1.10.1'
    implementation 'com.github.bumptech.glide:glide:4.8.0'   // To be modified with Gallery library
    annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0' // To be modified with Gallery library
    implementation('com.github.afollestad.material-dialogs:core:0.9.6.0') {
        transitive = true
        exclude group: "com.android.support"
    }
    implementation('com.github.afollestad.material-dialogs:commons:0.9.6.0') {
        transitive = true
        exclude group: "com.android.support"
    }
    implementation('org.mnode.ical4j:ical4j:3.0.11') {
        exclude group: 'commons.io'
    }
    implementation 'org.apache.commons:commons-lang3:3.9'
    implementation 'com.pnikosis:materialish-progress:1.5'
    implementation 'com.github.paolorotolo:appintro:1.3.0'
    implementation 'com.guolindev.permissionx:permissionx:1.8.0'
    implementation 'org.ocpsoft.prettytime:prettytime:3.2.7.Final'
    implementation 'org.bitbucket.cowwoc:diff-match-patch:1.2'
    implementation 'com.github.bosphere.android-filelogger:filelogger:1.0.7'
    implementation 'com.github.sevar83:indeterminate-checkbox:1.0.5@aar'
    implementation 'com.pixplicity.easyprefs:EasyPrefs:1.10.0'
    implementation 'com.lazygeniouz:dfc:0.8'

    implementation('com.github.federicoiosue:simplegallery:3.0.1') {
        exclude group: 'com.android.support'
    }
    implementation('com.github.federicoiosue:Omni-Notes-Commons:1.4.0') {
        exclude group: 'org.projectlombok'
    }
    implementation('com.github.federicoiosue:checklistview:4.1.0') {
        transitive = false
    }
    implementation 'com.github.federicoiosue:pixlui:3.0.1'

    // Flavors specific dependencies
    playImplementation 'com.google.android.gms:play-services-location:21.3.0'
    betaImplementation 'com.google.android.gms:play-services-location:21.3.0'
    alphaImplementation 'com.google.android.gms:play-services-location:21.3.0'
//    fossImplementation 'com.jakewharton.timber:timber:5.0.1'
}

def jacocoVersion = "0.8.7"

configurations.configureEach {
    exclude group: 'commons-logging', module: 'commons-logging'
    resolutionStrategy {
        force 'com.android.support:support-annotations:28.0.0'
        force 'com.android.support:support-v4:28.0.0'
        eachDependency { details ->
            if ('org.jacoco' == details.requested.group) {
                details.useVersion jacocoVersion
            }
        }
    }
}

android.productFlavors.each { flavor ->
    String analyticsUrl = flavor.name == 'foss' && ANALYTICS_PARAMS_FOSS?.trim() ? ANALYTICS_PARAMS_FOSS : ANALYTICS_PARAMS
    flavor.buildConfigField 'String', 'ANALYTICS_PARAMS', '"' + analyticsUrl + '"'
    flavor.buildConfigField 'String', 'CRASH_REPORTING_URL', '"' + CRASH_REPORTING_URL + '"'
    flavor.buildConfigField 'String', 'CRASH_REPORTING_LOGIN', '"' + CRASH_REPORTING_LOGIN + '"'
    flavor.buildConfigField 'String', 'CRASH_REPORTING_PASSWORD', '"' + CRASH_REPORTING_PASSWORD + '"'
    flavor.buildConfigField 'String', 'VERSION_CHECK_URL', '"' + VERSION_CHECK_URL + '"'
}

apply plugin: 'jacoco'
jacoco {
    toolVersion = jacocoVersion
}
task jacocoTestReport(type: JacocoReport, dependsOn: ['testAlphaDebugUnitTest', 'createAlphaDebugCoverageReport', 'connectedPlayDebugAndroidTest']) {
    reports {
        xml.required = true
        html.required = true
    }

    def fileFilter = ['**/R.class', '**/R$*.class', '**/BuildConfig.*', '**/Manifest*.*', '**/*Test*.*', 'android/**/*.*']
    def debugTree = fileTree(dir: "${buildDir}/intermediates/classes/debug", excludes: fileFilter)
    def mainSrc = "${project.projectDir}/src/main/java"

    sourceDirectories.from = files([mainSrc])
    classDirectories.from = files([debugTree])
    executionData.from = fileTree(dir: "$buildDir", includes: [
            "jacoco/testAlphaDebugUnitTest.exec",
            "outputs/code_coverage/alphaDebugAndroidTest/connected/*coverage.ec"
    ])
}

apply plugin: 'com.adarshr.test-logger'
tasks.withType(Test).configureEach {
    jacoco.includeNoLocationClasses = true
    jacoco.excludes = ['jdk.internal.*']

    testLogging {
        events "standardOut", "standardError", "started", "passed", "skipped", "failed"
        exceptionFormat "full"
    }
}

tasks.register('wrapper', Wrapper) {
    gradleVersion = '8.7'
    validateDistributionUrl = false
}
