package ru.dbotthepony.mc.otm.item.armor

import net.minecraft.client.model.HumanoidModel
import net.minecraft.sounds.SoundEvent
import net.minecraft.sounds.SoundEvents
import net.minecraft.world.damagesource.DamageSource
import net.minecraft.world.damagesource.DamageTypes
import net.minecraft.world.entity.Entity
import net.minecraft.world.entity.EquipmentSlot
import net.minecraft.world.entity.LivingEntity
import net.minecraft.world.item.ArmorItem
import net.minecraft.world.item.ArmorMaterial
import net.minecraft.world.item.ItemStack
import net.minecraft.world.item.Rarity
import net.minecraft.world.item.crafting.Ingredient
import net.minecraftforge.client.extensions.common.IClientItemExtensions
import net.minecraftforge.event.entity.living.LivingAttackEvent
import ru.dbotthepony.mc.otm.OverdriveThatMatters
import ru.dbotthepony.mc.otm.client.model.TritaniumArmorModel
import ru.dbotthepony.mc.otm.registry.MItemTags
import ru.dbotthepony.mc.otm.registry.MItems
import java.util.function.Consumer

private object TritaniumArmorMaterial : ArmorMaterial {
	override fun getDurabilityForType(p_40410_: ArmorItem.Type): Int {
		return when (p_40410_) {
			ArmorItem.Type.HELMET -> 520
			ArmorItem.Type.CHESTPLATE -> 920
			ArmorItem.Type.LEGGINGS -> 650
			ArmorItem.Type.BOOTS -> 540
		}
	}

	override fun getDefenseForType(p_40411_: ArmorItem.Type): Int {
		return when (p_40411_) {
			ArmorItem.Type.HELMET -> 4
			ArmorItem.Type.CHESTPLATE -> 9
			ArmorItem.Type.LEGGINGS -> 7
			ArmorItem.Type.BOOTS -> 3
		}
	}

	override fun getEnchantmentValue() = 9
	override fun getEquipSound(): SoundEvent = SoundEvents.ARMOR_EQUIP_IRON
	override fun getRepairIngredient(): Ingredient = Ingredient.of(MItemTags.REINFORCED_TRITANIUM_PLATES)

	const val ID = "${OverdriveThatMatters.MOD_ID}:tritanium_armor"

	override fun getName(): String = ID
	override fun getToughness() = 1f
	override fun getKnockbackResistance() = 0.08f
}

private object TritaniumArmorRenderProperties : IClientItemExtensions {
	override fun getHumanoidArmorModel(
		entityLiving: LivingEntity,
		itemStack: ItemStack,
		armorSlot: EquipmentSlot,
		_default: HumanoidModel<*>
	): HumanoidModel<*> {
		if (armorSlot === EquipmentSlot.LEGS) {
			return TritaniumArmorModel.getModelLegs()
		}

		return TritaniumArmorModel.getModel()
	}
}

class TritaniumArmorItem(slot: Type) : ArmorItem(TritaniumArmorMaterial, slot, Properties().stacksTo(1).rarity(Rarity.RARE)) {
	override fun initializeClient(consumer: Consumer<IClientItemExtensions>) {
		super.initializeClient(consumer)
		consumer.accept(TritaniumArmorRenderProperties)
	}

	override fun getArmorTexture(stack: ItemStack?, entity: Entity?, slot: EquipmentSlot?, type: String?): String {
		return TEXTURE_LOCATION
	}

	companion object {
		const val TEXTURE_LOCATION = "${OverdriveThatMatters.MOD_ID}:textures/models/armor/tritanium_armor.png"

		fun onHurt(event: LivingAttackEvent) {
			if (event.source.typeHolder().`is`(DamageTypes.SWEET_BERRY_BUSH) || event.source.msgId == "sweetBerryBush") {
				if (
					event.entity.getItemBySlot(EquipmentSlot.FEET).let { !it.isEmpty && it.item == MItems.TRITANIUM_BOOTS } &&
					event.entity.getItemBySlot(EquipmentSlot.LEGS).let { !it.isEmpty && it.item == MItems.TRITANIUM_PANTS }
				) {
					event.isCanceled = true
				}
			}
		}
	}
}
