apply plugin: 'com.android.application'
apply plugin: 'io.sentry.android.gradle'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'dagger.hilt.android.plugin'

def versionProperties = loadPropertiesFromFile(file("${rootDir}/version.properties"))

android {
    namespace 'com.automattic.simplenote'
    compileSdk 36

    buildTypes {
        debug {
            applicationIdSuffix '.debug'
        }

        release {
            minifyEnabled true
            shrinkResources false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }

        // Dedicated build type for screenshots so that we can add the special
        // permissions only to it, keeping debug and release closer together to
        // make bug hunting easier.
        screenshots {
            initWith debug
        }
    }

    defaultConfig {
        applicationId "com.automattic.simplenote"

        // Allow versionName to be overridden with property. e.g. -PversionName=1234
        if (project.hasProperty("versionName")) {
            versionName project.property("versionName")
        } else {
            versionName versionProperties.getProperty("versionName")
        }
        versionCode versionProperties.getProperty("versionCode").toInteger()

        minSdkVersion 23
        targetSdkVersion 35
    }


    testOptions {
        unitTests.returnDefaultValues = true
        // Disable animations in order to enter text in the text inputs in the
        // Espresso tests
        animationsDisabled = true
    }

    // Target Java8 to avoid use certain Kotlin's features
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_11
        targetCompatibility JavaVersion.VERSION_11
    }
    kotlinOptions {
        jvmTarget = JavaVersion.VERSION_11.toString()
    }

    viewBinding {
        enabled = true
    }
    lint {
        checkReleaseBuilds false
    }
}

buildscript {
    dependencies {
        classpath "io.sentry:sentry-android-gradle-plugin:$sentryVersion"
    }

    repositories {
        google()
        mavenCentral()
    }
}

dependencies {
    testImplementation 'junit:junit:4.13.2'
    testImplementation 'org.mockito:mockito-core:5.18.0'
    testImplementation 'org.mockito:mockito-inline:5.2.0'
    androidTestImplementation 'androidx.test:core:1.7.0'
    androidTestImplementation 'androidx.annotation:annotation:1.9.1'
    androidTestImplementation 'androidx.test:runner:1.7.0'
    androidTestImplementation 'androidx.test:rules:1.7.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.7.0'
    androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.7.0'
    androidTestImplementation('androidx.test.espresso:espresso-contrib:3.7.0') {
        // Necessary to avoid version conflicts
        exclude group: 'androidx', module: 'appcompat'
        exclude group: 'androidx', module: 'support-v4'
        exclude group: 'androidx', module: 'support-annotations'
        exclude module: 'recyclerview-v7'
    }
    // Infrastructure to test fragments
    testImplementation "androidx.arch.core:core-testing:2.2.0"
    testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.10.2"
    testImplementation "org.mockito.kotlin:mockito-kotlin:6.0.0"


    // Support for JsonObjects in unit tests since they are used by BucketObjects
    testImplementation 'org.json:json:20210307'

    // Fastlane screengrab for screenshots automation
    androidTestImplementation "tools.fastlane:screengrab:$screengrabVersion"
    // Automattic and WordPress dependencies
    implementation 'com.automattic:simperium:v1.4.0'
    implementation "com.automattic:Automattic-Tracks-Android:$automatticTracksVersion"
    implementation "com.automattic.tracks:crashlogging:$automatticTracksVersion"

    // Kotlin's coroutines
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.10.2'

    // Google Support
    implementation 'androidx.appcompat:appcompat:1.7.1'
    implementation 'com.google.android.material:material:1.12.0'
    implementation 'androidx.vectordrawable:vectordrawable:1.2.0'
    implementation 'androidx.preference:preference:1.2.1'
    implementation 'androidx.legacy:legacy-preference-v14:1.0.0'
    implementation 'androidx.work:work-runtime:2.10.3'
    implementation 'androidx.concurrent:concurrent-futures:1.3.0'
    implementation "androidx.constraintlayout:constraintlayout:2.2.1"
    implementation "androidx.multidex:multidex:2.0.1"
    // Support for ViewModels and LiveData
    implementation "androidx.activity:activity-ktx:1.10.1"
    implementation "androidx.fragment:fragment-ktx:1.8.8"
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$androidxLifecycleVersion"
    implementation "androidx.lifecycle:lifecycle-livedata-ktx:$androidxLifecycleVersion"
    // Support for androidx Fragments
    implementation "androidx.lifecycle:lifecycle-viewmodel-savedstate:$androidxLifecycleVersion"

    // Google Play Services
    implementation 'com.google.android.gms:play-services-wearable:19.0.0'
    // Third Party
    implementation 'com.squareup.okhttp3:okhttp:5.1.0'
    implementation("org.jetbrains:markdown:0.7.3")
    implementation 'net.openid:appauth:0.11.1'

    // Dagger Hilt dependencies
    implementation "com.google.dagger:hilt-android:$google_dagger"
    annotationProcessor "com.google.dagger:hilt-compiler:$google_dagger"
    kapt "com.google.dagger:hilt-compiler:$google_dagger"

    wearApp project(':Wear')
    implementation project(':PasscodeLock')
}

repositories {
    maven {
        url "https://a8c-libs.s3.amazonaws.com/android"
        content {
            includeGroup "com.automattic"
            includeGroup "com.automattic.tracks"
        }
    }
    maven { url "https://maven.google.com" }
    maven { url "https://s3.amazonaws.com/repo.commonsware.com" }
    mavenCentral()
    maven {
        url 'https://jitpack.io'
        content {
            includeGroup "com.github.Automattic"
        }
    }
}

if(["storeFile", "storePassword", "keyAlias", "keyPassword"].count { !project.hasProperty(it) } == 0 ){
    android {
      signingConfigs {
          release {
              storeFile = rootProject.file(project.storeFile)
              storePassword = project.storePassword
              keyAlias = project.keyAlias
              keyPassword = project.keyPassword
          }
      }
    }
    android.buildTypes.release.signingConfig = android.signingConfigs.release
}

task copyGradlePropertiesIfMissing(group: "generate", description: "Copy Gradle Properties") {
    if (!file("${rootDir}/Simplenote/gradle.properties").exists()) {
        copy {
            from("${rootDir}/Simplenote")
            into("${rootDir}/Simplenote")
            include('gradle.properties-example')
            rename('gradle.properties-example', 'gradle.properties')
        }
    }
}

static def loadPropertiesFromFile(inputFile) {
    def properties = new Properties()
    inputFile.withInputStream { stream ->
        properties.load(stream)
    }
    return properties
}

static String toSnakeCase( String text ) {
    text.replaceAll( /([A-Z])/, /_$1/ ).replaceAll( /^_/, '' )
}

android.applicationVariants.all { variant ->
    variant.generateBuildConfigProvider.configure {
        dependsOn(copyGradlePropertiesIfMissing)
    }
    def inputFile = file("${rootDir}/Simplenote/gradle.properties")
    def properties = loadPropertiesFromFile(inputFile)
    properties.any { property ->
        def key = toSnakeCase(property.key).toUpperCase()
        if (key == "SIMPERIUM_APP_ID" ||
            key == "SIMPERIUM_APP_KEY" ||
            key == "GOOGLE_ANALYTICS_ID" ||
            key == "LOGIN_EMAIL" ||
            key == "LOGIN_PASSWORD" ||
            key == "SENTRY_DSN" ||
            key == "WPCOM_CLIENT_ID" ||
            key == "SCREENSHOT_EMAIL" ||
            key == "SCREENSHOT_PASSWORD") {
            buildConfigField "String", key, "\"${property.value}\""
        }
    }
    buildConfigField "String", "BUILD", "\"${gitDescribe()}\""
    buildConfigField "String", "BUILD_HASH", "\"${gitHash()}\""
}

if (project.hasProperty("debugStoreFile")) {
    def sharedDebugStore = file(project.debugStoreFile.replaceFirst("^~", System.getProperty("user.home")))
    if (sharedDebugStore.exists()) {
        android {
            signingConfigs {
                debug {
                    storeFile sharedDebugStore
                }
            }
        }
    }
}
