package com.zivilon.cinder_loe.entity;

import com.zivilon.cinder_loe.CinderLoE;

import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.IRangedAttackMob;
import net.minecraft.entity.SharedMonsterAttributes;
import net.minecraft.entity.ai.EntityAIBase;
import net.minecraft.entity.ai.EntityAILookIdle;
import net.minecraft.entity.ai.EntityAIOpenDoor;
import net.minecraft.entity.ai.EntityAISwimming;
import net.minecraft.entity.ai.EntityAIWander;
import net.minecraft.entity.ai.EntityAIWatchClosest;
import net.minecraft.entity.ai.EntityAIWatchClosest2;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.util.StatCollector;
import net.minecraft.world.World;

import lotr.common.LOTRFoods;
import lotr.common.LOTRMod;
import lotr.common.entity.ai.LOTREntityAIAttackOnCollide;
import lotr.common.entity.ai.LOTREntityAIDrink;
import lotr.common.entity.ai.LOTREntityAIEat;
import lotr.common.entity.ai.LOTREntityAIFollowHiringPlayer;
import lotr.common.entity.ai.LOTREntityAIHiredRemainStill;
import lotr.common.entity.animal.LOTREntityHorse;
import lotr.common.entity.npc.LOTREntityMan;
import lotr.common.entity.npc.LOTREntityNPC;
import lotr.common.entity.npc.LOTRNPCMount;
import lotr.common.entity.npc.LOTRNames;
import lotr.common.fac.LOTRFaction;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class Renegade extends LOTREntityMan {

    public Renegade(World world) {
        super(world);
        setSize(0.6F, 1.8F);
        getNavigator().setAvoidsWater(true);
        getNavigator().setBreakDoors(true);
        ((EntityLiving) this).tasks.addTask(0, (EntityAIBase) new EntityAISwimming((EntityLiving) this));
        ((EntityLiving) this).tasks.addTask(1, (EntityAIBase) new LOTREntityAIHiredRemainStill(this));
        ((EntityLiving) this).tasks.addTask(2, (EntityAIBase) new LOTREntityAIAttackOnCollide(this, 1.3D, false));
        ((EntityLiving) this).tasks.addTask(3, (EntityAIBase) new LOTREntityAIFollowHiringPlayer(this));
        ((EntityLiving) this).tasks.addTask(4, (EntityAIBase) new EntityAIOpenDoor((EntityLiving) this, true));
        ((EntityLiving) this).tasks.addTask(6, (EntityAIBase) new LOTREntityAIEat(this, LOTRFoods.DUNLENDING, 8000));
        ((EntityLiving) this).tasks.addTask(6, (EntityAIBase) new LOTREntityAIDrink(this, LOTRFoods.DUNLENDING_DRINK, 8000));
        ((EntityLiving) this).tasks.addTask(7, (EntityAIBase) new EntityAIWatchClosest2((EntityLiving) this, EntityPlayer.class, 8.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));
        addTargetTasks(true);
    }

    public void attackEntityWithRangedAttack(EntityLivingBase target, float distanceFactor) {
        // Do nothing
    }

    public LOTRNPCMount createMountToRide() {
        LOTREntityHorse horse = (LOTREntityHorse) super.createMountToRide();
        horse.setMountArmor(new ItemStack(LOTRMod.horseArmorIron));
        return (LOTRNPCMount) horse;
    }

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

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

    public LOTRFaction getFaction() {
        return LOTRFaction.UTUMNO;
    }

    public String getNPCName() {
        return this.familyInfo.getName();
    }

    public String getNPCFormattedName(String npcName, String entityName) {
        if (getClass() == Renegade.class) return StatCollector
            .translateToLocalFormatted("entity.cinder_loe.Renegade.entityName", new Object[] { npcName });
        return super.getNPCFormattedName(npcName, entityName);
    }

    protected void onAttackModeChange(LOTREntityNPC.AttackMode mode, boolean mounted) {
        if (mode == LOTREntityNPC.AttackMode.IDLE) {
            setCurrentItemOrArmor(0, this.npcItemsInv.getIdleItem());
        } else {
            setCurrentItemOrArmor(0, this.npcItemsInv.getMeleeWeapon());
            ((EntityLiving) this).tasks.addTask(5, (EntityAIBase) new EntityAIWander(this, 1.0D));
        }
    }

    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++) dropItem(Items.bone, 1);
    }

    public boolean getCanSpawnHere() {
        return false;
    }

    public String getSpeechBank(EntityPlayer entityplayer) {
        if (isDrunkard()) return "renegade/drunkard/neutral";
        return "renegade/man/hostile";
    }

    List<Runnable> codeOptions = Arrays.asList(() -> {
        this.familyInfo.setName(LOTRNames.getGondorName(this.rand, this.familyInfo.isMale()));
    }, () -> {
        this.familyInfo.setName(LOTRNames.getBreeName(this.rand, this.familyInfo.isMale()));
    }, () -> {
        this.familyInfo.setName(LOTRNames.getRhudaurName(this.rand, this.familyInfo.isMale()));
    }, () -> {
        this.familyInfo.setName(LOTRNames.getRohirricName(this.rand, this.familyInfo.isMale()));
    },
        /*
         * () -> {
         * this.familyInfo.setName(LOTRNames.getDunlendingName(this.rand, this.familyInfo.isMale())),
         * }
         * () -> {
         * this.familyInfo.setName(LOTRNames.getDorwinionName(this.rand, this.familyInfo.isMale())),
         * }
         * () -> {
         * this.familyInfo.setName(LOTRNames.getDalishName(this.rand, this.familyInfo.isMale())),
         * }
         * () -> {
         * this.familyInfo.setName(LOTRNames.getRhunicName(this.rand, this.familyInfo.isMale())),
         * }
         * () -> {
         * this.familyInfo.setName(LOTRNames.getUmbarName(this.rand, this.familyInfo.isMale())),
         * }
         * () -> {
         * this.familyInfo.setName(LOTRNames.getHarnennorName(this.rand, this.familyInfo.isMale())),
         * }
         * () -> {
         * this.familyInfo.setName(LOTRNames.getSouthronCoastName(this.rand, this.familyInfo.isMale())),
         * }
         * () -> {
         * this.familyInfo.setName(LOTRNames.getNomadName(this.rand, this.familyInfo.isMale())),
         * }
         * () -> {
         * this.familyInfo.setName(LOTRNames.getGulfHaradName(this.rand, this.familyInfo.isMale())),
         * }
         * () -> {
         * this.familyInfo.setName(LOTRNames.getMoredainName(this.rand, this.familyInfo.isMale())),
         * }
         */
        () -> {
            this.familyInfo.setName(LOTRNames.getTauredainName(this.rand, this.familyInfo.isMale()));
        });

    public void setupNPCName() {
        if (this.familyInfo == null) System.out.println("[CinderCore] Family info is null");
        if (this.rand == null) System.out.println("[CinderCore] rand is null");
        Boolean gender = this.familyInfo.isMale();
        if (gender == null) System.out.println("[CinderCore] Gender is null");
        /*
         * int randomIndex = this.rand.nextInt(codeOptions.size());
         * codeOptions.get(randomIndex)
         * .run();;
         */
        this.familyInfo.setName(LOTRNames.getDorwinionName(this.rand, this.familyInfo.isMale()));
    }

    @Override
    public ItemStack getPickedResult(MovingObjectPosition target) {
        return new ItemStack(CinderLoE.spawnEgg, 1, 0);
    }
}
