buildscript {
    ext {
        compose_version = '1.7.0-alpha02'
        compose_compiler_version = '1.5.9'
        compose_tooling = '1.2.0'
        customView = '1.2.0-alpha02'
        customViewPooling = '1.0.0'
        kotlin_version = '1.9.22'
        room = "2.6.1"
        KAHelpers = '3.2.2'
        moshi = "1.15.1"
        daggerVersion = '2.50'
        hiltJetpackVersion = '1.0.0'
        retrofit = "2.9.0"
        accompanist = "0.30.1"
        lifecycle = '2.8.0-alpha01'
        composed = '0.0.17'
        paging_compose = '3.3.0-alpha03'
        paging_runtime = '3.2.0-alpha01'
        navigation = '2.7.7'
        activity = "1.8.2"
        hilt_navigation_compose = "1.1.0"
        jsoup = "1.17.2"
        data_store = "1.0.0"
        coreKTX = "1.12.0"
        crashy = "1.2.0"
        coil = "2.6.0"
        work_version = "2.9.0"
        hilt_work = "1.1.0"

        //compilation
        compileVersion = 32
        minVersion = 24

        //updatable
        verCode = 4
        verName = "1.2.0"

        testRunner = "androidx.test.runner.AndroidJUnitRunner"
    }
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:8.3.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "com.google.dagger:hilt-android-gradle-plugin:$daggerVersion"

    }
}
task clean(type: Delete) {
    delete rootProject.buildDir
}

static def excludeParentFoldersFor(project) {
    def name = ""
    def allProjects = project.getAllprojects()
    if (allProjects.size() > 1) {
        name = allProjects.first().name
    }
    return name
}

subprojects {

    switch (it.name) {
        case "app":
            apply plugin: 'com.android.application'
            apply plugin: 'kotlin-android'
            apply plugin: 'kotlin-kapt'
            apply plugin: 'dagger.hilt.android.plugin'
            applyAndroid(it)
            break

    //ignore parent folders
        case excludeParentFoldersFor(it):
            break

    // kotlin/java only modules
        case ["serverconstants", "generalbook", "bookdetailsmodel", "bookmodel", "scrapermodel"]:
            apply plugin: 'java-library'
            apply plugin: 'kotlin'
            applyKotlinModule(it)
            break

        default:
            //setup gradle for libraries
            apply plugin: 'com.android.library'
            apply plugin: 'kotlin-android'
            applyAndroid(it)
            break
    }

}

def enableCompose(project) {
    project.android {
        buildFeatures {
            compose = true
        }
        composeOptions {
            kotlinCompilerExtensionVersion compose_compiler_version
        }
    }

}

def enableBuildConfig(project) {
    project.android {
        buildFeatures {
            buildConfig = true
        }
    }
}

def applyKotlinModule(project) {
    project.java {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
}

def applyAndroid(project) {
    project.android {

        compileSdkVersion compileVersion

        defaultConfig {
            minSdkVersion minVersion
            targetSdkVersion compileVersion
            versionCode verCode
            versionName verName
            testInstrumentationRunner testRunner
        }

        compileOptions {
            sourceCompatibility = 1.8
            targetCompatibility = 1.8
        }

        kotlinOptions {
            jvmTarget = "1.8"
            freeCompilerArgs += "-opt-in"
            freeCompilerArgs += "-Xjvm-default=all"
        }

        testOptions.unitTests {
            includeAndroidResources = true
        }

        buildFeatures {
            aidl = false
            renderScript = false
            resValues = false
            shaders = false
        }
    }

}


def applyRoomDeps(project) {
    project.dependencies {
        //room
        implementation "androidx.room:room-ktx:$room"
        implementation "androidx.room:room-paging:$room"
        kapt "androidx.room:room-compiler:$room"
    }
}

def applyHiltDeps(project) {
    project.dependencies {
        //hilt
        implementation "com.google.dagger:hilt-android:$daggerVersion"
        kapt "com.google.dagger:hilt-android-compiler:$daggerVersion"
    }
}

def applyComposeUIDeps(project) {
    enableCompose(project)
    project.dependencies {
        implementation "androidx.compose.ui:ui:$compose_version"
        implementation "androidx.compose.runtime:runtime:$compose_version"
        implementation "androidx.compose.compiler:compiler:$compose_compiler_version"
        implementation "androidx.compose.foundation:foundation:$compose_version"
        implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
        implementation "androidx.lifecycle:lifecycle-runtime-compose:$lifecycle"

        debugImplementation "androidx.compose.ui:ui-tooling:$compose_version"
        debugImplementation "androidx.customview:customview:$customView"
        debugImplementation "androidx.customview:customview-poolingcontainer:$customViewPooling"
    }
}

def applyMoshiDeps(project) {
    project.dependencies {
        implementation "com.squareup.moshi:moshi:$moshi"
        kapt "com.squareup.moshi:moshi-kotlin-codegen:$moshi"
    }
}

def applyCoilDep(project) {
    project.dependencies {
        implementation "io.coil-kt:coil-compose:$coil"
    }
}

def applyWorkerDeps(project) {
    project.dependencies {
        implementation "androidx.work:work-runtime-ktx:$work_version"
        implementation "androidx.hilt:hilt-work:$hilt_work"
        kapt "androidx.hilt:hilt-compiler:$hilt_work"
    }
}