import java.time.LocalDateTime

plugins {
	id 'com.android.application'
	id 'kotlin-android'
	id 'kotlin-kapt'
	id 'com.google.devtools.ksp'
	id 'kotlin-parcelize'
	id 'dagger.hilt.android.plugin'
	id 'androidx.room'
}

android {
	compileSdk = 35
	buildToolsVersion = '35.0.0'
	namespace = 'org.koitharu.kotatsu'

	defaultConfig {
		applicationId 'org.koitharu.kotatsu'
		minSdk = 21
		targetSdk = 35
		versionCode = 1004
		versionName = '8.0'
		generatedDensities = []
		testInstrumentationRunner 'org.koitharu.kotatsu.HiltTestRunner'
		ksp {
			arg('room.generateKotlin', 'true')
		}
		androidResources {
			generateLocaleConfig true
		}
	}
	buildTypes {
		debug {
			applicationIdSuffix = '.debug'
		}
		release {
			minifyEnabled true
			shrinkResources true
			proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
		}
		nightly {
			initWith release
			applicationIdSuffix = '.nightly'
		}
	}
	buildFeatures {
		viewBinding true
		buildConfig true
	}
	packagingOptions {
		resources {
			excludes += [
				'META-INF/README.md',
				'META-INF/NOTICE.md'
			]
		}
	}
	sourceSets {
		androidTest.assets.srcDirs += files("$projectDir/schemas".toString())
		main.java.srcDirs += 'src/main/kotlin/'
	}
	compileOptions {
		coreLibraryDesugaringEnabled true
		sourceCompatibility JavaVersion.VERSION_11
		targetCompatibility JavaVersion.VERSION_11
	}
	kotlinOptions {
		jvmTarget = JavaVersion.VERSION_11.toString()
		freeCompilerArgs += [
			'-opt-in=kotlin.ExperimentalStdlibApi',
			'-opt-in=kotlinx.coroutines.ExperimentalCoroutinesApi',
			'-opt-in=kotlinx.coroutines.ExperimentalForInheritanceCoroutinesApi',
			'-opt-in=kotlinx.coroutines.FlowPreview',
			'-opt-in=kotlin.contracts.ExperimentalContracts',
			'-opt-in=coil3.annotation.ExperimentalCoilApi',
			'-opt-in=coil3.annotation.InternalCoilApi',
		]
	}
	room {
		schemaDirectory "$projectDir/schemas"
	}
	lint {
		abortOnError true
		disable 'MissingTranslation', 'PrivateResource', 'SetJavaScriptEnabled', 'SimpleDateFormat'
	}
	testOptions {
		unitTests.includeAndroidResources true
		unitTests.returnDefaultValues false
		kotlinOptions {
			freeCompilerArgs += ['-opt-in=org.koitharu.kotatsu.parsers.InternalParsersApi']
		}
	}
	applicationVariants.configureEach { variant ->
		if (variant.name == 'nightly') {
			variant.outputs.each { output ->
				def now = LocalDateTime.now()
				output.versionCodeOverride = now.format("yyMMdd").toInteger()
				output.versionNameOverride = 'N' + now.format("yyyyMMdd")
			}
		}
	}
}
afterEvaluate {
	compileDebugKotlin {
		kotlinOptions {
			freeCompilerArgs += ['-opt-in=org.koitharu.kotatsu.parsers.InternalParsersApi']
		}
	}
}
dependencies {
	def parsersVersion = libs.versions.parsers.get()
	if (System.properties.containsKey('parsersVersionOverride')) {
		// usage:
		// -DparsersVersionOverride=$(curl -s https://api.github.com/repos/kotatsuapp/kotatsu-parsers/commits/master -H "Accept: application/vnd.github.sha" | cut -c -10)
		parsersVersion = System.getProperty('parsersVersionOverride')
	}
	//noinspection UseTomlInstead
	implementation("com.github.KotatsuApp:kotatsu-parsers:$parsersVersion") {
		exclude group: 'org.json', module: 'json'
	}

	coreLibraryDesugaring libs.desugar.jdk.libs
	implementation libs.kotlin.stdlib
	implementation libs.kotlinx.coroutines.android
	implementation libs.kotlinx.coroutines.guava

	implementation libs.androidx.appcompat
	implementation libs.androidx.core
	implementation libs.androidx.activity
	implementation libs.androidx.fragment
	implementation libs.androidx.transition
	implementation libs.androidx.collection
	implementation libs.lifecycle.viewmodel
	implementation libs.lifecycle.service
	implementation libs.lifecycle.process
	implementation libs.androidx.constraintlayout
	implementation libs.androidx.swiperefreshlayout
	implementation libs.androidx.recyclerview
	implementation libs.androidx.viewpager2
	implementation libs.androidx.preference
	implementation libs.androidx.biometric
	implementation libs.material
	implementation libs.androidx.lifecycle.common.java8
	implementation libs.androidx.webkit

	implementation libs.androidx.work.runtime
	implementation libs.guava

	implementation libs.androidx.room.runtime
	implementation libs.androidx.room.ktx
	ksp libs.androidx.room.compiler

	implementation libs.okhttp
	implementation libs.okhttp.tls
	implementation libs.okhttp.dnsoverhttps
	implementation libs.okio

	implementation libs.adapterdelegates
	implementation libs.adapterdelegates.viewbinding

	implementation libs.hilt.android
	kapt libs.hilt.compiler
	implementation libs.androidx.hilt.work
	kapt libs.androidx.hilt.compiler

	implementation libs.coil.core
	implementation libs.coil.network
	implementation libs.coil.gif
	implementation libs.coil.svg
	implementation libs.avif.decoder
	implementation libs.ssiv
	implementation libs.disk.lru.cache
	implementation libs.markwon

	implementation libs.acra.http
	implementation libs.acra.dialog

	implementation libs.conscrypt.android

	debugImplementation libs.leakcanary.android
	debugImplementation libs.workinspector

	testImplementation libs.junit
	testImplementation libs.json
	testImplementation libs.kotlinx.coroutines.test

	androidTestImplementation libs.androidx.runner
	androidTestImplementation libs.androidx.rules
	androidTestImplementation libs.androidx.test.core
	androidTestImplementation libs.androidx.junit

	androidTestImplementation libs.kotlinx.coroutines.test

	androidTestImplementation libs.androidx.room.testing
	androidTestImplementation libs.moshi.kotlin

	androidTestImplementation libs.hilt.android.testing
	kaptAndroidTest libs.hilt.android.compiler
}
