package ru.dbotthepony.mc.otm.item.matter

import com.google.common.collect.ImmutableList
import net.minecraft.network.chat.Component
import net.minecraft.util.RandomSource
import net.minecraft.world.item.Item
import net.minecraft.world.item.ItemStack
import net.minecraft.world.item.Items
import net.minecraft.world.item.TooltipFlag
import net.minecraft.world.level.storage.loot.functions.LootItemConditionalFunction
import net.minecraft.world.level.storage.loot.functions.SetComponentsFunction
import net.neoforged.neoforge.capabilities.RegisterCapabilitiesEvent
import ru.dbotthepony.mc.otm.capability.MatteryCapability
import ru.dbotthepony.mc.otm.capability.matter.IPatternStorage
import ru.dbotthepony.mc.otm.capability.matter.PatternInsertFailure
import ru.dbotthepony.mc.otm.capability.matter.PatternInsertStatus
import ru.dbotthepony.mc.otm.capability.matter.PatternState
import ru.dbotthepony.mc.otm.capability.matter.gatherTooltip
import ru.dbotthepony.mc.otm.core.TextComponent
import ru.dbotthepony.mc.otm.core.TranslatableComponent
import ru.dbotthepony.mc.otm.core.stream
import ru.dbotthepony.mc.otm.item.MatteryItem
import ru.dbotthepony.mc.otm.registry.CapabilitiesRegisterListener
import ru.dbotthepony.mc.otm.registry.game.MDataComponentTypes
import java.util.*
import java.util.stream.Stream

class GoldDiskItem(parameters: Properties) : MatteryItem(parameters), CapabilitiesRegisterListener {
	override fun registerCapabilities(event: RegisterCapabilitiesEvent) {
		event.registerItem(MatteryCapability.PATTERN_ITEM, { o, _ -> ItemPatternStorageCapability(o) }, this)
	}

	override fun getName(stack: ItemStack): Component {
		val data = stack[MDataComponentTypes.PATTERNS]

		if (data == null || data.size > 1) {
			return super.getName(stack)
		} else if (data.size == 0 || data[0].item == Items.AIR) {
			return TranslatableComponent("$descriptionId.single_item", TextComponent("???"))
		} else {
			return TranslatableComponent("$descriptionId.single_item", data[0].item.getName(ItemStack(data[0].item)))
		}
	}

	override fun appendHoverText(
		itemStack: ItemStack,
		context: TooltipContext,
		components: MutableList<Component>,
		tooltipType: TooltipFlag
	) {
		itemStack.getCapability(MatteryCapability.PATTERN_ITEM)?.gatherTooltip(context, components, tooltipType)

		super.appendHoverText(itemStack, context, components, tooltipType)
	}

	class ItemPatternStorageCapability(val stack: ItemStack) : IPatternStorage {
		override val patternCapacity: Int get() {
			return stack[MDataComponentTypes.PATTERNS]?.size ?: 0
		}

		override val storedPatterns: Int get() {
			return patternCapacity
		}

		override val patterns: Stream<PatternState> get() {
			return stack[MDataComponentTypes.PATTERNS]?.stream() ?: Stream.empty()
		}

		override fun insertPattern(pattern: PatternState, onlyUpdate: Boolean, simulate: Boolean): PatternInsertStatus {
			return PatternInsertFailure
		}
	}

	companion object {
		fun patterns(random: RandomSource, vararg items: Item): LootItemConditionalFunction.Builder<*> {
			return SetComponentsFunction.setComponent(
				MDataComponentTypes.PATTERNS,
				items
					.stream()
					.map { PatternState(UUID(random.nextLong(), random.nextLong()), it, 1.0) }
					.collect(ImmutableList.toImmutableList()))
		}
	}
}
