package me.loganb1max.minionsplus.model.minions;

import me.loganb1max.minionsplus.MinionsPlus;
import me.loganb1max.minionsplus.model.Minion;
import me.loganb1max.minionsplus.util.Replacer;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import java.text.DecimalFormat;
import java.util.*;

public class ButcherMinion extends Minion {

	public ButcherMinion(final UUID owner, final String ownerName) {
		super(
				MinionsPlus.getInstance().getConfig().getConfigurationSection("Butcher"),
				owner,
				ownerName
		);
	}

	public ButcherMinion(final UUID owner, final String ownerName, final Location location, final double energy, final int level, final Set<Block> linkedBlocks) {
		this(owner, ownerName);
		setLocation(location);
		setEnergy(energy);
		setLevel(level);
		setLinkedBlocks(linkedBlocks);
		findStand();
	}

	public int getRadius(final int level) {
		return getSection().getInt("Levels." + level + ".Radius", 1);
	}

	@Override
	public void tick() {
		if (getEnergy() < getEnergyPerUse(getLevel())) return;
		final int radius = getRadius(getLevel());
		boolean useEnergy = false;
		for (Entity e : getStand().getNearbyEntities(radius, radius, radius)) {
			if (!(e instanceof LivingEntity)) continue;
			if (e instanceof Player) continue;
			LivingEntity le = (LivingEntity) e;
			MinionsPlus.getInstance().getMinionManager().getButcherKillMap().put(le, this);
			le.damage(Integer.MAX_VALUE);
			useEnergy = true;
		}
		if (useEnergy) consumeEnergy(getEnergyPerUse(getLevel()));
	}

	@Override
	public Replacer getReplacer() {
		final DecimalFormat format = new DecimalFormat("#.#");
		return Replacer.create()
				.add("%level%", String.valueOf(getLevel()))
				.add("%energy%", format.format(getEnergy()))
				.add("%owner%", getOwnerName())
				.add("%radius%", String.valueOf(getRadius(getLevel())));
	}

}
