package com.zivilon.cinder_loe.entity.npc.good_human;

import com.zivilon.cinder_loe.CinderLoE;

import lotr.common.*;
import lotr.common.entity.ai.*;
import lotr.common.entity.animal.LOTREntityHorse;
import lotr.common.entity.npc.*;
import lotr.common.fac.LOTRFaction;
import lotr.common.quest.LOTRMiniQuest;
import lotr.common.quest.LOTRMiniQuestFactory;
import lotr.common.world.structure.LOTRChestContents;
import net.minecraft.entity.*;
import net.minecraft.entity.ai.*;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.world.World;

public class BreeOutrider extends LOTREntityBreeGuard {
    private EntityAIBase rangedAttackAI = this.createBreeRangedAttackAI();
    private EntityAIBase meleeAttackAI = this.addBreeAttackAI();
    private static ItemStack[] guardWeapons = new ItemStack[]{new ItemStack(CinderLoE.swordBree), new ItemStack(CinderLoE.swordBree), new ItemStack(LOTRMod.pikeIron)};
    public BreeOutrider(World world) {
        super(world);
        this.setSize(0.6f, 1.8f);
        this.getNavigator().setAvoidsWater(true);
        this.getNavigator().setBreakDoors(true);
        ((EntityLiving)this).tasks.addTask(0, new EntityAISwimming((EntityLiving)this));
        ((EntityLiving)this).tasks.addTask(1, new LOTREntityAIHiredRemainStill(this));
        ((EntityLiving)this).tasks.addTask(2, this.addBreeAttackAI());
        ((EntityLiving)this).tasks.addTask(2, this.createBreeRangedAttackAI());
        ((EntityLiving)this).tasks.addTask(3, new LOTREntityAIFollowHiringPlayer(this));
        ((EntityLiving)this).tasks.addTask(4, new EntityAIOpenDoor((EntityLiving)this, true));
        ((EntityLiving)this).tasks.addTask(5, new EntityAIWander((EntityCreature)this, 1.0));
        ((EntityLiving)this).tasks.addTask(6, new LOTREntityAIEat(this, LOTRFoods.BREE, 8000));
        ((EntityLiving)this).tasks.addTask(6, new LOTREntityAIDrink(this, LOTRFoods.BREE_DRINK, 6000));
        ((EntityLiving)this).tasks.addTask(6, new LOTREntityAIHobbitSmoke(this, 12000));
        ((EntityLiving)this).tasks.addTask(7, new EntityAIWatchClosest2((EntityLiving)this, EntityPlayer.class, 10.0f, 0.02f));
        ((EntityLiving)this).tasks.addTask(7, (EntityAIBase)new EntityAIWatchClosest2((EntityLiving)this, LOTREntityNPC.class, 5.0f, 0.02f));
        ((EntityLiving)this).tasks.addTask(8, (EntityAIBase)new EntityAIWatchClosest((EntityLiving)this, EntityLiving.class, 8.0f, 0.02f));
        ((EntityLiving)this).tasks.addTask(9, (EntityAIBase)new EntityAILookIdle((EntityLiving)this));
        this.addTargetTasks(true);
    }
    protected EntityAIBase createBreeRangedAttackAI() {
        return new LOTREntityAIRangedAttack(this, 1.25, 30, 40, 16.0f);
    }
    protected EntityAIBase addBreeAttackAI() {
        return new LOTREntityAIAttackOnCollide(this, 1.5, true);

    }

    @Override
    protected void applyEntityAttributes() {
        super.applyEntityAttributes();
        this.getEntityAttribute(SharedMonsterAttributes.maxHealth).setBaseValue(20.0);
        this.getEntityAttribute(SharedMonsterAttributes.movementSpeed).setBaseValue(0.2);
    }
    @Override
    public LOTRFaction getFaction() {
        return LOTRFaction.BREE;
    }

    @Override
    public void setupNPCGender() {
        this.familyInfo.setMale((this).rand.nextBoolean());
    }

    @Override
    public void setupNPCName() {
        this.familyInfo.setName(LOTRNames.getBreeName((this).rand, this.familyInfo.isMale()));
    }
    @Override
    public String getNPCName() {
        return this.familyInfo.getName();
    }
    @Override
    protected void dropFewItems(boolean flag, int i) {
        super.dropFewItems(flag, i);
        int bones = (this).rand.nextInt(2) + (this).rand.nextInt(i + 1);
        for (int l = 0; l < bones; ++l) {
            this.dropItem(Items.bone, 1);
        }
        this.dropBreeItems(flag, i);
        dropNPCCrossbowBolts(i);
    }
    @Override
    protected LOTRAchievement getKillAchievement() {
        return LOTRAchievement.killBreelander;
    }

    protected void dropBreeItems(boolean flag, int i) {
        if ((this).rand.nextInt(6) == 0) {
            this.dropChestContents(LOTRChestContents.BREE_HOUSE, 1, 2 + i);
        }
    }


    @Override
    protected void entityInit() {
        super.entityInit();
        ((Entity) this).getDataWatcher().addObject(22, (Object)0);
        ((Entity) this).getDataWatcher().addObject(23, (Object)0);
    }

    @Override
    public IEntityLivingData onSpawnWithEgg(IEntityLivingData data) {
        data = super.onSpawnWithEgg(data);
        int i = (rand.nextInt(guardWeapons.length));
        this.npcItemsInv.setMeleeWeapon(guardWeapons[i].copy());
        this.npcItemsInv.setIdleItem(this.npcItemsInv.getMeleeWeapon());
        setCurrentItemOrArmor(1, new ItemStack(CinderLoE.bootsBree));
        setCurrentItemOrArmor(2, new ItemStack(CinderLoE.legsBree));
        setCurrentItemOrArmor(3, new ItemStack(CinderLoE.bodyBree));
        setCurrentItemOrArmor(4, new ItemStack(CinderLoE.helmetBreeKettle));
        this.npcItemsInv.setRangedWeapon(new ItemStack(LOTRMod.ironCrossbow));
        this.npcItemsInv.setIdleItem(this.npcItemsInv.getMeleeWeapon());
        return data;
    }

    @Override
    public void onAttackModeChange(LOTREntityNPC.AttackMode mode, boolean mounted) {
        if (mode == LOTREntityNPC.AttackMode.IDLE) {
            ((EntityLiving)this).tasks.removeTask(this.meleeAttackAI);
            ((EntityLiving)this).tasks.removeTask(this.rangedAttackAI);
            this.setCurrentItemOrArmor(0, this.npcItemsInv.getIdleItem());
        }
        if (mode == LOTREntityNPC.AttackMode.MELEE) {
            ((EntityLiving)this).tasks.removeTask(this.meleeAttackAI);
            ((EntityLiving)this).tasks.removeTask(this.rangedAttackAI);
            ((EntityLiving)this).tasks.addTask(2, this.meleeAttackAI);
            this.setCurrentItemOrArmor(0, this.npcItemsInv.getMeleeWeapon());
        }
        if (mode == LOTREntityNPC.AttackMode.RANGED) {
            ((EntityLiving)this).tasks.removeTask(this.meleeAttackAI);
            ((EntityLiving)this).tasks.removeTask(this.rangedAttackAI);
            ((EntityLiving)this).tasks.addTask(2, this.rangedAttackAI);
            this.setCurrentItemOrArmor(0, this.npcItemsInv.getRangedWeapon());
        }
    }

    @Override
    public ItemStack getPickedResult(MovingObjectPosition target) {
        return new ItemStack(CinderLoE.spawnEgg, 1, 21);
    }
    @Override
    public LOTRNPCMount createMountToRide() {
        LOTREntityHorse horse = (LOTREntityHorse)super.createMountToRide();
        horse.setMountArmor(new ItemStack(LOTRMod.horseArmorIron));
        return horse;
    }

    public void attackEntityWithRangedAttack(EntityLivingBase target, float f) {
        npcCrossbowAttack(target, f);
    }
    @Override
    public float getAlignmentBonus() {
        return 3.0f;
    }

    @Override
    public String getSpeechBank(EntityPlayer entityplayer) {
        if (this.isFriendlyAndAligned(entityplayer)) {
            if (this.hiredNPCInfo.getHiringPlayer() == entityplayer) {
                return "bree/guard/hired";
            }
            return "bree/guard/friendly";
        }
        return "bree/guard/hostile";
    }

    @Override
    public LOTRMiniQuest createMiniQuest() {
        return LOTRMiniQuestFactory.BREE.createQuest(this);
    }

    @Override
    public LOTRMiniQuestFactory getBountyHelpSpeechDir() {
        return LOTRMiniQuestFactory.BREE;
    }

}
