import kau.Dependencies
import kau.Versions

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-parcelize'
apply plugin: 'com.github.triplet.play'
apply plugin: 'dagger.hilt.android.plugin'
apply plugin: 'com.mikepenz.aboutlibraries.plugin'

play {
    serviceAccountCredentials = file('../files/gplay-keys.json')
    track = 'beta'
}

android {
    compileSdkVersion Versions.targetSdk

    androidGitVersion {
        codeFormat = 'MMNNPPXX'
        format = '%tag%%.count%%-commit%'
    }

    defaultConfig {
        applicationId "ca.allanwang.kau.sample"
        minSdkVersion Versions.minSdk
        targetSdkVersion Versions.targetSdk
        versionName androidGitVersion.name()
        versionCode androidGitVersion.code()
        multiDexEnabled true
        testInstrumentationRunner "ca.allanwang.kau.sample.SampleTestRunner"
    }
    buildFeatures {
        viewBinding = true
    }

    def releaseSigning = file("../files/kau.properties")
    def hasSigning = releaseSigning.exists()

    signingConfigs {

        if (hasSigning) {
            def releaseProps = new Properties()
            releaseSigning.withInputStream { releaseProps.load(it) }

            release {
                storeFile file("../files/kau.keystore")
                storePassword releaseProps.getProperty('storePassword')
                keyAlias releaseProps.getProperty('keyAlias')
                keyPassword releaseProps.getProperty('keyPassword')
            }
            println("Added KAU release signing")
        } else {
            println("No KAU release signing found")
        }

        debug {
            storeFile file("../files/debug.keystore")
            storePassword "debugKey"
            keyAlias "debugKey"
            keyPassword "debugKey"
        }
    }

    buildTypes {
        debug {
            applicationIdSuffix ".debug"
            versionNameSuffix "-debug"
            signingConfig signingConfigs.debug
            resValue "string", "app_name", "KAU Debug"
        }

        release {
            minifyEnabled true
            if (hasSigning) signingConfig signingConfigs.release
            resValue "string", "app_name", "KAU"
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    lintOptions {
        abortOnError false
        checkReleaseBuilds false
    }

    packagingOptions {
        pickFirst 'META-INF/core_release.kotlin_module'
        pickFirst 'META-INF/library_release.kotlin_module'
        pickFirst 'META-INF/library-core_release.kotlin_module'
        exclude "**/module-info.class"
    }

    compileOptions {
        sourceCompatibility Versions.java
        targetCompatibility Versions.java
    }

    kotlinOptions {
        jvmTarget = Versions.java.toString()
    }

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
        test.java.srcDirs += 'src/test/kotlin'
        androidTest.java.srcDirs += 'src/androidTest/kotlin'
        main.res.srcDirs += 'src/main/res-public'
    }

    testOptions {
        unitTests {
            // Don't throw runtime exceptions for android calls that are not mocked
            returnDefaultValues = true

            // Always show the result of every unit test, even if it passes.
            all {
                testLogging {
                    events 'skipped', 'failed', 'standardOut', 'standardError'
                }
            }
        }
    }
}

dependencies {
    implementation project(':about')
    implementation project(':fastadapter')
    implementation project(':fastadapter-viewbinding')
    implementation project(':colorpicker')
    implementation project(':core')
    implementation project(':core-ui')
    implementation project(':kpref-activity')
    implementation project(':searchview')
    implementation project(':mediapicker')

    implementation Dependencies.hilt
    kapt Dependencies.hiltCompiler

    implementation Dependencies.materialDialog("input")

    testImplementation Dependencies.kotlinTest
    testImplementation Dependencies.junit
    testImplementation Dependencies.hiltTest
    kaptTest Dependencies.hiltCompiler

    androidTestImplementation Dependencies.kotlinTest
    androidTestImplementation Dependencies.espresso
    androidTestImplementation Dependencies.espresso("intents")
    androidTestImplementation Dependencies.espresso("contrib")
    androidTestImplementation Dependencies.testRules
    androidTestImplementation Dependencies.testRunner
    androidTestImplementation Dependencies.hiltTest
    kaptAndroidTest Dependencies.hiltCompiler
}
