package com.yaohun.demonskills.component.damage;

import com.yaohun.demonskills.SkillsMain;
import com.yaohun.demonskills.api.SkillComponent;
import com.yaohun.demonskills.api.TargetSelector;
import org.bukkit.Bukkit;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;

public class DamageComponent implements SkillComponent {

    private final double hurtValue;
    private final TargetSelector selector;
    private boolean armorPenetrate = false;
    private int numberCount = 1;
    private long delayTick = 1;

    public DamageComponent(double hurtValue, TargetSelector selector) {
        this.hurtValue = hurtValue;
        this.selector = selector;
    }

    public DamageComponent(double hurtValue, TargetSelector selector,boolean armorPenetrate) {
        this.hurtValue = hurtValue;
        this.selector = selector;
        this.armorPenetrate = armorPenetrate;
    }

    public DamageComponent(double hurtValue, TargetSelector selector,boolean armorPenetrate,int numberCount) {
        this.hurtValue = hurtValue;
        this.selector = selector;
        this.armorPenetrate = armorPenetrate;
        this.numberCount = numberCount;
    }

    public DamageComponent(double hurtValue, TargetSelector selector,boolean armorPenetrate,int numberCount,long delayTick) {
        this.hurtValue = hurtValue;
        this.selector = selector;
        this.armorPenetrate = armorPenetrate;
        this.numberCount = numberCount;
        this.delayTick = delayTick;
    }

    @Override
    public void execute(Player caster) {
        for (int i = 0; i < numberCount; i++) {
            Bukkit.getScheduler().runTaskLater(SkillsMain.inst(), () -> {
                for (Entity target : selector.selectTargets(caster)) {
                    if (target instanceof LivingEntity) {
                        LivingEntity living = (LivingEntity) target;
                        if (!armorPenetrate) {
                            living.damage(hurtValue, caster);
                            living.damage(1,caster);
                        } else {
                            double bloodVolume = living.getHealth();
                            double newBloodVolume = Math.max(0.0D, bloodVolume - hurtValue);
                            living.setHealth(newBloodVolume);
                            living.damage(1,caster);
                        }
                    }
                }
            }, i * delayTick);
        }
    }

}
