package derpatiel.progressivediff;

import derpatiel.progressivediff.util.LOG;
import derpatiel.progressivediff.util.MobNBTHandler;
import net.minecraft.command.CommandBase;
import net.minecraft.command.CommandException;
import net.minecraft.command.ICommandSender;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.Style;
import net.minecraft.util.text.TextComponentString;
import net.minecraftforge.fml.common.registry.EntityEntry;
import net.minecraftforge.fml.common.registry.EntityRegistry;
import net.minecraftforge.fml.common.registry.ForgeRegistries;

import javax.annotation.Nullable;
import java.util.Iterator;
import java.util.List;

/**
 * Created by Jim on 5/7/2017.
 */
public class ServerCommand extends CommandBase {

    private String[] usage = new String[]{
            "progdiff (Progressive Difficulty) help:",
            "\"progdiff sync\" sync the config for the server",
            "     Useful for testing difficulty configs.",
    };

    @Override
    public String getName() {
        return "progdiff";
    }

    @Override
    public String getUsage(ICommandSender sender) {
        return "\"progdiff [sync|killmodified]\"";
    }

    @Override
    public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException {
        if(args.length==0 || args.length>1){
            sendChat(sender,usage);
        }
        if(args[0].equalsIgnoreCase("sync")){
            DifficultyConfiguration.syncConfig();
            sendChat(sender, new String[]{"Synced config."});
        }else if(args[0].equalsIgnoreCase("killmodified")){
            sendChat(sender, new String[]{"Killing all modified mobs in this dimension."});
            MobNBTHandler.getModifiedEntities(sender.getEntityWorld()).stream().forEach(mob->{
                mob.setDead();
            });
        }else{
            sendChat(sender,usage);
        }
    }

    private void sendChat(ICommandSender sender, String[] msg){
        EntityPlayer player = (EntityPlayer)sender.getCommandSenderEntity();
        for (String str : msg)
        {
            TextComponentString line = new TextComponentString(str);
            player.sendMessage(line);
        }
    }

    @Override
    public List<String> getTabCompletions(MinecraftServer server, ICommandSender sender, String[] args, @Nullable BlockPos targetPos) {
        if (args.length == 1) {
            String[] validCompletions = new String[]{
                    "sync",
                    "killmodified"
            };
            return CommandBase.getListOfStringsMatchingLastWord(args, validCompletions);
        }
        return CommandBase.getListOfStringsMatchingLastWord(args, new String[0]);
    }
}

@@ -121,24 +121,71 @@
 NBTHandler;%0A
+import net.minecraft.advancements.Advancement;%0A
 import net.m
@@ -859,41 +859,166 @@
 es;%0A
-%0Aimport javax.annotation.Nullable
+import org.apache.commons.lang3.builder.Diff;%0A%0Aimport javax.annotation.Nullable;%0Aimport java.io.BufferedWriter;%0Aimport java.io.File;%0Aimport java.io.FileWriter
 ;%0Aim
@@ -1334,17 +1334,16 @@
    %22    
- 
 Useful f
@@ -1375,16 +1375,382 @@
 figs.%22,%0A
+            %22%5C%22progdiff killmodified%5C%22 kill modified mobs in the %22,%0A            %22    same dimension as the player%22,%0A            %22%5C%22progdiff advancements%5C%22 print a list of all loaded%22,%0A            %22    advancements to the configuration directory as%22,%0A            %22    progdiff_advancements.txt%22,%0A            %22    (filters out advancements under minecraft:recipes/*)%22%0A
     %7D;%0A%0A
@@ -1938,16 +1938,29 @@
 modified
+%7Cadvancements
 %5D%5C%22%22;%0A  
@@ -2180,24 +2180,20 @@
        %7D
-%0A       
+else
  if(args
@@ -2625,24 +2625,1424 @@
         %7D);%0A
+        %7Delse if(args%5B0%5D.equalsIgnoreCase(%22advancements%22))%7B%0A            File configDir = DifficultyConfiguration.config.getConfigFile().getParentFile();%0A            File advancementsFile = new File(configDir,%22progdiff_advancements.txt%22);%0A            try(BufferedWriter writer = new BufferedWriter(new FileWriter(advancementsFile)))%7B%0A                for(Advancement advancement : sender.getServer().getAdvancementManager().getAdvancements())%7B%0A                    String id = advancement.getId().toString();%0A                    if(!id.startsWith(%22minecraft:recipes%22)) %7B%0A                        writer.write(advancement.getId().toString());%0A                        writer.newLine();%0A                    %7D%0A                %7D%0A            %7Dcatch(Exception e)%7B%0A                sendChat(sender, new String%5B%5D%7B%0A                        %22There was a problem writing the advancements file.%22,%0A                %7D);%0A                if(advancementsFile.exists())%7B%0A                    try %7B%0A                        advancementsFile.delete();%0A                    %7Dcatch(Exception e2)%7B%0A                        sendChat(sender, new String%5B%5D%7B%0A                                %22There was a problem deleting the broken file.%22,%0A                                %22Please manually delete the %5C%22progdiff_advancements.txt%5C%22 file before trying again.%22%0A                        %7D);%0A                    %7D%0A                %7D%0A            %7D%0A%0A
         %7Dels
@@ -4701,16 +4701,52 @@
 odified%22
+,%0A                    %22advancements%22
 %0A       

create new console command for printing advancements