package leigh.minecraft.link.quests;

import me.blackvein.quests.CustomReward;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeModifier;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import java.util.Arrays;
import java.util.Map;
import java.util.UUID;

/**
 * Custom reward for Quests which rewards the player with a LEIGH&CO prototype sword.
 *
 * @author Alex Leigh
 */
public class SwordReward extends CustomReward {
    // Note that changing these may break previously-created quests. See quests.yml
    private final static String KEY_ATTACK_SPEED = "attackSpeed";
    private final static String KEY_ATTACK_POWER = "attackPower";
    private final static String KEY_LORE = "lore";
    private final static String KEY_ENCHANT_DMG_ALL = "damageAll";
    private final static String KEY_DISPLAY_NAME = "displayName";

    public SwordReward() {
        // This is the name the quest editor will use
        this.setName("LCSwordReward");
        this.setAuthor("LEIGH&CO");
        this.setDisplay("Prototype Sword");
        this.setItem("CHEST", (short) 0);
        this.addStringPrompt(KEY_DISPLAY_NAME, "Display Name", null);
        this.addStringPrompt(KEY_LORE, "Lore Text", null);
        this.addStringPrompt(KEY_ATTACK_POWER, "Attack power of the sword.", null);
        this.addStringPrompt(KEY_ATTACK_SPEED, "Attack Speed.", null);
        this.addStringPrompt(KEY_ENCHANT_DMG_ALL, "DMG Enchant Level (0-3)", "0");
    }

    @Override
    public void giveReward(Player player, Map<String, Object> map) {
        // Multiple of 9, between 9 and 54
        Inventory inv = Bukkit.getServer().createInventory(player, 9, "Reward");
        ItemStack sword = new ItemStack(Material.IRON_SWORD, 1);
        ItemMeta meta = sword.getItemMeta();

        String lore = (String) map.get(KEY_LORE);
        if (lore != null) {
            meta.setLore(Arrays.asList(lore));
        }

        String name = (String) map.get(KEY_DISPLAY_NAME);
        if (name != null) {
            meta.setDisplayName(name);
        }

        meta.setLore(Arrays.asList("A LEIGH&CO factory prototype."));

        try {
            int power = Integer.parseInt((String) map.get(KEY_ATTACK_POWER));
            AttributeModifier damageModifier = new AttributeModifier(UUID.randomUUID(),
                    "Technica", power, AttributeModifier.Operation.ADD_NUMBER, EquipmentSlot.HAND);
            meta.addAttributeModifier(Attribute.GENERIC_ATTACK_DAMAGE, damageModifier);
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            int speed = Integer.parseInt((String) map.get(KEY_ATTACK_SPEED));
            AttributeModifier speedModifier = new AttributeModifier(UUID.randomUUID(),
                    "Technica", speed, AttributeModifier.Operation.ADD_NUMBER, EquipmentSlot.HAND);
            meta.addAttributeModifier(Attribute.GENERIC_ATTACK_SPEED, speedModifier);
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            int dmgLevel = Integer.parseInt((String) map.get(KEY_ENCHANT_DMG_ALL));
            // We prompted for 0-3 but will actually take more.
            if (dmgLevel > 0) {
                meta.addEnchant(Enchantment.DAMAGE_ALL, dmgLevel, true);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        sword.setItemMeta(meta);
        inv.setItem(0, sword);
        player.openInventory(inv);
    }
}
