package com.zivilon.cinder_loe.mixins;

import com.zivilon.cinder_loe.CinderLoE;
import lotr.common.LOTRMod;
import lotr.common.entity.npc.LOTREntityNPCRideable;
import lotr.common.entity.npc.LOTREntityWarg;
import net.minecraft.entity.Entity;
import net.minecraft.entity.SharedMonsterAttributes;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.MathHelper;
import net.minecraft.world.World;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import org.spongepowered.asm.mixin.Shadow;
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 java.util.Random;


@Mixin(LOTREntityWarg.class)
public abstract class MixinLOTREntityWarg extends LOTREntityNPCRideable {

    public MixinLOTREntityWarg(World worldIn) {
        super(worldIn);
    }

    /**
     * @author KeyLime17
     * @reason Mevans
     */

    @Shadow(remap = false)
    public abstract LOTREntityWarg.WargType getWargType();

    @Overwrite(remap = false)
    protected void func_110147_ax() { // applyEntityAttributes()
        super.applyEntityAttributes();
        this.getEntityAttribute(SharedMonsterAttributes.maxHealth).setBaseValue((double) MathHelper.getRandomIntegerInRange((Random)this.rand, (int)20, (int)40));
        this.getEntityAttribute(SharedMonsterAttributes.followRange).setBaseValue(32.0);
        this.getEntityAttribute(SharedMonsterAttributes.movementSpeed).setBaseValue(0.22);
        this.getEntityAttribute(npcAttackDamage).setBaseValue((double)MathHelper.getRandomIntegerInRange((Random)this.rand, (int)3, (int)5));
    }

    @Overwrite(remap = false)
    protected void func_70628_a(boolean flag, int i) {
        Item furItem = null;
        int furMeta = 0;
        switch(getWargType().wargID) {
            case 0:
                furItem = LOTRMod.fur;
                break;
            case 1:
                furItem = CinderLoE.cinderFurItem;
                break;
            case 2:
                furItem = CinderLoE.cinderFurItem;
                furMeta = 1;
                break;
            case 3:
                furItem = CinderLoE.cinderFurItem;
                furMeta = 2;
                break;
            case 4:
                furItem = CinderLoE.cinderFurItem;
                furMeta = 0; // Needs ice fur to be added
                break;
            case 5:
                furItem = CinderLoE.cinderFurItem;
                furMeta = 3;
                break;
            case 6:
                furItem = CinderLoE.cinderFurItem;
                furMeta = 0; // Needs fire fur to be added
                break;
        }

        int furs = 1 + this.rand.nextInt(3) + this.rand.nextInt(i + 1);
        for (int l = 0; l < furs; l++)
            entityDropItem(new ItemStack(furItem, 1, furMeta), 0.0F);
        int bones = 2 + this.rand.nextInt(2) + this.rand.nextInt(i + 1);
        for (int j = 0; j < bones; j++)
            dropItem(LOTRMod.wargBone, 1);
        if (flag) {
            int rugChance = 50 - i * 8;
            rugChance = Math.max(rugChance, 1);
            if (this.rand.nextInt(rugChance) == 0)
                entityDropItem(new ItemStack(LOTRMod.wargskinRug, 1, (getWargType()).wargID), 0.0F);
        }
    }
    @Inject(method = "func_70658_aO", at = @At("RETURN"), cancellable = true, remap = false)
    private void cinderloe$boostArmorFromRider(CallbackInfoReturnable<Integer> cir) {
        if (this.riddenByEntity instanceof EntityPlayer) {
            EntityPlayer rider = (EntityPlayer) this.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);
            }
        }
    }
}
