plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id 'kotlin-kapt'
    id 'dagger.hilt.android.plugin'
    id 'com.google.gms.google-services'
    id 'com.google.firebase.crashlytics'
}
apply plugin: 'kotlin-android'

android {
    compileSdk target_sdk

    defaultConfig {
        applicationId "com.habitrpg.android.habitica"
        minSdk 26
        targetSdk target_sdk
        versionCode app_version_code + 1
        versionName app_version_name
        buildConfigField "String", "TESTING_LEVEL", "\"production\""

        def formattedDate = new Date().format('yyMMdd')
        archivesBaseName = "Habitica-WearOS-${formattedDate}${versionCode}"
    }

    signingConfigs {
        release
    }

    buildTypes {
        debug {
            minifyEnabled false
            debuggable true
            applicationIdSuffix ".debug"
            ext.enableCrashlytics = false
            ext.alwaysUpdateBuildId = false
            resValue "string", "app_name", "Habitica Debug"
        }
        release {
            signingConfig signingConfigs.release
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            resValue "string", "app_name", "Habitica"
        }
    }

    flavorDimensions "buildType"

    productFlavors {
        dev {
            dimension "buildType"
        }

        staff {
            dimension "buildType"
            buildConfigField "String", "TESTING_LEVEL", "\"staff\""
            resValue "string", "app_name", "Habitica Staff"
            versionCode app_version_code + 9
        }

        partners {
            dimension "buildType"
            buildConfigField "String", "TESTING_LEVEL", "\"partners\""
            resValue "string", "app_name", "Habitica"
            versionCode app_version_code + 7
        }

        alpha {
            dimension "buildType"
            buildConfigField "String", "TESTING_LEVEL", "\"alpha\""
            resValue "string", "app_name", "Habitica Alpha"
            versionCode app_version_code + 5
        }

        beta {
            buildConfigField "String", "TESTING_LEVEL", "\"beta\""
            dimension "buildType"
            versionCode app_version_code + 3
        }

        prod {
            buildConfigField "String", "TESTING_LEVEL", "\"production\""
            dimension "buildType"
            versionCode app_version_code + 1
        }
    }

    buildFeatures {
        viewBinding true
    }
    namespace 'com.habitrpg.android.habitica'
}

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

    implementation "androidx.core:core-ktx:$core_ktx_version"
    implementation "com.google.android.gms:play-services-wearable:$play_wearables_version"
    implementation "androidx.recyclerview:recyclerview:$recyclerview_version"
    implementation 'androidx.wear:wear:1.2.0'
    implementation "androidx.wear:wear-input:1.1.0"

    //Networking
    implementation "com.squareup.okhttp3:okhttp:$okhttp_version"
    implementation "com.squareup.okhttp3:logging-interceptor:$okhttp_version"

    //REST API handling
    implementation("com.squareup.retrofit2:retrofit:$retrofit_version") {
        exclude module: 'okhttp'
    }
    implementation("com.squareup.retrofit2:converter-moshi:$retrofit_version")
    implementation("com.squareup.moshi:moshi-kotlin:$moshi_version")
    implementation 'androidx.coordinatorlayout:coordinatorlayout:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    kapt("com.squareup.moshi:moshi-kotlin-codegen:$moshi_version")

    implementation platform("com.google.firebase:firebase-bom:$firebase_bom")
    implementation 'com.google.firebase:firebase-crashlytics-ktx'
    implementation 'com.google.firebase:firebase-analytics-ktx'

    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
    implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
    implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version"
    implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version"
    implementation "androidx.preference:preference-ktx:$preferences_version"
    implementation "androidx.navigation:navigation-fragment-ktx:2.5.3"

    implementation "com.google.android.gms:play-services-auth:$play_auth_version"

    implementation project(':common')
    implementation project(':shared')
    implementation "androidx.appcompat:appcompat:$appcompat_version"

    implementation "com.google.dagger:hilt-android:$daggerhilt_version"
    kapt "com.google.dagger:hilt-compiler:$daggerhilt_version"
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
}
repositories {
    mavenCentral()
}

final File HRPG_PROPS_FILE = new File(projectDir.absolutePath + '/../habitica.properties')
if (HRPG_PROPS_FILE.canRead()) {
    Properties HRPG_PROPS = new Properties()
    HRPG_PROPS.load(new FileInputStream(HRPG_PROPS_FILE))

    if (HRPG_PROPS != null) {
        android.buildTypes.all { buildType ->
            HRPG_PROPS.any { property ->
                buildType.buildConfigField "String", property.key, "\"${property.value}\""
            }
        }
    } else {
        throw new MissingResourceException('habitica.properties found but some entries are missing')
    }
} else {
    throw new MissingResourceException('habitica.properties not found')
}

Properties props = new Properties()
def propFile = new File('signingrelease.properties')
if (propFile.canRead()) {
    props.load(new FileInputStream(propFile))

    if (props != null && props.containsKey('STORE_FILE') && props.containsKey('STORE_PASSWORD') &&
            props.containsKey('KEY_ALIAS') && props.containsKey('KEY_PASSWORD')) {
        android.signingConfigs.release.storeFile = file(props['STORE_FILE'])
        android.signingConfigs.release.storePassword = props['STORE_PASSWORD']
        android.signingConfigs.release.keyAlias = props['KEY_ALIAS']
        android.signingConfigs.release.keyPassword = props['KEY_PASSWORD']
    } else {
        println 'signing.properties found but some entries are missing'
        android.buildTypes.release.signingConfig = null
    }
} else {
    println 'signing.properties not found'
    android.buildTypes.release.signingConfig = null
}