package com.yaohun.demonskills.component.projectile;

import com.yaohun.demonskills.api.SkillComponent;
import com.yaohun.demonskills.api.TargetSelector;
import com.yaohun.demonskills.target.SelfSelector;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.util.Vector;

public class ProjectilePullComponent implements SkillComponent {

    private final double pullStrength;
    private final double y;
    private final TargetSelector targetSelector;

    public ProjectilePullComponent(final double pullStrength, final double y, TargetSelector targetSelector) {
        this.pullStrength = pullStrength;
        this.y = y;
        this.targetSelector = targetSelector;
    }

    @Override
    public void execute(Player caster) {
        Location center = caster.getLocation();
        for (Entity target : targetSelector.selectTargets(caster)) {
            if (target instanceof LivingEntity) {
                LivingEntity living = (LivingEntity) target;
                Vector direction = center.toVector().subtract(living.getLocation().toVector()).normalize();
                Vector pullVector = direction.multiply(pullStrength);
                living.setVelocity(pullVector.setY(y));
            }
        }
    }
}
