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 ProjectileAdvanceComponent implements SkillComponent {

    private final double vigor;
    private final double y;
    private TargetSelector targetSelector = new SelfSelector();

    public ProjectileAdvanceComponent(final double vigor, final double y) {
        this.vigor = vigor;
        this.y = y;
    }

    public ProjectileAdvanceComponent(final double vigor, final double y,TargetSelector targetSelector) {
        this.vigor = vigor;
        this.y = y;
        this.targetSelector = targetSelector;
    }

    @Override
    public void execute(Player caster) {
        if(targetSelector.equals(new SelfSelector())) {
            Location origin = caster.getEyeLocation();
            Vector direction = origin.getDirection().normalize();
            caster.setVelocity(direction.multiply(vigor).setY(y));
        } else {
            for (Entity target : targetSelector.selectTargets(caster)) {
                if (target instanceof LivingEntity) {
                    LivingEntity living = (LivingEntity) target;
                    Location origin = living.getEyeLocation();
                    Vector direction = origin.getDirection().normalize();
                    living.setVelocity(direction.multiply(vigor).setY(y));
                }
            }
        }
    }
}
