package de.steamwar.lobby.particle.elements.custom;

import de.steamwar.lobby.particle.ParticleElement;
import de.steamwar.lobby.particle.ParticleTickData;
import de.steamwar.lobby.particle.elements.DelegatingParticleElement;
import org.bukkit.Location;
import org.bukkit.util.Vector;

public class HearthBeat extends DelegatingParticleElement {

    private double speed;

    private double y(double time) {
        double d1 = Math.pow(2, -Math.pow(time * 17 - 7.5, 2));
        double d2 = Math.pow(2, -Math.pow(time * 20 - 11, 2)) * 0.5;
        double d3 = Math.pow(2, -Math.pow(time * 25 - 17, 2)) * 0.1;
        return (d1 - d2 + d3) * 0.5;
    }

    public HearthBeat(ParticleElement particleElement, double speed) {
        super(particleElement);
        this.speed = speed;
    }

    @Override
    public String attribute() {
        return null;
    }

    @Override
    public void tick(ParticleTickData particleTickData) {
        double time = ((particleTickData.getDeg() * speed) % 360) / 360.0;
        double y = y(time) * 2;
        double x = (time - 0.5) * 2;

        Vector vector = new Vector(x, 1.1 + y - (particleTickData.getPlayer().isSneaking() ? 0.5 : 0), 0.7);
        vector.rotateAroundY(Math.toRadians(particleTickData.getPlayer().getLocation().getYaw() * -1));
        vector.setX(-vector.getX());
        vector.setZ(-vector.getZ());
        Location location = particleTickData.getPlayer().getLocation().clone().add(vector);
        ParticleTickData current = particleTickData.withLocation(location);
        particleElement.tick(current);
    }
}
