package com.yaohun.demonskills.component.particles;

import com.yaohun.demonskills.SkillsMain;
import com.yaohun.demonskills.api.SkillComponent;
import org.bukkit.Location;
import org.bukkit.Particle;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;

public class RotatingCircle implements SkillComponent {

    private final Particle particle;
    private final double radius;
    private final int totalTicks;

    public RotatingCircle(Particle particle, double radius, int totalTicks) {
        this.particle = particle;
        this.radius = radius;
        this.totalTicks = totalTicks;
    }

    @Override
    public void execute(Player caster) {
        Location center = caster.getLocation().add(0, 0.05, 0);

        new BukkitRunnable() {
            int tick = 0;

            @Override
            public void run() {
                if (tick++ >= totalTicks) {
                    cancel();
                    return;
                }

                double angle = (tick % 360) * Math.PI / 180;
                double x = Math.cos(angle) * radius;
                double z = Math.sin(angle) * radius;

                Location particleLoc = center.clone().add(x, 0, z);
                caster.getWorld().spawnParticle(particle, particleLoc, 2, 0, 0, 0, 0);
            }
        }.runTaskTimer(SkillsMain.inst(), 0L, 1L);
    }
}
