package mcjty.rftoolsdim.commands;

import mcjty.rftoolsdim.dimensions.RfToolsDimensionManager;
import mcjty.rftoolsdim.dimensions.description.DimensionDescriptor;
import mcjty.rftoolsdim.dimensions.dimlets.DimletKey;
import mcjty.rftoolsdim.dimensions.dimlets.types.DimletType;
import mcjty.rftoolsdim.dimensions.types.TerrainType;
import net.minecraft.command.ICommandSender;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.util.text.TextFormatting;

import java.util.ArrayList;
import java.util.List;

public class CmdCreateDimension extends AbstractRfToolsCommand {
    @Override
    public String getHelp() {
        return "<Name> <Terrain>";
    }

    @Override
    public String getCommand() {
        return "createdim";
    }

    @Override
    public int getPermissionLevel() {
        return 3;
    }

    @Override
    public boolean isClientSide() {
        return false;
    }

    @Override
    public void execute(ICommandSender sender, String[] args) {
        if (!(sender instanceof EntityPlayer)) {
            sender.sendMessage(new TextComponentString(TextFormatting.RED + "You must be a player to use this command!"));
            return;
        }
        EntityPlayer player = (EntityPlayer) sender;

        if (args.length < 3) {
            player.sendStatusMessage(new TextComponentString(TextFormatting.RED + "The name and terrain parameters are missing!"), false);
            return;
        } else if (args.length > 3) {
            player.sendStatusMessage(new TextComponentString(TextFormatting.RED + "Too many parameters!"), false);
            return;
        }

        String name = fetchString(player, args, 1, "");

        String terrainName = fetchString(player, args, 2, "Void");
        TerrainType terrainType = TerrainType.getTerrainById(terrainName);
        if (terrainType == null) {
            player.sendStatusMessage(new TextComponentString(TextFormatting.RED + "Unknown terrain type!"), false);
            return;
        }

        RfToolsDimensionManager dimensionManager = RfToolsDimensionManager.getDimensionManager(player.getEntityWorld());
        List<DimletKey> descriptors = new ArrayList<>();
        descriptors.add(new DimletKey(DimletType.DIMLET_TERRAIN, terrainType.getId()));
        DimensionDescriptor descriptor = new DimensionDescriptor(descriptors, 0);
        int dim = dimensionManager.createNewDimension(player.getEntityWorld(), descriptor, name, player.getDisplayName().toString(), player.getPersistentID());
        player.sendStatusMessage(new TextComponentString(TextFormatting.GREEN + "Created dimension: " + dim), false);

        dimensionManager.save(player.getEntityWorld());
    }
}

@@ -708,17 +708,35 @@
 me%3E 
-%3C
+%5B
 Terrain
-%3E
+%7CDescriptor%5D %5BSeed%5D
 %22;%0A 
@@ -1339,17 +1339,17 @@
 ength %3C 
-3
+2
 ) %7B%0A    
@@ -1440,20 +1440,8 @@
 ame 
-and terrain 
 para
@@ -1449,13 +1449,11 @@
 eter
-s are
+ is
  mis
@@ -1522,17 +1522,17 @@
 ength %3E 
-3
+4
 ) %7B%0A    
@@ -1797,16 +1797,200 @@
 Void%22);%0A
+        List%3CDimletKey%3E descriptors;%0A        if(terrainName.charAt(0) == '@') %7B%0A            descriptors = DimensionDescriptor.parseDescriptionString(terrainName);%0A        %7D else %7B%0A    
         
@@ -2060,24 +2060,28 @@
 e);%0A        
+  
+  
 if (terrainT
@@ -2091,24 +2091,28 @@
  == null) %7B%0A
+    
             
@@ -2219,32 +2219,36 @@
 e);%0A            
+    
 return;%0A        
@@ -2251,155 +2251,25 @@
     
-%7D%0A%0A
     
+%7D%0A
     
-RfToolsDimensionManager dimensionManager = RfToolsDimensionManager.getDimensionManager(player.getEntityWorld());%0A        List%3CDimletKey%3E
+       
  des
@@ -2295,19 +2295,24 @@
 yList%3C%3E(
+1
 );%0A
+    
         
@@ -2387,24 +2387,156 @@
 .getId()));%0A
+        %7D%0A%0A        RfToolsDimensionManager dimensionManager = RfToolsDimensionManager.getDimensionManager(player.getEntityWorld());%0A
         Dime
@@ -2601,17 +2601,44 @@
 iptors, 
-0
+fetchInt(player, args, 3, 0)
 );%0A     

Allow specifying a descriptor to createdim