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;
import org.bukkit.util.Vector;

public class DiscParticlesComponent implements SkillComponent {

    private final Particle particle;
    private final int amount;
    private int playCount = 10;

    public DiscParticlesComponent(Particle particle, int amount) {
        this.particle = particle;
        this.amount = amount;
    }

    public DiscParticlesComponent(Particle particle, int amount, int playCount) {
        this.particle = particle;
        this.amount = amount;
        this.playCount = playCount;
    }

    @Override
    public void execute(Player caster) {
        Location origin = caster.getLocation().add(0,0.05,0);
        double angleStep = 360.0 / 20;
        for (int i = 0; i < 20; i++) {
            double angle = Math.toRadians(i * angleStep);
            Vector dir = new Vector(Math.cos(angle), 0, Math.sin(angle)).normalize().multiply(0.3);
            Location start = origin.clone();
            new BukkitRunnable() {
                int ticks = 0;
                Location pos = start.clone();
                @Override
                public void run() {
                    if (ticks++ > playCount) {
                        cancel();
                        return;
                    }
                    pos.add(dir);
                    caster.getWorld().spawnParticle(particle, pos, amount, 0, 0, 0, 0);
                }
            }.runTaskTimer(SkillsMain.inst(), 0L, 1L);
        }
    }
}
