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

import net.minecraft.ChatFormatting
import net.minecraft.core.Direction
import net.minecraft.nbt.CompoundTag
import net.minecraft.network.chat.Component
import net.minecraft.world.item.Item
import net.minecraft.world.item.ItemStack
import net.minecraft.world.item.Rarity
import net.minecraft.world.item.TooltipFlag
import net.minecraft.world.level.Level
import net.minecraftforge.common.capabilities.Capability
import net.minecraftforge.common.capabilities.ICapabilityProvider
import net.minecraftforge.common.util.LazyOptional
import net.minecraftforge.registries.ForgeRegistries
import ru.dbotthepony.mc.otm.capability.MatteryCapability
import ru.dbotthepony.mc.otm.capability.matter.IPatternState
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.core.TranslatableComponent
import ru.dbotthepony.mc.otm.core.getID
import ru.dbotthepony.mc.otm.matter.MatterManager
import java.util.*
import java.util.stream.Stream

class CreativePatternItem : Item(Properties().rarity(Rarity.EPIC).stacksTo(1)) {
	private object Patterns : IPatternStorage, ICapabilityProvider {
		private val resolver = LazyOptional.of<IPatternStorage> { this }

		override val patterns: Stream<out IPatternState>
			get() = MatterManager.map.keys.stream().map { PatternState(UUID(34783464838L, 4463458382L + ForgeRegistries.ITEMS.getID(it)), it, 1.0) }

		override val patternCapacity: Int
			get() {
				return MatterManager.map.size
			}

		override val storedPatterns: Int
			get() {
				return MatterManager.map.size
			}

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

		override fun <T> getCapability(cap: Capability<T>, side: Direction?): LazyOptional<T> {
			return if (cap == MatteryCapability.PATTERN) resolver.cast() else LazyOptional.empty()
		}
	}

	override fun initCapabilities(stack: ItemStack?, nbt: CompoundTag?): ICapabilityProvider {
		return Patterns
	}

	override fun appendHoverText(
		p_41421_: ItemStack,
		p_41422_: Level?,
		p_41423_: MutableList<Component>,
		p_41424_: TooltipFlag
	) {
		super.appendHoverText(p_41421_, p_41422_, p_41423_, p_41424_)
		p_41423_.add(TranslatableComponent("$descriptionId.description1").withStyle(ChatFormatting.DARK_GRAY))
		p_41423_.add(TranslatableComponent("$descriptionId.description2").withStyle(ChatFormatting.DARK_GRAY))
	}
}
