package com.zivilon.cinder_loe.mixins.overrides;

import lotr.common.entity.npc.LOTREntityNPC;
import lotr.common.entity.npc.LOTRHiredNPCInfo;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.SharedMonsterAttributes;
import net.minecraft.entity.ai.attributes.IAttributeInstance;
import net.minecraft.entity.item.EntityFireworkRocket;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.util.ChatComponentTranslation;
import net.minecraft.util.IChatComponent;
import net.minecraft.world.World;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.Shadow;

import java.util.UUID;

@Mixin(LOTRHiredNPCInfo.class)
public class MixinLOTRHiredNPCInfo {
    @Shadow
    private LOTREntityNPC theEntity;
    @Shadow
    private UUID hiringPlayerUUID;
    @Shadow
    public int xpLevel = 1;

    @Overwrite(remap = false)
    private void onLevelUp() {
        EntityPlayer hirer;
        if (this.theEntity.getMaxHealth() >= 60) {
            this.addLevelUpHealthGain((EntityLivingBase) this.theEntity);
        }
        Entity mount = this.theEntity.ridingEntity;
        if (mount instanceof EntityLivingBase && !(mount instanceof LOTREntityNPC)) {
            this.addLevelUpHealthGain((EntityLivingBase)mount);
        }
        if ((hirer = this.getHiringPlayer()) != null) {
            hirer.addChatMessage((IChatComponent)new ChatComponentTranslation("lotr.hiredNPC.levelUp", new Object[]{this.theEntity.getCommandSenderName(), this.xpLevel}));
        }
        this.spawnLevelUpFireworks();
    }

    @Overwrite(remap = false)
    private void addLevelUpHealthGain(EntityLivingBase gainingEntity) {
        float healthBoost = 1.0f;
        IAttributeInstance attrHealth = gainingEntity.getEntityAttribute(SharedMonsterAttributes.maxHealth);
        attrHealth.setBaseValue(attrHealth.getBaseValue() + (double)healthBoost);
        gainingEntity.heal(healthBoost);
    }

    @Overwrite(remap = false)
    public EntityPlayer getHiringPlayer() {
        if (this.hiringPlayerUUID == null) {
            return null;
        }
        return this.theEntity.worldObj.func_152378_a(this.hiringPlayerUUID);
    }

    private void spawnLevelUpFireworks() {
        boolean bigLvlUp = this.xpLevel % 5 == 0;
        World world = this.theEntity.worldObj;
        ItemStack itemstack = new ItemStack(Items.fireworks);
        NBTTagCompound itemData = new NBTTagCompound();
        NBTTagCompound fireworkData = new NBTTagCompound();
        NBTTagList explosionsList = new NBTTagList();
        int explosions = 1;
        for (int l = 0; l < explosions; ++l) {
            NBTTagCompound explosionData = new NBTTagCompound();
            explosionData.setBoolean("Flicker", true);
            explosionData.setBoolean("Trail", bigLvlUp);
            int[] colors = new int[]{0xFF5500, this.theEntity.getFaction().getFactionColor()};
            explosionData.setIntArray("Colors", colors);
            boolean effectType = bigLvlUp;
            explosionData.setByte("Type", (byte)(effectType ? 1 : 0));
            explosionsList.appendTag((NBTBase)explosionData);
        }
        fireworkData.setTag("Explosions", (NBTBase)explosionsList);
        itemData.setTag("Fireworks", (NBTBase)fireworkData);
        itemstack.setTagCompound(itemData);
        EntityFireworkRocket firework = new EntityFireworkRocket(world, this.theEntity.posX, this.theEntity.boundingBox.minY + (double)this.theEntity.height, this.theEntity.posZ, itemstack);
        NBTTagCompound fireworkNBT = new NBTTagCompound();
        firework.writeEntityToNBT(fireworkNBT);
        fireworkNBT.setInteger("LifeTime", bigLvlUp ? 20 : 15);
        firework.readEntityFromNBT(fireworkNBT);
        world.spawnEntityInWorld((Entity)firework);
    }

}
