import dev.kikugie.stonecutter.build.StonecutterBuildExtension
import dev.kikugie.stonecutter.controller.StonecutterControllerExtension
import org.gradle.api.Project
import org.gradle.kotlin.dsl.*

val Project.mod: ModData get() = ModData(this)
fun Project.prop(key: String): String? = findProperty(key)?.toString()


val Project.stonecutterBuild get() = extensions.getByType<StonecutterBuildExtension>()
val Project.stonecutterController get() = extensions.getByType<StonecutterControllerExtension>()

val Project.common
	get() = requireNotNull(stonecutterBuild.node.sibling("common")) {
		"No common project for $project"
	}
val Project.commonProject get() = rootProject.project(stonecutterBuild.current.project)
val Project.commonMod get() = commonProject.mod

val Project.loader: String? get() = prop("loader")

@JvmInline
value class ModData(private val project: Project) {
	val id: String get() = modProp("id")
	val name: String get() = modProp("name")
	val version: String get() = modProp("version")
	val group: String get() = modProp("group")
	val author: String get() = modProp("author")
	val description: String get() = modProp("description")
	val license: String get() = modProp("license")
	val credits: String get() = modProp("credits")
	val issueUrl: String get() = modPropOrNull("issue_url") ?: ""
	val homepageUrl: String get() = modPropOrNull("homepage_url") ?: ""
	val sourcesUrl: String get() = modPropOrNull("sources_url") ?: ""
	val minecraft_version: String get() = propOrNull("minecraft_version") ?: project.stonecutterBuild.current.version

	fun propOrNull(key: String) = project.prop(key)
	fun prop(key: String) = requireNotNull(propOrNull(key)) { "Missing '$key'" }
	fun modPropOrNull(key: String) = project.prop("mod.$key")
	fun modProp(key: String) = requireNotNull(modPropOrNull(key)) { "Missing 'mod.$key'" }
	fun modrinth(name: String, version: String) = "maven.modrinth:$name:$version"
}