import org.jetbrains.kotlin.gradle.dsl.JvmTarget

plugins {
    id("com.google.devtools.ksp").version("2.2.0-2.0.2")
}

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'org.sonarqube'
apply plugin: 'com.google.gms.google-services'
apply plugin: 'com.google.firebase.crashlytics'
apply plugin: 'com.mikepenz.aboutlibraries.plugin'

// apply signing configuration if config file exists
if (file('signing.gradle').exists()) {
    apply from: 'signing.gradle'
}

aboutLibraries {
    exclusionPatterns = [~"com_google_android.*", ~"android.*", ~"com_mikepenz__fastadapter_extensions.*"]
    configPath = "about-config"
    additionalLicenses {
        LGPL_2_1_or_later
        BSD_3_Clause
    }
}

kotlin {
    compilerOptions {
        jvmTarget = JvmTarget.JVM_17
        freeCompilerArgs = ['-XXLanguage:+PropertyParamAnnotationDefaultTargetMode']
    }
}

android {
    compileSdk = 36
    compileOptions {
        targetCompatibility JavaVersion.VERSION_17
        sourceCompatibility JavaVersion.VERSION_17
    }
    defaultConfig {
        applicationId "me.devsaki.hentoid"
        minSdkVersion 26
        targetSdkVersion 36
        versionCode 130 // is updated automatically by BitRise; only used when building locally
        versionName '1.20.26'

        def includeObjectBoxBrowser = System.getenv("INCLUDE_OBJECTBOX_BROWSER") ?: "false"
        def includeLeakCanary = System.getenv("INCLUDE_LEAK_CANARY") ?: "false"

        buildConfigField "boolean", "INCLUDE_OBJECTBOX_BROWSER", includeObjectBoxBrowser
        buildConfigField "boolean", "INCLUDE_LEAK_CANARY", includeLeakCanary

        buildConfigField 'String', 'GITHUB_API_URL', '"https://api.github.com/repos/avluis/Hentoid/"'

        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        resourceConfigurations += ['en', 'ru', 'it', 'uk', 'hu', 'ko', 'ja', 'es', 'sv']
// Only include complete translations
    }
    buildFeatures {
        viewBinding = true
        buildConfig = true
    }
    buildTypes {
        debug {
            applicationIdSuffix ".debug"
            versionNameSuffix "-DEBUG"

            buildConfigField 'String', 'UPDATE_URL', '"https://raw.githubusercontent.com/avluis/Hentoid/dev/app/"'

            proguardFile 'proguard-rules-debug.pro'

            // Library specific proguard files
            proguardFiles fileTree(dir: '../.proguard', include: ['*.pro']).asList().toArray()
        }
        release {
            debuggable false
            minifyEnabled true

            buildConfigField 'String', 'UPDATE_URL', '"https://raw.githubusercontent.com/avluis/Hentoid/master/app/"'

            proguardFile 'proguard-rules-release.pro'
            testProguardFile 'proguard-rules-test.pro'

            // Library specific proguard files
            proguardFiles fileTree(dir: '../.proguard', include: ['*.pro']).asList().toArray()
        }
    }
    packagingOptions {
        resources {
            // Added there to resolve a conflict between library dependencies
            pickFirsts.add('LICENSE-EDL-1.0.txt')
            pickFirsts.add('LICENSE-EPL-1.0.txt')
        }
        jniLibs {
            useLegacyPackaging = true
        }
    }
    testOptions {
        unitTests.includeAndroidResources = true
    }
    lint {
        informational 'MissingTranslation'
        informational 'ExtraTranslation'
    }
    namespace = 'me.devsaki.hentoid'
}

dependencies {
    implementation project(path: ':app:gles-renderer')
    implementation project(path: ':app:customssiv')
    implementation files('../ai-upscale/ai-upscale.aar')

    def includeObjectBoxBrowser = System.getenv("INCLUDE_OBJECTBOX_BROWSER") ?: "false"
    def includeLeakCanary = System.getenv("INCLUDE_LEAK_CANARY") ?: "false"

    // Compile any included libraries
    implementation fileTree(include: ['*jar'], dir: 'libs')
    implementation fileTree(include: ['*aar'], dir: 'libs')

    /**
     * TESTING
     */
    testImplementation 'junit:junit:4.13.2'
    testImplementation 'org.mockito:mockito-core:5.19.0'
    testImplementation 'androidx.test:core:1.7.0'
    testImplementation 'io.kotlintest:kotlintest-assertions:3.4.2'
    testImplementation 'androidx.arch.core:core-testing:2.2.0'
    testImplementation 'org.robolectric:robolectric:4.15.1'
    // Auto Timber trees that redirect logs to the JUnit output
    testImplementation 'net.lachlanmckee:timber-junit-rule:1.0.1'
    androidTestImplementation 'androidx.test:runner:1.7.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.7.0'


    /**
     * ANDROID FRAMEWORK
     */
    // Support libraries
    implementation 'androidx.recyclerview:recyclerview:1.4.0'
    implementation 'androidx.appcompat:appcompat:1.7.1'
    implementation 'com.google.android.material:material:1.13.0'
    implementation 'androidx.preference:preference:1.2.1'
    implementation 'androidx.constraintlayout:constraintlayout:2.2.1'
    implementation 'androidx.paging:paging-runtime:2.1.2'
    implementation 'androidx.fragment:fragment-ktx:1.8.9'
    implementation "androidx.documentfile:documentfile:1.1.0"
    // <-- if documentfile is bumped, please examine CachedDocumentsContract for evolutions
    implementation 'androidx.viewpager2:viewpager2:1.1.0'
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.10.2'
    implementation 'androidx.core:core-ktx:1.17.0'
    implementation 'androidx.webkit:webkit:1.14.0'

    def android_work_version = '2.10.4'
    implementation "androidx.work:work-runtime:$android_work_version"
    implementation "androidx.work:work-runtime-ktx:$android_work_version"

    // For ViewModel to work
    def android_lifecycle_version = '2.9.4'
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$android_lifecycle_version"
    implementation "androidx.lifecycle:lifecycle-process:$android_lifecycle_version"
    implementation "androidx.lifecycle:lifecycle-livedata-ktx:$android_lifecycle_version"
    implementation "androidx.lifecycle:lifecycle-runtime-ktx:$android_lifecycle_version"


    /**
     * MEDIA
     */
    // Image loader and animated pics support -> https://github.com/coil-kt/coil
    def Coil_version = '3.3.0'
    implementation("io.coil-kt.coil3:coil:$Coil_version")
    implementation("io.coil-kt.coil3:coil-network-okhttp:$Coil_version")
    implementation("io.coil-kt.coil3:coil-gif:$Coil_version")

    // Coil support for JXL
    implementation 'io.github.awxkee:jxl-coder-coil:2.5.0'

    // Coil support for AVIF
    implementation 'com.github.awxkee:avif-coder-coil:2.1.4'

    // Animated pics that Coil doesn't support (APNG, animated AVIF) -> https://github.com/penfeizhou/APNG4Android
    def APNG4Android_version = '3.0.5'
    implementation "com.github.penfeizhou.android.animation:apng:$APNG4Android_version"
    implementation "com.github.penfeizhou.android.animation:avif:$APNG4Android_version"

    // Animated GIF creator -> https://github.com/shaksternano/gif.kt
    implementation("com.shakster:gifkt:0.1.2")

    // PDF creator/extractor
    implementation 'com.itextpdf:itext7-core:8.0.5'

    /**
     * UI
     */
    implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0"

    // https://github.com/mikepenz/FastAdapter
    def fastadapter_version = '5.7.0'
    implementation "com.mikepenz:fastadapter:$fastadapter_version"
    implementation "com.mikepenz:fastadapter-extensions-diff:$fastadapter_version"
    implementation "com.mikepenz:fastadapter-extensions-swipe:$fastadapter_version"
    implementation "com.mikepenz:fastadapter-extensions-drag:$fastadapter_version"
    implementation "com.mikepenz:fastadapter-extensions-expandable:$fastadapter_version"
    implementation "com.mikepenz:fastadapter-extensions-scroll:$fastadapter_version"
    implementation "com.mikepenz:fastadapter-extensions-paged:$fastadapter_version"
    implementation "com.mikepenz:fastadapter-extensions-ui:$fastadapter_version"
    implementation "com.mikepenz:fastadapter-extensions-utils:$fastadapter_version"

    // Intro screens or slides: github.com/apl-devs/AppIntro
    implementation 'com.github.AppIntro:AppIntro:6.1.0'

    // Tooltips
    implementation 'com.github.skydoves:balloon:1.6.13'

    // Popup menus with icons
    implementation 'com.github.skydoves:powermenu:2.2.4'

    // Micro menu
    implementation "com.github.skydoves:submarine:1.0.7"

    // Fast scroller
    implementation 'me.zhanghai.android.fastscroll:library:1.3.0'

    // Specific UI layout to display the tag mosaic : github.com/google/flexbox-layout
    implementation 'com.google.android.flexbox:flexbox:3.0.0'

    // Format recyclerView item as a badge https://github.com/nekocode/Badge
    implementation "com.github.nekocode:Badge:2.1"


    /**
     * NETWORKING
     */
    // JSON serialization/deserialization (Java Objects into JSON and back): https://github.com/square/moshi
    def moshi_version = '1.15.2'
    implementation "com.squareup.moshi:moshi:$moshi_version"
    implementation "com.squareup.moshi:moshi-adapters:$moshi_version"
    implementation "com.squareup.moshi:moshi-kotlin:$moshi_version"
    ksp("com.squareup.moshi:moshi-kotlin-codegen:$moshi_version")

    // HTTP & HTTP/2 client: github.com/square/okhttp
    def okhttpVersion = '5.1.0'
    implementation "com.squareup.okhttp3:okhttp:$okhttpVersion"
    implementation "com.squareup.okhttp3:okhttp-dnsoverhttps:$okhttpVersion"
    implementation "com.squareup.okhttp3:okhttp-brotli:$okhttpVersion"

    // Retrofit-ready-ready HTML parser with CSS selectors : https://github.com/DroidsOnRoids/jspoon; uses JSOUP
    implementation "pl.droidsonroids:jspoon:1.3.3"

    // JSOUP HTML parser: github.com/jhy/jsoup
    implementation 'org.jsoup:jsoup:1.21.2'

    //    https://github.com/square/retrofit
    def retrofit_version = '3.0.0'
    implementation "com.squareup.retrofit2:retrofit:$retrofit_version"
    implementation "com.squareup.retrofit2:converter-moshi:$retrofit_version"

    // Download speed limit
    implementation 'com.bucket4j:bucket4j_jdk17-core:8.15.0'

    // TLS v1.3 support for Android <= 9
    implementation 'org.conscrypt:conscrypt-android:2.5.3'

    /**
     * UTILS
     */

    // ObjectBox browser dependencies must be set before applying ObjectBox plugin so it does not add objectbox-android
    // (would result in two conflicting versions, e.g. "Duplicate files copied in APK lib/armeabi-v7a/libobjectbox.so").
    def objectbox_version = "4.3.1"
    if (includeObjectBoxBrowser.toBoolean()) {
        debugImplementation "io.objectbox:objectbox-android-objectbrowser:$objectbox_version"
        releaseImplementation "io.objectbox:objectbox-android:$objectbox_version"
    }

    // Timber (Extensible Logger): github.com/JakeWharton/timber
    implementation 'com.jakewharton.timber:timber:5.0.1'

    // EventBus: github.com/greenrobot/EventBus
    implementation 'org.greenrobot:eventbus:3.3.1'

    // LeakCanary: github.com/square/leakcanary
    if (includeLeakCanary.toBoolean()) {
        debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.12'
    }

    // https://mvnrepository.com/artifact/org.apache.commons/commons-text/
    implementation 'org.apache.commons:commons-text:1.14.0'

    // Archive management
    implementation 'com.github.omicronapps:7-Zip-JBinding-4Android:Release-16.02-2.03'

    // Natural strings comparator
    implementation 'net.grey-panther:natural-comparator:1.1'

    // Efficient hashing
    implementation 'io.whitfin:siphash:2.0.0'

    // Sentence similarity detection
    implementation 'info.debatty:java-string-similarity:2.0.0'

    // Biometrics
    implementation 'dev.skomlach:biometric:2.2.51'

    // Searchable prefs
    implementation 'com.github.ByteHamster:SearchPreference:2.7.3'

    // Automated generation of the Licenses screen
    def about_version = '8.9.4'
    implementation "com.mikepenz:aboutlibraries-core:$about_version"
    implementation "com.mikepenz:aboutlibraries:$about_version"


    /**
     * ANALYTICS
     */
    // Import the BoM for the Firebase platform
    implementation platform('com.google.firebase:firebase-bom:33.11.0')

    // Crashlytics
    implementation 'com.google.firebase:firebase-crashlytics'
    implementation 'com.google.firebase:firebase-analytics'
}

sonarqube {
    properties {
        property "sonar.projectKey", "RobbWatershed_hentoid-sonar"
        property "sonar.organization", "robbwatershed-github"
        property "sonar.host.url", "https://sonarcloud.io"
        property "sonar.language", "java"
        property "sonar.sources", "src/main/"
        property "sonar.sourceEncoding", "UTF-8"
        property "sonar.login", "3d0dc80792bad95f0193e7ed626cf090ac83e208"
    }
}

apply plugin: 'io.objectbox'
apply plugin: 'org.jetbrains.kotlin.android'// apply last