package com.zivilon.cinder_loe.entity;

import com.zivilon.cinder_loe.CinderLoE;
import com.zivilon.cinder_loe.mixins.MixinLOTRTradeEntries;

import lotr.common.entity.ai.LOTREntityAIEat;
import lotr.common.entity.npc.LOTREntityMan;
import lotr.common.entity.npc.LOTRTradeEntries;
import lotr.common.entity.npc.LOTRTradeEntries.TradeType;
import lotr.common.entity.npc.LOTRTradeable;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.world.World;
import lotr.common.LOTRFoods;
import lotr.common.LOTRMod;
import lotr.common.fac.LOTRFaction;
import net.minecraft.entity.IEntityLivingData;
import net.minecraft.item.ItemStack;
import lotr.common.entity.npc.LOTRTradeEntry;
import net.minecraft.entity.ai.EntityAIBase;
import net.minecraft.init.Items;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.ai.EntityAIBase;
import net.minecraft.entity.ai.EntityAIPanic;
import net.minecraft.entity.ai.EntityAIWander;
import net.minecraft.entity.ai.EntityAIAvoidEntity;
import lotr.common.entity.ai.LOTREntityAIEat;
import lotr.common.entity.ai.LOTREntityAIDrink;
import net.minecraft.entity.ai.EntityAIWatchClosest2;
import net.minecraft.entity.ai.EntityAIWatchClosest;
import net.minecraft.entity.ai.EntityAILookIdle;
import lotr.common.entity.npc.LOTREntityNPC;
import net.minecraft.entity.SharedMonsterAttributes;
import net.minecraft.util.StatCollector;
import net.minecraft.nbt.NBTTagCompound;


public class UtumnoSlaveTrader extends LOTREntityMan implements LOTRTradeable.Smith {
    public static LOTRTradeEntries UTUMNO_SLAVE_BUY;
    public static LOTRTradeEntries UTUMNO_SLAVE_SELL;

    public boolean bound = true;

    public UtumnoSlaveTrader(World world) {
        super(world);
        setSize(0.6F, 1.8F);
        ((EntityLiving)this).tasks.addTask(1, (EntityAIBase)new EntityAIPanic(this, 1.6D));
        ((EntityLiving)this).tasks.addTask(2, (EntityAIBase)new EntityAIWander(this, 1.0D));
        ((EntityLiving)this).tasks.addTask(3, (EntityAIBase)new EntityAIAvoidEntity(this, EntityPlayer.class, 12.0F, 1.5D, 1.8D));
        ((EntityLiving)this).tasks.addTask(4, (EntityAIBase)new LOTREntityAIEat(this, LOTRFoods.NURN_SLAVE, 8000));
        ((EntityLiving)this).tasks.addTask(4, (EntityAIBase)new LOTREntityAIDrink(this, LOTRFoods.NURN_SLAVE_DRINK, 8000));
        ((EntityLiving)this).tasks.addTask(5, (EntityAIBase)new EntityAIWatchClosest2((EntityLiving)this, EntityPlayer.class, 10.0F, 0.1F));
        ((EntityLiving)this).tasks.addTask(5, (EntityAIBase)new EntityAIWatchClosest2((EntityLiving)this, LOTREntityNPC.class, 5.0F, 0.05F));
        ((EntityLiving)this).tasks.addTask(6, (EntityAIBase)new EntityAIWatchClosest((EntityLiving)this, EntityLiving.class, 8.0F, 0.02F));
        ((EntityLiving)this).tasks.addTask(7, (EntityAIBase)new EntityAILookIdle((EntityLiving)this));
        addTargetTasks(false);
    }

    public LOTRFaction getFaction() {
        if (bound) return LOTRFaction.UTUMNO;
        return LOTRFaction.UNALIGNED;
    }

    @Override
    public String getNPCFormattedName(String npcName, String entityName) {
        if (bound) entityName = "entity.cinder_loe.UtumnoSlaveTrader.bound.name";
        return StatCollector.translateToLocal(entityName);
    }

    public boolean interact(EntityPlayer entityplayer) {
        boolean flag = super.interact(entityplayer);
        if (bound) {
            bound = false;
            return false;
        }
        return flag;
    }

    protected void applyEntityAttributes() {
        super.applyEntityAttributes();
        getEntityAttribute(SharedMonsterAttributes.maxHealth).setBaseValue(20.0D);
        getEntityAttribute(SharedMonsterAttributes.movementSpeed).setBaseValue(0.2D);
    }

    public boolean canTradeWith(EntityPlayer entityplayer) {
        return !bound;
    }

    public IEntityLivingData onSpawnWithEgg(IEntityLivingData data) {
        data = super.onSpawnWithEgg(data);
        this.npcItemsInv.setMeleeWeapon(new ItemStack(LOTRMod.pouch));
        this.npcItemsInv.setIdleItem(this.npcItemsInv.getMeleeWeapon());
        return data;
    }

    public LOTRTradeEntries getBuyPool() {
        return UTUMNO_SLAVE_BUY;
    }

    public LOTRTradeEntries getSellPool() {
        return UTUMNO_SLAVE_SELL;
    }

    // Replace with correct speechbanks
    public String getSpeechBank(EntityPlayer entityplayer) {
        if (isFriendlyAndAligned(entityplayer)) {
            if (canTradeWith(entityplayer))
                return "utumno/slaveTrader/friendly";
            return "utumno/slaveTrader/neutral";
            }
        return "utumno/slaveTrader/hostile";
    }

    static {
        UTUMNO_SLAVE_BUY = new LOTRTradeEntries(TradeType.BUY,
            new LOTRTradeEntry[] {
                new LOTRTradeEntry(new ItemStack(Items.arrow), 1),
                new LOTRTradeEntry(new ItemStack(Items.iron_ingot), 6),
                new LOTRTradeEntry(new ItemStack(LOTRMod.orcSteel), 6),
                new LOTRTradeEntry(new ItemStack(LOTRMod.mugOrcDraught, 1, 4), 20),
                new LOTRTradeEntry(new ItemStack(LOTRMod.modTemplate), 20)
            }
        );
        ((MixinLOTRTradeEntries)UTUMNO_SLAVE_BUY).vessels(LOTRFoods.NURN_SLAVE_DRINK);

        UTUMNO_SLAVE_SELL = new LOTRTradeEntries(TradeType.SELL,
            new LOTRTradeEntry[] {
                new LOTRTradeEntry(new ItemStack(Items.bread), 3),
                new LOTRTradeEntry(new ItemStack(Items.iron_ingot), 5),
                new LOTRTradeEntry(new ItemStack(Items.gold_ingot), 17),
                new LOTRTradeEntry(new ItemStack(Items.stick, 4), 1),
                new LOTRTradeEntry(new ItemStack(LOTRMod.bronze), 5),
                new LOTRTradeEntry(new ItemStack(LOTRMod.orcSteel), 5),
                new LOTRTradeEntry(new ItemStack(LOTRMod.amber), 15),
                new LOTRTradeEntry(new ItemStack(LOTRMod.ruby), 15),
                new LOTRTradeEntry(new ItemStack(LOTRMod.amethyst), 12),
                new LOTRTradeEntry(new ItemStack(LOTRMod.mugWater), 5),
                new LOTRTradeEntry(new ItemStack(LOTRMod.wargBone), 3),
                new LOTRTradeEntry(new ItemStack(LOTRMod.orcBone), 3),
                new LOTRTradeEntry(new ItemStack(LOTRMod.trollBone), 4)
            }
        );
    }

    @Override
    public void onPlayerTrade(EntityPlayer entityplayer, LOTRTradeEntries.TradeType type, ItemStack itemstack) {
    }

    @Override
    public void attackEntityWithRangedAttack(EntityLivingBase p_82196_1_, float p_82196_2_) {
    }

    public void writeEntityToNBT(NBTTagCompound nbt) {
        super.writeEntityToNBT(nbt);
        nbt.setByte("Bound", (byte)(bound ? 1 : 0));
    }


    public void readEntityFromNBT(NBTTagCompound nbt) {
        super.readEntityFromNBT(nbt);
        bound = nbt.getByte("Bound") != 0;
    }

}
