package com.yaohun.demonskills.component;

import com.yaohun.demonskills.api.SkillComponent;
import com.yaohun.demonskills.api.TargetSelector;
import com.yaohun.demonskills.target.SelfSelector;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;

public class PotionEffectComponent implements SkillComponent {

    private final PotionEffectType effectType;
    private final int ticks;
    private final int level;
    private TargetSelector targetSelector = new SelfSelector();

    public PotionEffectComponent(PotionEffectType effectType, int ticks, int level) {
        this.effectType = effectType;
        this.ticks = ticks;
        this.level = level;
    }

    public PotionEffectComponent(PotionEffectType effectType, int ticks, int level, TargetSelector targetSelector) {
        this.effectType = effectType;
        this.ticks = ticks;
        this.level = level;
        this.targetSelector = targetSelector;
    }

    @Override
    public void execute(Player caster) {
        if (targetSelector.equals(new SelfSelector())) {
            caster.addPotionEffect(new PotionEffect(effectType, ticks, level, true, true));
        } else {
            for (Entity entity : targetSelector.selectTargets(caster)){
                if(entity instanceof LivingEntity){
                    LivingEntity living = (LivingEntity) entity;
                    living.addPotionEffect(new PotionEffect(effectType, ticks, level, true, true));
                }
            }
        }
    }
}
