package com.yaohun.demonskills.component.particles;

import com.yaohun.demonskills.SkillsMain;
import com.yaohun.demonskills.api.SkillComponent;
import com.yaohun.demonskills.api.TargetSelector;
import com.yaohun.demonskills.target.SelfSelector;
import com.yaohun.demonskills.util.RandomUtil;
import org.bukkit.Location;
import org.bukkit.Particle;
import org.bukkit.World;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;

public class ParticlesComponent implements SkillComponent {

    private final Particle particle;
    private final int amount; // 每次出现粒子数量
    private final double radius; // 半径范围
    private final int playCount; // 播放次数
    private TargetSelector targetSelector = new SelfSelector();

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

    public ParticlesComponent(Particle particle, int amount, double radius, int playCount,TargetSelector targetSelector) {
        this.particle = particle;
        this.amount = amount;
        this.radius = radius;
        this.playCount = playCount;
        this.targetSelector = targetSelector;
    }

    @Override
    public void execute(Player caster) {
        if(targetSelector.equals(new SelfSelector())) {
            new BukkitRunnable() {
                private int count = 0;
                @Override
                public void run() {
                    if (count++ >= playCount) {
                        cancel();
                        return;
                    }
                    Location center = caster.getLocation().add(0, 1, 0); // 调高粒子位置
                    World world = center.getWorld();
                    for (int i = 0; i < amount; i++) {
                        world.spawnParticle(particle, RandomUtil.getRandomLocation(caster,radius), 1, 0, 0, 0, 0);
                    }
                }
            }.runTaskTimer(SkillsMain.inst(), 0L, 1L);
        } else {
            for (Entity target : targetSelector.selectTargets(caster)) {
                if (target instanceof LivingEntity) {
                    LivingEntity living = (LivingEntity) target;
                    new BukkitRunnable() {
                        private int count = 0;
                        @Override
                        public void run() {
                            if (count++ >= playCount) {
                                cancel();
                                return;
                            }
                            Location center = living.getLocation().add(0, 1, 0); // 调高粒子位置
                            World world = center.getWorld();
                            for (int i = 0; i < amount; i++) {
                                world.spawnParticle(particle, RandomUtil.getRandomLocation(living,radius), 1, 0, 0, 0, 0);
                            }
                        }
                    }.runTaskTimer(SkillsMain.inst(), 0L, 1L);
                }
            }
        }
    }
}
