package io.github.aquerr.eaglefactions.common.commands;

import io.github.aquerr.eaglefactions.api.EagleFactions;
import io.github.aquerr.eaglefactions.api.entities.Claim;
import io.github.aquerr.eaglefactions.api.entities.Faction;
import io.github.aquerr.eaglefactions.common.EagleFactionsPlugin;
import io.github.aquerr.eaglefactions.common.PluginInfo;
import io.github.aquerr.eaglefactions.common.messaging.Messages;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.command.CommandException;
import org.spongepowered.api.command.CommandResult;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.command.args.CommandContext;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.format.TextColors;

import java.util.Optional;

public class RegenCommand extends AbstractCommand
{
    public RegenCommand(EagleFactions plugin)
    {
        super(plugin);
    }

    @Override
    public CommandResult execute(CommandSource source, CommandContext context) throws CommandException
    {
        final Faction factionToRegen = context.requireOne("faction");

        if (factionToRegen.isSafeZone() || factionToRegen.isWarZone()) {
            throw new CommandException(Text.of(PluginInfo.ERROR_PREFIX, TextColors.RED, "This faction cannot be disbanded!"));
        }

        /* Firstly, we're simply disbanding the faction. */

        boolean didSucceed = super.getPlugin().getFactionLogic().disbandFaction(factionToRegen.getName());
        if(didSucceed)
        {
            if (source instanceof Player) {
                Player player = (Player) source;

                EagleFactionsPlugin.AUTO_CLAIM_LIST.remove(player.getUniqueId());
                EagleFactionsPlugin.CHAT_LIST.remove(player.getUniqueId());
            }
        }
        else
        {
            throw new CommandException((Text.of(PluginInfo.PLUGIN_PREFIX, TextColors.RED, Messages.SOMETHING_WENT_WRONG)));
        }

        /* After a successful disband we can regenerate faction claims. */

        for (Claim claim : factionToRegen.getClaims()) {
            Sponge.getServer().getWorld(claim.getWorldUUID()).ifPresent(world -> {
                world.regenerateChunk(claim.getChunkPosition());
            });
        }


        source.sendMessage(Text.of(PluginInfo.PLUGIN_PREFIX, TextColors.WHITE, "The faction was successfully regenerated!"));

        return CommandResult.success();
    }
}

@@ -817,16 +817,58 @@
 tColors;
+%0Aimport org.spongepowered.api.world.World;
 %0A%0Aimport
@@ -1287,16 +1287,24 @@
 rZone())
+%0A       
  %7B%0A     
@@ -1678,16 +1678,28 @@
  Player)
+%0A           
  %7B%0A     
@@ -2213,16 +2213,24 @@
 laims())
+%0A       
  %7B%0A     
@@ -2235,16 +2235,40 @@
         
+ Optional%3CWorld%3E world =
  Sponge.
@@ -2313,34 +2313,204 @@
 D())
-.ifPresent(world -%3E %7B%0A    
+;%0A%0A            if (!world.isPresent())%0A            %7B%0A                throw new CommandException((Text.of(PluginInfo.PLUGIN_PREFIX, TextColors.RED, Messages.SOMETHING_WENT_WRONG)));%0A            %7D%0A%0A
     
@@ -2523,16 +2523,22 @@
   world.
+get().
 regenera
@@ -2584,26 +2584,9 @@
     
-    %7D);%0A        %7D%0A
+%7D
 %0A%0A  

Fix code formatting