/*
 * Copyright 2020 Adrián García
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

apply plugin: "com.android.application"

apply plugin: "kotlin-android"
apply plugin: "kotlin-kapt"

apply plugin: "io.gitlab.arturbosch.detekt"

apply plugin: "com.gladed.androidgitversion"
apply plugin: "com.hyperdevs.poeditor"

apply plugin: "de.mannodermaus.android-junit5"

apply from: "$rootDir/gradle/properties_utils.gradle"

androidGitVersion {
    codeFormat "MNNPP"
    tagPattern(/^[0-9]+.*/) // Tag names should follow the pattern vM.NN.PP
}

poEditor {
    // Provide your own PoEditor API token by setting the "apiToken" property in "/poeditor.properties"
    apiToken = loadEnvOrProperty("API_TOKEN", "", "poeditor.properties")
    projectId = 366581
}

ext {
    /*
     ********************
     * Android variables
     ********************
     */
    compile_sdk_version = 34
    min_sdk_version = 21
    max_sdk_version = 29
    target_sdk_version = 34
    build_tools_version = "34.0.0"
    // The Android version is multiplied by a big number so it is placed in the leftmost part
    // of the version code because we can't add it in androidGitVersion's codeFormat parameter
    version_code = compile_sdk_version * 100000 + androidGitVersion.code()
    version_name = androidGitVersion.name()

    /*
     ********************
     * Libraries
     ********************
     */
    kotlin_coroutines_version = "1.7.3"
    espresso_version = "3.5.1"
    mockito_version = "3.11.2"
    junit_jupiter_version = "5.9.2"
    fragment_version = "1.3.1"
    compose_ui_version = "1.5.0"
    compose_material_version = "1.5.0"
    compose_compiler_version = "1.4.8"
    accompanist_version = "0.30.1"
}

android {
    namespace "com.adriangl.pict2cam"
    compileSdk = compile_sdk_version
    buildToolsVersion = build_tools_version

    defaultConfig {
        applicationId "com.adriangl.pict2cam"
        minSdkVersion min_sdk_version
        targetSdkVersion target_sdk_version
        maxSdkVersion max_sdk_version
        versionCode version_code
        versionName version_name

        // Change the base name of the generated APK to match the project name and include the
        // binary version
        project.archivesBaseName = "${rootProject.name}-${version_name}"

        // Only bundle verified translations
        resConfigs("en", "es")

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    // New system introduced in AGP 4.0.0 to specify features that your build needs (or doesn't need) and improve build
    // performance.
    // For more info and default values, check: https://developer.android.com/studio/releases/gradle-plugin#buildFeatures
    buildFeatures {
        // Determines whether to generate a BuildConfig class.
        buildConfig = false
        // Determines whether to support View Binding.
        // Note that the viewBinding.enabled property is now deprecated.
        viewBinding = false
        // Determines whether to support Data Binding.
        // Note that the dataBinding.enabled property is now deprecated.
        dataBinding = false
        // Determines whether to generate binder classes for your AIDL files.
        aidl = false
        // Determines whether to support RenderScript.
        renderScript = false
        // Determines whether to support injecting custom variables into the module’s R class.
        resValues = false
        // Determines whether to support shader AOT compilation.
        shaders = false
        // Determines if Compose is enabled
        compose = true
    }

    signingConfigs {
        /*
         * Debug signing configuration
         *
         * Used to ensure that all developers use the same debug keystore
         */
        debug {
            storeFile file("$rootDir/debug.keystore")
            storePassword "android"
            keyAlias "androiddebugkey"
            keyPassword "android"
        }

        /*
         * Local release signing configuration
         *
         * Use this if you need to assemble a release version but you don"t have the proper release keys
         */
        localRelease {
            storeFile = file("${System.getProperty("user.home")}/.android/debug.keystore")
            storePassword "android"
            keyAlias "androiddebugkey"
            keyPassword "android"
        }

        /*
         * Release signing configurations
         *
         * Use them in CI environments or manual signing.
         * Requires configuration via signing.properties file or injected via environment variables.
         * The loadEnvOrProperty will try to find the environment variable (in snake_case) or the
         * property in signing.properties (converted to camelCase)
         */
        release {
            storeFile file(loadEnvOrProperty("RELEASE_KEYSTORE_PATH", "/", "signing.properties"))
            storePassword loadEnvOrProperty("RELEASE_KEYSTORE_PWD", "", "signing.properties")
            keyAlias loadEnvOrProperty("RELEASE_KEYSTORE_ALIAS", "", "signing.properties")
            keyPassword loadEnvOrProperty("RELEASE_KEYSTORE_ALIAS_PWD", "", "signing.properties")
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
            signingConfig signingConfigs.release
        }
    }

    sourceSets.configureEach {
        java.srcDirs += "src/${name}/kotlin"
    }

    compileOptions {
        // Flag to enable support for the new language APIs
        coreLibraryDesugaringEnabled true
        // Sets Java compatibility to Java 8
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = "1.8"
    }

    composeOptions {
        kotlinCompilerExtensionVersion compose_compiler_version
    }

    packagingOptions {
        resources {
            excludes += '/META-INF/{AL2.0,LGPL2.1}'
        }
    }
}

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

    // Java 8+ API desugaring support
    coreLibraryDesugaring "com.android.tools:desugar_jdk_libs:2.0.3"

    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlin_coroutines_version"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$kotlin_coroutines_version"

    // AndroidX dependencies
    implementation "androidx.appcompat:appcompat:1.6.1"
    implementation "androidx.core:core-ktx:1.10.1"

    // Jetpack Compose
    implementation "androidx.compose.ui:ui:$compose_ui_version"
    implementation "androidx.compose.ui:ui-util:$compose_ui_version"
    implementation "androidx.compose.material:material:$compose_material_version"
    implementation "androidx.compose.ui:ui-tooling-preview:$compose_ui_version"
    implementation "androidx.activity:activity-compose:1.7.2"

    // Accompanist
    implementation "com.google.accompanist:accompanist-pager:$accompanist_version"
    implementation "com.google.accompanist:accompanist-pager-indicators:$accompanist_version"

    // Misc
    implementation "com.theartofdev.edmodo:android-image-cropper:2.8.0"

    // Detekt formatting plugin
    detektPlugins("io.gitlab.arturbosch.detekt:detekt-formatting:${detekt_version}")

    // Testing
    testImplementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
    testImplementation "org.mockito:mockito-core:5.4.0"
    testImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0"

    // Use jUnit5 in unit tests
    testImplementation "junit:junit:4.13.2"
    testImplementation "org.junit.jupiter:junit-jupiter-api:$junit_jupiter_version"
    testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:$junit_jupiter_version"

    androidTestImplementation "androidx.test.uiautomator:uiautomator:2.2.0"

    androidTestImplementation "com.agoda.kakao:kakao:2.4.0"
    androidTestImplementation "androidx.test:runner:1.5.2"
    androidTestImplementation "androidx.test:rules:1.5.0"
    androidTestImplementation("androidx.test.espresso:espresso-core:$espresso_version", {
        exclude group: "androidx.annotation", module: "annotation"
    })
    androidTestImplementation "androidx.test.espresso:espresso-contrib:$espresso_version"
    androidTestImplementation "androidx.test.espresso:espresso-intents:$espresso_version"
    androidTestImplementation "androidx.test.espresso:espresso-web:$espresso_version"

    // Compose testing
    androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_ui_version"
    debugImplementation "androidx.compose.ui:ui-tooling:$compose_ui_version"
}

afterEvaluate {
    detekt {
        toolVersion = detekt_version
        source.setFrom(files(android.sourceSets.collect { it.java.srcDirs.collect { it.path } }.flatten()))
        config.setFrom(files("${project.rootDir}/config/detekt/detekt.yml"))
        autoCorrect = true
    }
}
