package ru.dbotthepony.mc.otm.item.weapon

import net.minecraft.world.effect.MobEffectInstance
import net.minecraft.world.effect.MobEffects
import net.minecraft.world.entity.EquipmentSlotGroup
import net.minecraft.world.entity.LivingEntity
import net.minecraft.world.entity.ai.attributes.AttributeModifier
import net.minecraft.world.entity.ai.attributes.Attributes
import net.minecraft.world.item.Item
import net.minecraft.world.item.ItemStack
import net.minecraft.world.item.SwordItem
import net.minecraft.world.item.Tiers
import net.minecraft.world.item.component.ItemAttributeModifiers
import ru.dbotthepony.mc.otm.registry.game.MItems

class WitheredSteelSwordItem(properties: Properties) : SwordItem(Tiers.IRON, properties) {
	private val attributes: ItemAttributeModifiers

	init {
		val builder = ItemAttributeModifiers.builder()
		builder.add(Attributes.ATTACK_DAMAGE, AttributeModifier(BASE_ATTACK_DAMAGE_ID, 4.5, AttributeModifier.Operation.ADD_VALUE), EquipmentSlotGroup.MAINHAND)
		builder.add(Attributes.ATTACK_SPEED, AttributeModifier(BASE_ATTACK_SPEED_ID, -2.4, AttributeModifier.Operation.ADD_VALUE), EquipmentSlotGroup.MAINHAND)

		attributes = builder.build()
	}

	override fun isEnchantable(stack: ItemStack): Boolean {
		return stack.count == 1
	}

	override fun getEnchantmentValue(stack: ItemStack): Int {
		return 10
	}

	override fun isValidRepairItem(stack: ItemStack, ingredient: ItemStack): Boolean {
		return ingredient.`is`(MItems.WITHERED_STEEL)
	}

	override fun hurtEnemy(stack: ItemStack, target: LivingEntity, attacker: LivingEntity): Boolean {
		val status = super.hurtEnemy(stack, target, attacker)
		if (status) target.addEffect(MobEffectInstance(MobEffects.WITHER, 100, 0), attacker)
		return status
	}

	override fun getDefaultAttributeModifiers(stack: ItemStack): ItemAttributeModifiers {
		return attributes
	}
}
