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.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;

public class CircleParticlesComponent implements SkillComponent {

    private final Particle particle;
    private final int amount; // 每点粒子数量（通常为1）
    private final double radius; // 圆圈半径
    private final int points; //粒子分布密度（推荐 20~60）
    private int playCount = 1;

    public CircleParticlesComponent(Particle particle, int amount, double radius, int points) {
        this.particle = particle;
        this.amount = amount;
        this.radius = radius;
        this.points = points;
    }

    public CircleParticlesComponent(Particle particle, int amount, double radius, int points, int playCount) {
        this.particle = particle;
        this.amount = amount;
        this.radius = radius;
        this.points = points;
        this.playCount = playCount;
    }

    @Override
    public void execute(Player caster) {
        new BukkitRunnable() {
            private int count = 0;
            @Override
            public void run() {
                if(count++ >= playCount) {
                    cancel();
                    return;
                }
                Location center = caster.getLocation().add(0, 0.05, 0);
                double angleStep = 2 * Math.PI / points;
                for (int i = 0; i < points; i++) {
                    double angle = i * angleStep;
                    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, amount, 0, 0, 0, 0);
                }
            }
        }.runTaskTimer(SkillsMain.inst(),0L,5L);
    }
}
