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

    private final Vector vector;
    private final double x;
    private final double y;
    private final double z;
    private TargetSelector targetSelector = new SelfSelector();

    public ProjectileVectorComponent(Vector vector, double x, double y, double z) {
        this.vector = vector;
        this.x = x;
        this.y = y;
        this.z = z;
    }

    public ProjectileVectorComponent(Vector vector, double x, double y, double z,TargetSelector targetSelector) {
        this.vector = vector;
        this.x = x;
        this.y = y;
        this.z = z;
        this.targetSelector = targetSelector;
    }

    @Override
    public void execute(Player caster) {
        if(x != 0){
            this.vector.setX(x);
        }
        if(y != 0){
            this.vector.setY(y);
        }
        if(z != 0){
            this.vector.setZ(z);
        }
        if(targetSelector.equals(new SelfSelector())) {
            caster.setVelocity(vector);
        } else {
            for (Entity target : targetSelector.selectTargets(caster)) {
                if (target instanceof LivingEntity) {
                    LivingEntity living = (LivingEntity) target;
                    living.setVelocity(vector);
                }
            }
        }
    }
}
