import com.android.build.api.dsl.ApplicationProductFlavor

plugins {
    id("com.android.application")
    kotlin("android")
    id("kotlin-parcelize")
    kotlin("kapt")
    id("dagger.hilt.android.plugin")
    //TODO - enable later: id("com.google.gms.google-services")
    id("com.google.firebase.crashlytics")

}

android {
    namespace = Version.applicationId
    compileSdk = 36


    defaultConfig {
        applicationId = Version.applicationId
        minSdk = Version.minSdk
        targetSdk = Version.targetSdk
        compileSdk = Version.targetSdk
        versionCode = Version.versionCode
        versionName = Version.versionName
        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
        setProperty("archivesBaseName", "${Version.applicationId}-build${Version.versionCode}")
        multiDexEnabled = true
        ndk {
            abiFilters += listOf("armeabi", "armeabi-v7a", "arm64-v8a", "x86", "x86_64")
        }
        renderscriptTargetApi = 23
        renderscriptSupportModeEnabled = true
    }

    kapt {
        correctErrorTypes = true
        arguments {
            arg("AROUTER_MODULE_NAME", project.name)
        }
    }



    hilt {
        enableExperimentalClasspathAggregation = true
        enableAggregatingTask = false
    }

    buildFeatures {
        dataBinding = true
        viewBinding = true
    }

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

    signingConfigs {
        val storeFileName = "storeFile"
        val storePasswordName = "KEYSTORE_PWD"
        val keyAliasName = "KEY_ALIAS"
        val keyPasswordName = "KEY_PWD"

        fun names(suffix: String = "") =
            listOf(storeFileName + suffix, storePasswordName + suffix, keyAliasName + suffix, keyPasswordName + suffix)

        create("release") {
            storeFile = file(project.properties[names()[0]]?.toString() ?: "")
            storePassword = project.properties[names()[1]] as? String?
            keyAlias = project.properties[names()[2]] as? String?
            keyPassword = project.properties[names()[3]] as? String?
        }
        getByName("debug") {
            storeFile = file(project.properties[names()[0]]?.toString() ?: "")
            storePassword = project.properties[names()[1]] as? String?
            keyAlias = project.properties[names()[2]] as? String?
            keyPassword = project.properties[names()[3]] as? String?
        }
    }



    buildTypes {
        release {
            isMinifyEnabled = false
            isShrinkResources = false
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
        debug {
            isMinifyEnabled = false
            isShrinkResources = false
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    compileOptions {
        isCoreLibraryDesugaringEnabled = true
        targetCompatibility(JavaVersion.VERSION_17)
        sourceCompatibility(JavaVersion.VERSION_17)
    }



    val flavorDimensionName = "VisualNovel"
    flavorDimensions.add(flavorDimensionName)
    productFlavors {
        fun ApplicationProductFlavor.buildConfigString(name: String, value: String) =
            buildConfigField("String", name, "\"$value\"")

        fun ApplicationProductFlavor.buildConfigBoolean(name: String, value: String) =
            buildConfigField("Boolean", name, value)

        create("novelTest") {
            dimension = flavorDimensionName
            signingConfig = signingConfigs.getByName("debug")

            buildConfigString("HOST", "https://www.xxxxx.ai/")
            buildConfigString("ABOUT_US", "https://www.xxxxx.ai/about")
            buildConfigString("API_FROG", "https://www.test-frog.xxxxx.ai")
            buildConfigString("EPAL_TERMS_SERVICES", "https://www.xxxxx.ai/policy/tos")
            buildConfigString("API_SHARK", "https://test-shark.xxxxx.ai")
            buildConfigString("API_COW", "https://test-cow.xxxxx.ai")
            buildConfigString("API_PIGEON", "https://test-pigeon.xxxx.ai")
            buildConfigString("API_LION", "https://test-lion.xxxx.ai")
            buildConfigString("RECHAEGE_SERVICES", "https://test.xxxxx.ai/policy/recharge")

            buildConfigString("RTC_APP_ID", "689ade491323ae01797818e0-XXX-TODO")
        }


        create("product") {
            dimension = flavorDimensionName
            signingConfig = signingConfigs.getByName("release")

            buildConfigString("HOST", "https://test.xxxxx.ai/")
            buildConfigString("ABOUT_US", "https://test.xxxxx.ai/about")
            buildConfigString("API_FROG", "https://test-frog.xxxxx.ai")
            buildConfigString("EPAL_TERMS_SERVICES", "https://test.xxxxx.ai/policy/tos")
            buildConfigString("API_SHARK", "https://test-shark.xxxxx.ai")
            buildConfigString("API_COW", "https://test-cow.xxxxx.ai")
            buildConfigString("API_PIGEON", "https://test-pigeon.xxxx.ai")
            buildConfigString("API_LION", "https://test-lion.xxxx.ai")
            buildConfigString("RECHAEGE_SERVICES", "https://test.xxxxx.ai/policy/recharge")

            buildConfigString("RTC_APP_ID", "689ade491323ae01797818e0-XXX-TODO")
        }
    }
}

dependencies {
    implementation(fileTree("dir" to "libs", "include" to listOf("*.jar", "*.aar")))
    coreLibraryDesugaring("com.android.tools:desugar_jdk_libs:2.1.5")

    coreLibraryDesugaring(Deps.desugar)
    implementation(libs.androidx.core.ktx)
    implementation(libs.androidx.appcompat)
    implementation(libs.material)
    implementation(libs.androidx.activity)
    implementation(libs.androidx.constraintlayout)
    testImplementation(libs.junit)
    androidTestImplementation(libs.androidx.junit)
    androidTestImplementation(libs.androidx.espresso.core)




    //dagger hilt
    implementation(Deps.hilt)
    kapt(Deps.hiltAndroidCompiler)
    kapt(Deps.hiltCompiler)

    //Retrofit2
    implementation(Deps.retrofit2)
    implementation(Deps.retrofitAdapterRxjava2)
    implementation(Deps.retrofit2ConverterGson)
    implementation(Deps.okhttp3LoggingInterceptor)


    // timber used for: recording logs
    implementation(Deps.timber)


    // tecent mmkv
    implementation(Deps.mmkv)
    implementation(Deps.material)

    // gson
    implementation(Deps.gson)

    // viewpager2
    implementation(Deps.viewpager2)

    // for multiple dex support
    implementation(Deps.multidex)


    //google firebase
    implementation(platform(Deps.firebaseBom))
    implementation(Deps.firebaseMessageKtx)
    implementation(Deps.firebaseAnalyticsKtx)
    implementation(Deps.firebaseCrashlyticsKtx)
    implementation(Deps.firebaseAuthKtx)
    implementation(Deps.credentials)
    implementation(Deps.credentialsAuth)
    implementation(Deps.googleId)


    // coroutine support
    implementation(Deps.kotlinCoroutinesCore)
    implementation(Deps.kotlinCoroutinesAndroid)

    implementation(Deps.appcompat)
    implementation(Deps.constraintlayout)
    implementation(Deps.flexbox)
    implementation(Deps.coreKtx)
    implementation(Deps.activityKtx)
    implementation(Deps.activityCompose)
    implementation(Deps.fragmentKtx)

    // vm and lifecycle
    implementation(Deps.viewModel)
    implementation(Deps.livedata)
    implementation(Deps.lifecycleJava8)
    implementation(Deps.lifecycleRuntime)
    implementation(Deps.datastore)


    //glide
    implementation(Deps.glide)
    kapt(Deps.glideCompiler)
    implementation(Deps.glideTransformations)
    implementation(Deps.glideWebpdecoder)

    // eventbus
    implementation(Deps.modularEventbus)
    kapt(Deps.modularEventbusCompiler)

    // indicator
    implementation(Deps.magicIndicator)

    // BlurView and Luban
    implementation(Deps.Luban)
    implementation(Deps.BlurView)

    //ali aRouter
    implementation(Deps.arouter)
    kapt(Deps.arouterCompiler)


    //Permission
    implementation(Deps.permission)
    // lottie
    implementation(Deps.lottie)
    // float window
    implementation(Deps.easyFloat)
    // guide
    implementation(Deps.newbieGuide)
    // refreshLayout
    implementation(Deps.refreshLayout)
    // apng
    implementation(Deps.apng)
    // softKey board
    implementation(Deps.skbGlobal)


    // baseRecyclerAdapter
    implementation(Deps.brvah)
    implementation(Deps.swipeMenuLayout)
    implementation(Deps.BRV)

    //banner
    implementation(Deps.banner)

    // spannable
    implementation(Deps.spannablex)

    // Media related libs
    implementation(Deps.mp3Recorder)
    implementation(Deps.photoView)
    implementation(Deps.transition)
    implementation(Deps.paging)
    implementation(Deps.exoplayer)
    implementation(Deps.subsamplingScaleImageView)

    //s3图片上传 oss
    implementation(Deps.awsS3)
    implementation(Deps.awsCore)

    // 网易 云信
    implementation(Deps.nimBase)
    implementation(Deps.nimPush)

    //内购 / 充值
    implementation(Deps.billing)

    // RTC : 实时通信
    implementation(Deps.BytePlusRTC)


    implementation(project(mapOf("path" to ":loadingstateview")))
    implementation(project(mapOf("path" to ":loadingstateview-ktx")))
    implementation(project(mapOf("path" to ":viewbinding-base")))
    implementation(project(mapOf("path" to ":viewbinding-nonreflection-ktx")))
}