package leigh.minecraft.murderarium;

import net.milkbowl.vault.economy.Economy;
import net.milkbowl.vault.economy.EconomyResponse;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Item;
import org.bukkit.entity.Player;
import org.bukkit.event.entity.EntityDamageByBlockEvent;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.ProjectileHitEvent;
import org.bukkit.event.entity.ProjectileLaunchEvent;
import org.bukkit.event.player.PlayerPickupItemEvent;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.mineacademy.corearena.CoreArenaPlugin;
import org.mineacademy.fo.Common;
import org.mineacademy.fo.remain.CompSound;
import org.mineacademy.game.exception.EventHandledException;
import org.mineacademy.game.impl.ArenaPlayer;
import org.mineacademy.game.impl.arena.FeatureArena;
import org.mineacademy.game.model.ExpItem;
import org.mineacademy.game.settings.Localization;
import org.mineacademy.game.type.DeathCause;
import org.mineacademy.game.type.LeaveCause;
import org.mineacademy.game.type.StopCause;

import static org.bukkit.Bukkit.getServer;

/**
 * This class implements the LEIGH&CO Murderarium. It works substantially like the regular CoreArena with the
 * exception that players are paid directly for picking up XP orbs that drop from kills.
 *
 * @author Alex Leigh
 */
public class Murderarium extends FeatureArena {
    private final  RegisteredServiceProvider<Economy> economyProvider;

    public Murderarium(String name) {
        super(name);
        economyProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class);
    }

    @Override
    /**
     * This custom modification directly pays the player marbles. It continues to perform the regular mechanic,
     * so the original functionality is maintained.
     */
    public void onPlayerPickupTag(PlayerPickupItemEvent e, ExpItem tag) throws EventHandledException {
        super.checkRunning();

        final Player pl = e.getPlayer();
        final Item item = e.getItem();
        final ArenaPlayer.InArenaCache cache = CoreArenaPlugin.getDataFor(e.getPlayer()).getArenaCache();

        // Pay the player
        {
            cache.giveAndShowExp(pl, tag.getExpGiven());
            economyProvider.getProvider().depositPlayer(pl, tag.getExpGiven());
            Common.tell(pl, "Got paid! Collected M"+tag.getExpGiven());
            CompSound.ORB_PICKUP.play(pl, 1, 1);
            Common.tell(pl, Localization.Experience.PICKUP.replace("{level}",
                    Localization.Cases.LEVEL.formatWithCount(cache.getLevel())).replace("{exp}",
                    Localization.Cases.EXP.formatWithCount(tag.getExpGiven())));
        }

        // Take care of the item
        {
            e.setCancelled(true);
            item.remove();
        }
    }


    @Override
    public void onPlayerDeath(Player pl, Player killer) throws EventHandledException {
        super.onPlayerDeath(pl, killer);

        kickPlayer(pl, LeaveCause.KILLED);

        getMessenger().broadcastExcept(pl, pl, Localization.Arena.Game.KICK_KILL_BROADCAST.replace("{killer}", killer.getName()));
        getMessenger().tell(pl, Localization.Arena.Game.KICK_KILL_TO_VICTIM.replace("{killer}", killer.getName()));
    }

    @Override
    public void onPlayerDeath(Player pl, DeathCause cause) throws EventHandledException {
        super.onPlayerDeath(pl, cause);

        kickPlayer(pl, LeaveCause.KILLED);
    }

    @Override
    protected void handleArenaPostStop(StopCause cause) {

    }

    @Override
    public void onPlayerDamage(EntityDamageByEntityEvent event, Player player, Entity source, double damage) {

    }

    @Override
    public void onPlayerBlockDamage(EntityDamageByBlockEvent event, Player player, double damage) {

    }

    @Override
    public void onProjectileLaunch(ProjectileLaunchEvent event) {

    }

    @Override
    public void onProjectileHit(ProjectileHitEvent event) {

    }
}
