plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android' version '2.0.21'  // Apply here directly
    id 'com.google.gms.google-services'
    id 'com.google.firebase.crashlytics'
    id 'com.google.devtools.ksp'
    id 'kotlin-kapt'
}

def keystorePropertiesFile = rootProject.file("keystore.properties")
def keystoreProperties = new Properties()
if (keystorePropertiesFile.exists() && keystorePropertiesFile.canRead()) {
    keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
}

android {
    compileSdkVersion=35
    buildToolsVersion='35.0.0'
    defaultConfig {
        applicationId "io.github.gmathi.novellibrary"
        minSdkVersion 23
        targetSdkVersion 35
        multiDexEnabled true
        versionCode 119
        versionName "0.26.1.beta"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables.useSupportLibrary = true
    }

    signingConfigs {
        if (keystorePropertiesFile.exists() && keystorePropertiesFile.canRead()) {
            release {
                keyAlias keystoreProperties['keyAlias']
                keyPassword keystoreProperties['keyPassword']
                storeFile file(keystoreProperties['storeFile'])
                storePassword keystoreProperties['storePassword']
            }
        }
    }

    buildTypes {
        debug {
            pseudoLocalesEnabled true
            getIsDefault().set(true)
        }

        release {
            if (keystorePropertiesFile.exists() && keystorePropertiesFile.canRead()) {
                signingConfig signingConfigs.release
            }
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    flavorDimensions = ["mode"]
    productFlavors {
        mirror {
            // Flavor adds a suffix to the package name, so the
            // build can be installed alongside the main build.
            dimension "mode"
            applicationIdSuffix ".mirror"
        }

        canary {
            // Flavor to enable leekCanary.
            dimension "mode"
        }

        normal {
            // Normal build.
            dimension "mode"
            getIsDefault().set(true)
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
    }

    kotlinOptions {
        jvmTarget = JavaVersion.VERSION_17.toString()
    }

    buildFeatures {
        dataBinding true
        viewBinding true
        buildConfig true
    }
    sourceSets {
        mirror {
            res {
                srcDirs 'src\\mirror\\res'
            }
        }
    }
    namespace 'io.github.gmathi.novellibrary'

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar', '*.aar'])
    implementation 'androidx.constraintlayout:constraintlayout:2.2.1'
    implementation 'androidx.browser:browser:1.8.0'
    implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.8.7'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.8.7'

    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.2.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.6.1'

    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.appcompat:appcompat:1.7.0'
    implementation 'androidx.cardview:cardview:1.0.0'
    implementation 'com.google.android.material:material:1.12.0'
    implementation 'androidx.vectordrawable:vectordrawable:1.2.0'
    implementation 'androidx.preference:preference-ktx:1.2.1'

    implementation 'androidx.multidex:multidex:2.0.1'

    implementation 'org.jsoup:jsoup:1.16.1'
    implementation 'org.greenrobot:eventbus:3.3.1'

    implementation 'com.github.chrisbanes:PhotoView:2.3.0'
    implementation 'com.github.medyo:fancybuttons:1.8.3'

    implementation 'com.ogaclejapan.smarttablayout:library:2.0.0@aar'
    implementation 'jp.wasabeef:recyclerview-animators:4.0.2'
    implementation 'nl.dionsegijn:konfetti:1.3.2'

    implementation('com.firebaseui:firebase-ui-auth:8.0.2', {
        exclude group: 'com.google.firebase'
        exclude group: 'com.google.android.gms'
        exclude group: 'com.android.support'
        exclude group: 'com.google.api-client'
        exclude group: 'com.google.apis'
    })

    final material_dialogs_version = '3.3.0'
    implementation "com.afollestad.material-dialogs:core:$material_dialogs_version"
    implementation "com.afollestad.material-dialogs:input:$material_dialogs_version"
    implementation "com.afollestad.material-dialogs:color:$material_dialogs_version"
    implementation "com.afollestad.material-dialogs:lifecycle:$material_dialogs_version"

    implementation "com.google.android.gms:play-services-gcm:17.0.0"
    implementation "com.google.android.gms:play-services-drive:17.0.0"
    implementation 'com.google.android.gms:play-services-auth:21.3.0'

    def work_version = '2.10.0'
    // Job scheduling
    implementation "androidx.work:work-runtime-ktx:$work_version"
    implementation "androidx.work:work-gcm:$work_version"
    androidTestImplementation "androidx.work:work-testing:$work_version"

    implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'


    implementation 'com.airbnb.android:lottie:6.1.0'
    implementation 'com.github.chimbori:crux:2.2.0'

    implementation "uy.kohesive.injekt:injekt-core:1.16.1"

    // Network client
    final okhttp_version = '5.0.0-alpha.2'
    implementation "com.squareup.okhttp3:okhttp:$okhttp_version"
    implementation "com.squareup.okhttp3:logging-interceptor:$okhttp_version"
    implementation "com.squareup.okhttp3:okhttp-dnsoverhttps:$okhttp_version"
    implementation 'com.squareup.okio:okio:3.5.0'

    // TLS 1.3 support for Android < 10
    implementation 'org.conscrypt:conscrypt-android:2.5.2'

    // REST
    final retrofit_version = '2.9.0'
    implementation "com.squareup.retrofit2:retrofit:$retrofit_version"
    implementation 'com.jakewharton.retrofit:retrofit2-kotlinx-serialization-converter:1.0.0'
    implementation "com.squareup.retrofit2:converter-gson:$retrofit_version"
    implementation "com.squareup.retrofit2:adapter-rxjava:$retrofit_version"

    //ReactiveX
    implementation "io.reactivex:rxandroid:1.2.1"
    implementation "io.reactivex:rxjava:1.3.8"
    implementation "com.jakewharton.rxrelay:rxrelay:1.2.0"
    implementation "com.github.pwittchen:reactivenetwork:0.13.0"


    canaryImplementation 'com.squareup.leakcanary:leakcanary-android:2.12'

    implementation 'com.github.bumptech.glide:glide:4.16.0'
    ksp 'com.github.bumptech.glide:ksp:4.16.0'

    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:2.0.21"

    final kotlinx_coroutines_version = '1.7.3'
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlinx_coroutines_version"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$kotlinx_coroutines_version"

    final kotlinSerializationVersion = '1.6.0'
    implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:$kotlinSerializationVersion"
    implementation "org.jetbrains.kotlinx:kotlinx-serialization-protobuf:$kotlinSerializationVersion"
    implementation 'com.google.code.gson:gson:2.10.1'
    implementation "com.github.salomonbrys.kotson:kotson:2.5.0"

    // Image library
    final coilVersion = '2.4.0'
    implementation("io.coil-kt:coil:$coilVersion")
    implementation("io.coil-kt:coil-gif:$coilVersion")


    implementation 'com.github.tingyik90:snackprogressbar:6.4.2'

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

    implementation 'com.google.firebase:firebase-analytics-ktx:22.4.0'

    implementation 'com.google.firebase:firebase-core:21.1.1'
    implementation 'com.google.firebase:firebase-config-ktx:22.1.0'
    implementation 'com.google.firebase:firebase-crashlytics-ktx:19.4.2'
    implementation 'com.google.firebase:firebase-messaging-ktx:24.1.1'
    implementation 'com.google.firebase:firebase-auth-ktx:23.2.0'
    implementation 'com.google.firebase:firebase-analytics-ktx:22.4.0'

    // Markdown parser for websites that use markdown
    implementation 'org.jetbrains:markdown:0.5.0'
}

tasks.register('countTranslations') {
    File json = file('src\\main\\assets\\translations.json')

    Set<File> resources = fileTree('src\\main\\res')
            .files.findAll { (it.name == 'strings.xml') }
    File englishResources = resources.find { it.parentFile.name == 'values' }
    resources.remove(englishResources)

    List<String> englishStrings = englishResources.text.readLines()
            .findAll { it.trim().startsWith('<string name="') && !it.contains('translatable="false"') && it.endsWith('</string>') }
            .collect { it.trim().split('">')[1].split('</')[0] }

    json.text = '{\r\n  "en": "' + englishStrings.size() + '"'

    resources.each { resource ->
        List<String> strings = resource.text.readLines()
                .findAll { it.trim().startsWith('<string name="') && !it.contains('translatable="false"') && it.endsWith('</string>') }
                .collect { it.trim().split('">')[1].split('</')[0] }
                .findAll { !englishStrings.contains(it) }
        json.text += ',\r\n  "' + resource.parentFile.name.split('-')[1] + '": "' + strings.size() + '"'
    }

    json.text += '\r\n}'
    //print(json.text)
}

repositories {
    google()
    mavenCentral()
    maven { url "https://maven.google.com" }
    maven { url "https://jitpack.io" }
    maven { url 'https://maven.fabric.io/public' }
}
