package ru.dbotthepony.mc.otm.datagen.advancements

import net.minecraft.advancements.Advancement
import net.minecraft.advancements.CriterionTriggerInstance
import net.minecraft.advancements.DisplayInfo
import net.minecraft.advancements.FrameType
import net.minecraft.advancements.critereon.ContextAwarePredicate
import net.minecraft.advancements.critereon.EntityPredicate
import net.minecraft.advancements.critereon.InventoryChangeTrigger
import net.minecraft.advancements.critereon.ItemPredicate
import net.minecraft.network.chat.Component
import net.minecraft.resources.ResourceLocation
import net.minecraft.tags.TagKey
import net.minecraft.world.item.Item
import net.minecraft.world.item.ItemStack
import net.minecraft.world.level.ItemLike
import ru.dbotthepony.mc.otm.core.TextComponent

fun AdvancementBuilder(): Advancement.Builder = Advancement.Builder.advancement()

fun DisplayInfo(
	itemStack: ItemStack,
	title: Component = TextComponent("undefined"),
	description: Component = TextComponent("undefined"),
	background: ResourceLocation? = null,
	frameType: FrameType = FrameType.TASK,
	showToast: Boolean = true,
	announceChat: Boolean = true,
	hidden: Boolean = false,
): DisplayInfo {
	return DisplayInfo(
		itemStack,
		title,
		description,
		background,
		frameType,
		showToast,
		announceChat,
		hidden
	)
}

// compiler thinks it is shadowed - it is not
@Suppress("EXTENSION_SHADOWED_BY_MEMBER")
fun Advancement.Builder.display(
	itemStack: ItemStack,
	title: Component = TextComponent("undefined"),
	description: Component = TextComponent("undefined"),
	background: ResourceLocation? = null,
	frameType: FrameType = FrameType.TASK,
	showToast: Boolean = true,
	announceChat: Boolean = true,
	hidden: Boolean = false,
) = display(itemStack, title, description, background, frameType, showToast, announceChat, hidden)

fun predicate(tag: TagKey<Item>): ItemPredicate {
	return ItemPredicate.Builder.item().of(tag).build()
}

fun criterion(tag: TagKey<Item>): CriterionTriggerInstance {
	return InventoryChangeTrigger.TriggerInstance.hasItems(predicate(tag))
}

fun predicate(item: ItemLike): ItemPredicate {
	return ItemPredicate.Builder.item().of(item).build()
}

fun criterion(item: ItemLike): CriterionTriggerInstance {
	return InventoryChangeTrigger.TriggerInstance.hasItems(predicate(item))
}

fun EntityPredicate.wrap(): ContextAwarePredicate = EntityPredicate.wrap(this)
