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

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

    public ProjectileAroundComponent(final double vigor, final double y) {
        this.vigor = vigor;
        this.y = y;
    }
    public ProjectileAroundComponent(final double vigor, final double y,boolean LeftDirection) {
        this.vigor = vigor;
        this.y = y;
        this.LeftDirection = LeftDirection;
    }


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

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

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