package com.zivilon.cinder_loe.items.specials;

import com.zivilon.cinder_loe.entity.corrupt.*;
import lotr.common.LOTRLevelData;
import lotr.common.entity.npc.*;
import lotr.common.fac.LOTRFaction;
import lotr.common.item.LOTRItemDagger;
import lotr.common.item.LOTRMaterial;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.IEntityLivingData;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.potion.Potion;
import net.minecraft.potion.PotionEffect;

public class VoidDagger extends LOTRItemDagger {
    public VoidDagger(LOTRMaterial material) {
        super(material);
    }
    private LOTRFaction getFaction(EntityPlayer player) {
        LOTRFaction faction = LOTRLevelData.getData(player).getPledgeFaction();
        if (faction == null) {
            faction = LOTRFaction.RUFFIAN;
        }
        return faction;
    }

    @Override
    public boolean hitEntity(ItemStack itemstack, EntityLivingBase target, EntityLivingBase attacker) {
        itemstack.damageItem(1, attacker);

        // Check if the target is dead and call the onKillEntity method
        if (target.getHealth() <= 0.0F) {
            onKillEntity(target, attacker);
        }

        return true;
    }
    public void onKillEntity(EntityLivingBase entityKilled, EntityLivingBase killer) {
            EntityPlayer player = (EntityPlayer) killer;
            LOTRFaction faction = getFaction(player);
            entityKilled.worldObj.playSoundAtEntity(player, "mob.zombie.unfect", 1F,1F);

            // God forgive me for a million If Statements instead of a Switch Case
            if (!entityKilled.worldObj.isRemote && entityKilled instanceof EntityPlayer) {
                CorruptMan entity = new CorruptMan(entityKilled.worldObj);
                entity.copyLocationAndAnglesFrom(entityKilled);

                for (int i = 0; i < 4; i++) {
                    ItemStack armor = entityKilled.getEquipmentInSlot(i + 1); // Armor slots
                    entity.setCurrentItemOrArmor(i + 1, armor);
                }

                ItemStack mainWeapon = ((LOTREntityMan) entityKilled).npcItemsInv.getMeleeWeapon(); // Main hand weapon
                entity.npcItemsInv.setIdleItem(mainWeapon);
                entity.npcItemsInv.setMeleeWeapon(mainWeapon);
                entity.setHealth(20);
                entity.addPotionEffect(new PotionEffect(Potion.regeneration.id, 9999, 0));
                entity.setCustomNameTag(((EntityPlayer) entityKilled).getDisplayName());

                entityKilled.worldObj.removeEntity(entityKilled);
                entity.onSpawnWithEgg((IEntityLivingData) null);

                entityKilled.worldObj.spawnEntityInWorld(entity);
                entityKilled.worldObj.playAuxSFXAtEntity(null, 1016, (int) entity.posX, (int) entity.posY, (int) entity.posZ, 0);

            }
            if (!entityKilled.worldObj.isRemote && entityKilled instanceof LOTREntityOrc) {
                CorruptOrc corruptOrc = new CorruptOrc(entityKilled.worldObj);
                corruptOrc.copyLocationAndAnglesFrom(entityKilled);

                for (int i = 0; i < 4; i++) {
                    ItemStack armor = entityKilled.getEquipmentInSlot(i + 1); // Armor slots
                    corruptOrc.setCurrentItemOrArmor(i + 1, armor);
                }

                ItemStack mainWeapon = ((LOTREntityOrc) entityKilled).npcItemsInv.getMeleeWeapon(); // Main hand weapon
                corruptOrc.npcItemsInv.setIdleItem(mainWeapon);
                corruptOrc.npcItemsInv.setMeleeWeapon(mainWeapon);
                corruptOrc.setHealth(10);
                corruptOrc.addPotionEffect(new PotionEffect(Potion.regeneration.id, 9999, 0));

                entityKilled.worldObj.removeEntity(entityKilled);
                corruptOrc.onSpawnWithEgg((IEntityLivingData) null);

                entityKilled.worldObj.spawnEntityInWorld(corruptOrc);
                entityKilled.worldObj.playAuxSFXAtEntity(null, 1016, (int) corruptOrc.posX, (int) corruptOrc.posY, (int) corruptOrc.posZ, 0);

            }
            if (!entityKilled.worldObj.isRemote && entityKilled instanceof LOTREntityMan && !(entityKilled instanceof LOTREntityHobbit)) {

                    CorruptMan entity = new CorruptMan(entityKilled.worldObj);
                    entity.copyLocationAndAnglesFrom(entityKilled);

                    for (int i = 0; i < 4; i++) {
                        ItemStack armor = entityKilled.getEquipmentInSlot(i + 1); // Armor slots
                        entity.setCurrentItemOrArmor(i + 1, armor);
                    }

                    ItemStack mainWeapon = ((LOTREntityMan) entityKilled).npcItemsInv.getMeleeWeapon(); // Main hand weapon
                    entity.npcItemsInv.setIdleItem(mainWeapon);
                    entity.npcItemsInv.setMeleeWeapon(mainWeapon);
                    entity.setHealth(10);
                    entity.addPotionEffect(new PotionEffect(Potion.regeneration.id, 9999, 0));

                    entityKilled.worldObj.removeEntity(entityKilled);
                    entity.onSpawnWithEgg((IEntityLivingData) null);

                    entityKilled.worldObj.spawnEntityInWorld(entity);
                    entityKilled.worldObj.playAuxSFXAtEntity(null, 1016, (int) entity.posX, (int) entity.posY, (int) entity.posZ, 0);
                }
            if (entityKilled instanceof LOTREntityHobbit) {
                CorruptHobbit entity = new CorruptHobbit(entityKilled.worldObj);
                entity.copyLocationAndAnglesFrom(entityKilled);

                for (int i = 0; i < 4; i++) {
                    ItemStack armor = entityKilled.getEquipmentInSlot(i + 1); // Armor slots
                    entity.setCurrentItemOrArmor(i + 1, armor);
                }

                ItemStack mainWeapon = ((LOTREntityHobbit) entityKilled).npcItemsInv.getMeleeWeapon(); // Main hand weapon
                entity.npcItemsInv.setIdleItem(mainWeapon);
                entity.npcItemsInv.setMeleeWeapon(mainWeapon);
                entity.setHealth(10);
                entity.addPotionEffect(new PotionEffect(Potion.regeneration.id, 9999, 0));

                entityKilled.worldObj.removeEntity(entityKilled);
                entity.onSpawnWithEgg((IEntityLivingData) null);

                entityKilled.worldObj.spawnEntityInWorld(entity);
                entityKilled.worldObj.playAuxSFXAtEntity(null, 1016, (int) entity.posX, (int) entity.posY, (int) entity.posZ, 0);
            }
            if (!entityKilled.worldObj.isRemote && entityKilled instanceof LOTREntityEnt) {
                CorruptEnt entity = new CorruptEnt(entityKilled.worldObj);
                entity.copyLocationAndAnglesFrom(entityKilled);

                entityKilled.worldObj.removeEntity(entityKilled);
                entity.onSpawnWithEgg((IEntityLivingData) null);
                entity.setHealth(10);
                entity.addPotionEffect(new PotionEffect(Potion.regeneration.id, 9999, 0));

                entityKilled.worldObj.spawnEntityInWorld(entity);
                entityKilled.worldObj.playAuxSFXAtEntity(null, 1016, (int) entity.posX, (int) entity.posY, (int) entity.posZ, 0);
            }
            if (!entityKilled.worldObj.isRemote && entityKilled instanceof LOTREntityElf) {
                CorruptElf entity = new CorruptElf(entityKilled.worldObj);
                entity.copyLocationAndAnglesFrom(entityKilled);

                for (int i = 0; i < 4; i++) {
                    ItemStack armor = entityKilled.getEquipmentInSlot(i + 1); // Armor slots
                    entity.setCurrentItemOrArmor(i + 1, armor);
                }
                entity.setHealth(10);
                entity.addPotionEffect(new PotionEffect(Potion.regeneration.id, 9999, 0));

                ItemStack mainWeapon = ((LOTREntityElf) entityKilled).npcItemsInv.getMeleeWeapon(); // Main hand weapon
                entity.npcItemsInv.setIdleItem(mainWeapon);
                entity.npcItemsInv.setMeleeWeapon(mainWeapon);

                entityKilled.worldObj.removeEntity(entityKilled);
                entity.onSpawnWithEgg((IEntityLivingData) null);

                entityKilled.worldObj.spawnEntityInWorld(entity);
                entityKilled.worldObj.playAuxSFXAtEntity(null, 1016, (int) entity.posX, (int) entity.posY, (int) entity.posZ, 0);
            }
            if (!entityKilled.worldObj.isRemote && entityKilled instanceof LOTREntityDwarf) {
                CorruptDwarf entity = new CorruptDwarf(entityKilled.worldObj);
                entity.copyLocationAndAnglesFrom(entityKilled);


                for (int i = 0; i < 4; i++) {
                    ItemStack armor = entityKilled.getEquipmentInSlot(i + 1); // Armor slots
                    entity.setCurrentItemOrArmor(i + 1, armor);
                }

                ItemStack mainWeapon = ((LOTREntityDwarf) entityKilled).npcItemsInv.getMeleeWeapon(); // Main hand weapon
                entity.npcItemsInv.setIdleItem(mainWeapon);
                entity.npcItemsInv.setMeleeWeapon(mainWeapon);
                entity.setHealth(10);
                entity.addPotionEffect(new PotionEffect(Potion.regeneration.id, 9999, 0));

                entityKilled.worldObj.removeEntity(entityKilled);
                entity.onSpawnWithEgg((IEntityLivingData) null);

                entityKilled.worldObj.spawnEntityInWorld(entity);
                entityKilled.worldObj.playAuxSFXAtEntity(null, 1016, (int) entity.posX, (int) entity.posY, (int) entity.posZ, 0);
            }
    }
}
