package com.zivilon.cinder_loe.mixins;

import lotr.common.entity.animal.LOTREntityHorse;
import lotr.common.enchant.LOTREnchantment;
import lotr.common.enchant.LOTREnchantmentHelper;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import lotr.common.item.LOTRItemArmor;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(LOTREntityHorse.class)
public class MixinLOTREntityHorse {

    @Inject(method = "func_70658_aO", at = @At("RETURN"), cancellable = true, remap = false)
    private void cinderloe$boostArmorFromRider(CallbackInfoReturnable<Integer> cir) {
        LOTREntityHorse horse = (LOTREntityHorse)(Object)this;

        if (horse.riddenByEntity instanceof EntityPlayer) {
            EntityPlayer rider = (EntityPlayer) horse.riddenByEntity;

            int bonus = 0;
            for (ItemStack armor : rider.inventory.armorInventory) {
                if (armor != null && LOTREnchantmentHelper.hasEnchant(armor, LOTREnchantment.getEnchantmentByName("mountArmor"))) {
                    bonus++;
                }
            }

            if (bonus > 0) {
                int newArmorValue = cir.getReturnValue() + bonus;
                cir.setReturnValue(newArmorValue);
            }
        }
    }
}
