package com.zivilon.cinder_loe.entity;

import java.util.Random;

import lotr.common.LOTRMod;
import lotr.common.entity.npc.LOTREntityGollum;
import lotr.common.entity.npc.LOTRSpeech;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.entity.Entity;
import net.minecraft.entity.ai.EntityAIBase;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraft.util.MathHelper;
import net.minecraft.util.Vec3;
import net.minecraft.world.World;

public class MonkeyBananaAI extends EntityAIBase {
    private LOTREntityGollum theGollum;
    private double moveSpeed;
    private boolean avoidsWater;
    private World theWorld;
    private double xPosition;
    private double yPosition;
    private double zPosition;
    private int moveTick;
    private int fishTick;
    private boolean finished;

    public MonkeyBananaAI(LOTREntityGollum entity, double d) {
        this.theGollum = entity;
        this.moveSpeed = d;
        this.theWorld = entity.worldObj;
        this.setMutexBits(3);
    }

    public boolean shouldExecute() {
        if (this.theGollum.getGollumOwner() == null) {
            return false;
        }
        if (this.theGollum.isGollumSitting()) {
            return false;
        }
        if (this.theGollum.prevFishTime > 0) {
            return false;
        }
        if (this.theGollum.isFishing) {
            return false;
        }
        if (this.theGollum.getEquipmentInSlot(0) != null) {
            return false;
        }
        if (this.theGollum.getRNG().nextInt(60) == 0) {
            Vec3 vec3 = this.findPossibleFishingLocation();
            if (vec3 == null) {
                return false;
            }
            this.xPosition = vec3.xCoord;
            this.yPosition = vec3.yCoord;
            this.zPosition = vec3.zCoord;
            return true;
        }
        return false;
    }

    private Vec3 findPossibleFishingLocation() {
        Random random = this.theGollum.getRNG();
        int range = 16;
        for (int l = 0; l < 32; ++l) {
            int x = MathHelper.floor_double((double)this.theGollum.posX) - range + random.nextInt(range * 2 + 1);
            int y = MathHelper.floor_double((double)this.theGollum.boundingBox.minY) - 8 + random.nextInt(17);
            int z = MathHelper.floor_double((double)this.theGollum.posZ) - range + random.nextInt(range * 2 + 1);
            Block banana = theWorld.getBlock(x, y, z);

            if (banana == LOTRMod.bananaBlock) {
                return Vec3.createVectorHelper(((double)x + 0.5), ((double)y + 0.5), (double)z + 0.5);
            }
        }
        return null;
    }

    public boolean continueExecuting() {
        return this.theGollum.getGollumOwner()
                != null && !this.theGollum.isGollumSitting()
                && this.moveTick < 300
                && !this.finished;
    }

    public void startExecuting() {
        this.avoidsWater = this.theGollum.getNavigator().getAvoidsWater();
        this.theGollum.getNavigator().setAvoidsWater(false);
        this.finished = false;
    }

    public void resetTask() {
        this.theGollum.getNavigator().clearPathEntity();
        this.theGollum.getNavigator().setAvoidsWater(this.avoidsWater);
        this.moveTick = 0;

        this.fishTick = 0;
        if (this.finished) {
            this.finished = false;
            this.theGollum.prevFishTime = 3000;
        } else {
            this.theGollum.prevFishTime = 600;
        }
        this.theGollum.isFishing = false;
    }

    public void updateTask() {
        if (theGollum.getDistance(xPosition, yPosition, zPosition) < 4.0) {
            int x = MathHelper.floor_double((double)this.theGollum.posX);
            int y = MathHelper.floor_double((double)this.theGollum.boundingBox.minY);
            int z = MathHelper.floor_double((double)this.theGollum.posZ);

            if (theWorld.getBlock(x, y, z) == LOTRMod.bananaBlock) {
                theWorld.setBlockToAir(x, y, z);
                LOTRMod.bananaBlock.dropBlockAsItem(theWorld, x, y, z, theWorld.getBlockMetadata(x, y, z), 0);

                finished = true;
                LOTRSpeech.sendSpeech(this.theGollum.getGollumOwner(), this.theGollum, LOTRSpeech.getRandomSpeechForPlayer(this.theGollum, "monkey/say", this.theGollum.getGollumOwner()));
            } else {
                theGollum.getNavigator().tryMoveToXYZ(xPosition, yPosition, zPosition, moveSpeed);
                moveTick++;
            }
        }
    }

    private boolean atFishingLocation() {
        if (this.theGollum.getDistanceSq(this.xPosition, this.yPosition, this.zPosition) < 4.0) {
            int k;
            int j;
            int i = MathHelper.floor_double((double)this.theGollum.posX);
            return this.theWorld.getBlock(i, j = MathHelper.floor_double((double)this.theGollum.boundingBox.minY), k = MathHelper.floor_double((double)this.theGollum.posZ)).getMaterial() == Material.water || this.theWorld.getBlock(i, j - 1, k).getMaterial() == Material.water;
        }
        return false;
    }
}

