package ru.themixray.repeating_mod.event.events;

import net.minecraft.entity.MovementType;
import net.minecraft.util.math.Vec3d;
import ru.themixray.repeating_mod.Main;
import ru.themixray.repeating_mod.event.RecordEvent;

public class MoveEvent extends RecordEvent {
    public Vec3d vec;
    public float yaw;
    public float pitch;

    public static final float MOVE_THRESHOLD = 0.001f;

    public static MoveEvent deserialize(String[] a) {
        return new MoveEvent(new Vec3d(
                Double.parseDouble(a[0]),
                Double.parseDouble(a[1]),
                Double.parseDouble(a[2])),
                Float.parseFloat(a[3]),
                Float.parseFloat(a[4]));
    }

    public MoveEvent(Vec3d vec, float yaw, float pitch) {
        this.vec = vec;
        this.yaw = yaw;
        this.pitch = pitch;
    }

    public void replay() {
        if (Main.client.player != null) {
            Vec3d p = Main.client.player.getPos();
            Vec3d v = new Vec3d(vec.getX() - p.getX(), vec.getY() - p.getY(), vec.getZ() - p.getZ());
            if (Math.abs(v.x) > MOVE_THRESHOLD || Math.abs(v.y) > MOVE_THRESHOLD || Math.abs(v.z) > MOVE_THRESHOLD)
                Main.client.player.move(MovementType.SELF, v);

            if (Math.abs(Main.client.player.getYaw() - yaw) > MOVE_THRESHOLD)
                Main.client.player.setYaw(yaw);
            if (Math.abs(Main.client.player.getPitch() - pitch) > MOVE_THRESHOLD)
                Main.client.player.setPitch(pitch);
        }
    }

    protected String[] serializeArgs() {
        return new String[]{
                String.valueOf(vec.getX()),
                String.valueOf(vec.getZ()),
                String.valueOf(yaw),
                String.valueOf(pitch)
        };
    }
}
