package leigh.minecraft.link.quests;

import leigh.mecha.log.MechaLogger;
import leigh.mecha.log.MechaLoggerFactory;
import leigh.minecraft.link.MinecraftLinkPlugin;
import leigh.minecraft.link.faction.Reputation;
import me.blackvein.quests.CustomReward;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

import java.util.Map;

/**
 * Custom reward handler for Quests which rewards the player with a reputation adjustment.
 *
 * @author Alex Leigh
 */
public class ReputationReward extends CustomReward {
    private static final MechaLogger logger = MechaLoggerFactory.getLogger(ReputationReward.class);

    private final static String KEY_FACTION_ID = "factionId";
    private final static String KEY_REPUTATION = "reputation";
    private final Reputation reputation;

    public ReputationReward() {
        this.setName("LCReputationReward");
        this.setAuthor("LEIGH&CO");
        this.setDisplay("Reputation");
        this.setItem("CHEST", (short) 0);
        this.addStringPrompt(KEY_FACTION_ID, "Faction ID", null);
        this.addStringPrompt(KEY_REPUTATION, "Reputation Delta", null);

        MinecraftLinkPlugin link =
                (MinecraftLinkPlugin) Bukkit.getPluginManager().getPlugin("MinecraftLink");
        assert link != null;

        reputation = link.getReputation();
    }

    @Override
    public void giveReward(Player player, Map<String, Object> map) {
        int factionId = Integer.parseInt((String) map.get(KEY_FACTION_ID));
        int delta = Integer.parseInt((String) map.get(KEY_REPUTATION));

        reputation.adjustReputation(player.getUniqueId(), factionId, delta);
    }
}
