package nivia.modules.exploits;

import net.minecraft.network.play.client.C07PacketPlayerDigging;
import net.minecraft.network.play.client.C0APacketAnimation;
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing;
import nivia.events.EventTarget;
import nivia.events.Priority;
import nivia.events.events.EventPacketSend;
import nivia.events.events.EventPostMotionUpdates;
import nivia.events.events.EventPreMotionUpdates;
import nivia.managers.PropertyManager;
import nivia.modules.Module;
import nivia.utils.Helper;
import nivia.utils.utils.Timer;

public class CivBreak extends Module {

    private boolean b;
    public CivBreak() {
        super("CivBreak", 0, 0xFFFFFF, Category.EXPLOITS, "Allows you to break a block multiple times.", new String[]{"cv", "civb", "cbreak", "cb", "cvb"}, true);
    }

    //Thanks to N3xus_DK for original idea & help with method.

    public PropertyManager.DoubleProperty range = new PropertyManager.DoubleProperty(this, "Range", 1, 0, 20);
    public PropertyManager.DoubleProperty delay = new PropertyManager.DoubleProperty(this, "Delay", 1000, 0, 5000);
    private BlockPos blockPos;

    private EnumFacing enumFacing;

    private Timer timer = new Timer();
    @EventTarget
    public void onPre(EventPreMotionUpdates e) {
        if (blockPos == null)
            return;
        if (mc.thePlayer.getDistance(blockPos.getX(), blockPos.getY() + 0.5F, blockPos.getZ()) <= range.getValue()) {
            float[] angles = Helper.blockUtils().getAngles(mc.thePlayer, blockPos);
            mc.thePlayer.rotationYaw = angles[0];
            mc.thePlayer.rotationPitch = angles[1];
        }
        if (blockPos == null || Helper.world().getBlockState(blockPos) == null)
            return;

        if (timer.hasTimeElapsed((long) (delay.getValue() / 2 + Helper.mathUtils().getRandom((int) delay.getValue() / 2))) && mc.thePlayer.getDistance(blockPos.getX(), blockPos.getY(), blockPos.getZ()) <= range.getValue()) {
            Helper.sendPacket(new C0APacketAnimation());
            Helper.sendPacket(new C07PacketPlayerDigging(C07PacketPlayerDigging.Action.STOP_DESTROY_BLOCK, blockPos, enumFacing));
            timer.reset();
        }
    }
    @EventTarget
    public void onPost(EventPostMotionUpdates post) {

    }

    @EventTarget(Priority.HIGHEST)
    public void onPacketSend(EventPacketSend e) {
        if (e.getPacket() instanceof C07PacketPlayerDigging) {
            C07PacketPlayerDigging packet = (C07PacketPlayerDigging) e.getPacket();
            if (packet.func_180762_c() == C07PacketPlayerDigging.Action.START_DESTROY_BLOCK) {
                if (blockPos != packet.func_179715_a()) {
                    blockPos = packet.func_179715_a();
                    enumFacing = packet.func_179714_b();
                } else {
                    e.setCancelled(true);
                }
            }
        }
    }

    @Override
    protected void onDisable() {
        super.onDisable();
        blockPos = null;
        enumFacing = null;
        timer.reset();
    }
}