package ru.dbotthepony.mc.otm.item

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.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 ru.dbotthepony.mc.otm.capability.IMatteryUpgrade
import ru.dbotthepony.mc.otm.capability.MatteryCapability
import ru.dbotthepony.mc.otm.capability.UpgradeType
import ru.dbotthepony.mc.otm.capability.addUpgradeTooltipLines
import ru.dbotthepony.mc.otm.core.TextComponent
import ru.dbotthepony.mc.otm.core.math.Decimal

class SimpleUpgrade(
	properties: Properties,
	override val upgradeTypes: Set<UpgradeType>,
	override val speedBonus: Double = 0.0,
	override val processingItems: Int = 0,
	override val energyStorageFlat: Decimal = Decimal.ZERO,
	override val energyStorage: Decimal = Decimal.ZERO,
	override val energyConsumed: Decimal = Decimal.ZERO,
	override val energyThroughputFlat: Decimal = Decimal.ZERO,
	override val energyThroughput: Decimal = Decimal.ZERO,
	override val matterStorage: Decimal = Decimal.ZERO,
	override val matterStorageFlat: Decimal = Decimal.ZERO,
	override val failureMultiplier: Double = 1.0,
) : Item(properties), IMatteryUpgrade, ICapabilityProvider {
	private val resolver = LazyOptional.of { this }

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

		return LazyOptional.empty()
	}

	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_)
		addUpgradeTooltipLines(p_41423_)
	}

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

