package me.loganb1max.minions.model.minions;

import me.loganb1max.minions.MinionsPlugin;
import me.loganb1max.minions.model.AbstractMinion;
import me.loganb1max.minions.util.ItemUtil;
import me.loganb1max.minions.util.Replacer;
import me.loganb1max.minions.util.SkullCreator;
import me.loganb1max.minions.util.Text;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import java.util.List;
import java.util.stream.Collectors;

public class FarmerMinion extends AbstractMinion {

	private final ConfigurationSection section;

	public FarmerMinion(final Player owner) {
		super(owner);
		this.section = MinionsPlugin.getInstance().getConfig().getConfigurationSection("Farmer");
	}

	@Override
	public ItemStack getIcon() {
		final Material material = Material.getMaterial(section.getString("Item.Material"));
		final int durability = section.getInt("Item.Durability");
		final String skull = section.getString("Item.Skull", null);
		final String displayName = section.getString("Item.DisplayName");
		final List<String> lore = section.getStringList("Item.Lore");
		final Replacer replacer = Replacer.create()
				.add("%level%", String.valueOf(getLevel()))
				.add("%energy%", String.valueOf(getEnergy()))
				.add("%owner%", getOwnerName())
				.add("%radius%", String.valueOf(getRadius(getLevel())));
		ItemStack item = new ItemStack(material, 1);
		item.setDurability((short) durability);
		if (material == Material.SKULL_ITEM && skull != null) {
			item = SkullCreator.itemWithBase64(item, skull);
		}
		final ItemMeta meta = item.getItemMeta();
		meta.setDisplayName(Text.color(displayName));
		meta.setLore(lore.stream().map(Text::color).map(replacer::replace).collect(Collectors.toList()));
		item.setItemMeta(meta);
		return item;
	}

	@Override
	public ItemStack toItemStack() {
		ItemStack item = getIcon();
		item = ItemUtil.setKeyString(item, "minion-owner", getOwner().toString());
		item = ItemUtil.setKeyString(item, "minion-owner-name", getOwnerName());
		item = ItemUtil.setKeyInt(item, "minion-level", getLevel());
		item = ItemUtil.setKeyDouble(item, "minion-energy", getEnergy());
		return item;
	}

	@Override
	public void equipEquipment(ArmorStand stand) {
		final String skull = section.getString("Equipment.Skull", "");
		ItemStack skullItem = SkullCreator.itemFromBase64(skull);
		stand.setHelmet(skullItem);
		final ItemStack chestplate = ItemUtil.fromConfigSection(section.getConfigurationSection("Equipment.Chestplate"));
		stand.setChestplate(chestplate);
		final ItemStack leggings = ItemUtil.fromConfigSection(section.getConfigurationSection("Equipment.Leggings"));
		stand.setLeggings(leggings);
		final ItemStack boots = ItemUtil.fromConfigSection(section.getConfigurationSection("Equipment.Boots"));
		stand.setBoots(boots);
		final ItemStack rightHand = ItemUtil.fromConfigSection(section.getConfigurationSection("Equipment.RightHand"));
		stand.setItemInHand(rightHand);
	}

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

	public double getPrice(final int level) {
		return section.getDouble("Levels." + level + ".Price", 0.0);
	}

	public double getEnergyPerUse(final int level) {
		return section.getDouble("Levels." + level + ".EnergyPerAction", 0.0);
	}

	@Override
	public String getName() {
		return section.getString("Name");
	}

}
