package io.github.aquerr.eaglefactions.listeners;

import io.github.aquerr.eaglefactions.EagleFactions;
import io.github.aquerr.eaglefactions.PluginInfo;
import io.github.aquerr.eaglefactions.entities.Faction;
import io.github.aquerr.eaglefactions.logic.PluginMessages;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.Order;
import org.spongepowered.api.event.entity.DestructEntityEvent;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.format.TextColors;

import java.util.Optional;

public class PlayerDeathListener extends AbstractListener
{
    public PlayerDeathListener(EagleFactions plugin)
    {
        super(plugin);
    }

    @Listener(order = Order.POST)
    public void onPlayerDeath(DestructEntityEvent.Death event)
    {
        if(event.getTargetEntity() instanceof Player)
        {
            Player player = (Player)event.getTargetEntity();

            getPlugin().getPowerManager().decreasePower(player.getUniqueId());

            player.sendMessage(Text.of(PluginInfo.PLUGIN_PREFIX, PluginMessages.YOUR_POWER_HAS_BEEN_DECREASED_BY + " ", TextColors.GOLD, String.valueOf(getPlugin().getConfiguration().getConfigFileds().getPowerDecrement()) + "\n",
                    TextColors.GRAY, PluginMessages.CURRENT_POWER + " ", String.valueOf(getPlugin().getPowerManager().getPlayerPower(player.getUniqueId())) + "/" + String.valueOf(getPlugin().getPowerManager().getPlayerMaxPower(player.getUniqueId()))));

            Optional<Faction> optionalChunkFaction = getPlugin().getFactionLogic().getFactionByChunk(player.getWorld().getUniqueId(), player.getLocation().getChunkPosition());

            if (getPlugin().getConfiguration().getConfigFileds().getWarZoneWorldNames().contains(player.getWorld().getName()) || (optionalChunkFaction.isPresent() && optionalChunkFaction.get().getName().equals("WarZone")))
            {
                getPlugin().getPlayerManager().setDeathInWarZone(player.getUniqueId(), true);
            }

            if (getPlugin().getConfiguration().getConfigFileds().shouldBlockHomeAfterDeathInOwnFaction())
            {
                Optional<Faction> optionalPlayerFaction = getPlugin().getFactionLogic().getFactionByPlayerUUID(player.getUniqueId());

                if (optionalChunkFaction.isPresent() && optionalPlayerFaction.isPresent() && optionalChunkFaction.get().getName().equals(optionalPlayerFaction.get().getName()))
                {
                    getPlugin().getAttackLogic().blockHome(player.getUniqueId());
                }
            }
        }
    }
}

@@ -975,32 +975,38 @@
 );%0A%0A            
+super.
 getPlugin().getP
@@ -1367,32 +1367,38 @@
  String.valueOf(
+super.
 getPlugin().getP
@@ -1592,32 +1592,38 @@
 lChunkFaction = 
+super.
 getPlugin().getF
@@ -1738,32 +1738,38 @@
             if (
+super.
 getPlugin().getC
@@ -1981,32 +1981,38 @@
                 
+super.
 getPlugin().getP
@@ -2104,16 +2104,22 @@
     if (
+super.
 getPlugi
@@ -2272,16 +2272,22 @@
 ction = 
+super.
 getPlugi
@@ -2570,16 +2570,22 @@
         
+super.
 getPlugi
@@ -2661,32 +2661,252 @@
  %7D%0A            %7D
+%0A%0A            if(super.getPlugin().getPVPLogger().isActive() && super.getPlugin().getPVPLogger().isPlayerBlocked(player))%0A            %7B%0A                super.getPlugin().getPVPLogger().removePlayer(player);%0A            %7D
 %0A        %7D%0A    %7D

Remove player from pvplogger after death