package me.jacobcrofts.simplestructureloader.util;

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

import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

public class Selection {
	
	private Location left;
	private Location right;
	private List<Block> blocks;
	
	public Selection() {
		this.blocks = new ArrayList<Block>();
	}
	
	public Location getLeftClickLocation() {
		return this.left;
	}
	
	public Location getRightClickLocation() {
		return this.right;
	}
	
	public void setLeftClickLocation(Location location) {
		this.left = location;
	}
	
	public void setRightClickLocation(Location location) {
		this.right = location;
	}
	
	public void save() {
		List<Block> blocks = new ArrayList<Block>();
		
		int xMin = Math.min(left.getBlockX(), right.getBlockX());
		int xMax = Math.max(left.getBlockX(), right.getBlockX());
		int yMin = Math.min(left.getBlockY(), right.getBlockY());
		int yMax = Math.max(left.getBlockY(), right.getBlockY());
		int zMin = Math.min(left.getBlockZ(), right.getBlockZ());
		int zMax = Math.max(left.getBlockZ(), right.getBlockZ());
		
		World world = left.getWorld();
		
		for (int x = xMin; x <= xMax; x++) {
			for (int y = yMin; y <= yMax; y++) {
				for (int z = zMin; z <= zMax; z++) {
					Location l = new Location(world, x, y, z);
					blocks.add(l.getBlock());
				}
			}
		}
		
		this.blocks = blocks;
	}
	
	public List<Block> getBlocks() {
		return this.blocks;
	}
	
	@SuppressWarnings({ "deprecation", "unchecked" })
	public JSONArray toJSON() {
		JSONArray blockDataArray = new JSONArray();
		
		List<Block> blocks = this.getBlocks();
		
		for (Block block : blocks) {
			JSONObject blockJSON = new JSONObject();
			blockJSON.put("x", block.getLocation().getBlockX());
			blockJSON.put("y", block.getLocation().getBlockY());
			blockJSON.put("z", block.getLocation().getBlockZ());
			blockJSON.put("type", block.getType().toString());
			blockJSON.put("data", block.getData());
			blockDataArray.add(blockJSON);
		}
		
		return blockDataArray;
	}
	
	public void saveFromJSON(JSONObject json) {
		// TODO
	}

}

@@ -154,39 +154,8 @@
 ld;%0A
-import org.bukkit.block.Block;%0A
 impo
@@ -306,32 +306,42 @@
 rivate List%3C
+Saved
 Block%3E 
-b
+savedB
 locks;%0A%09%0A%09pu
@@ -358,33 +358,38 @@
 tion() %7B%0A%09%09this.
-b
+savedB
 locks = new Arra
@@ -386,32 +386,37 @@
 = new ArrayList%3C
+Saved
 Block%3E();%0A%09%7D%0A%09%0A%09
@@ -741,32 +741,37 @@
 save() %7B%0A%09%09List%3C
+Saved
 Block%3E blocks = 
@@ -780,24 +780,29 @@
 w ArrayList%3C
+Saved
 Block%3E();%0A%09%09
@@ -1385,16 +1385,31 @@
 cks.add(
+new SavedBlock(
 l.getBlo
@@ -1413,16 +1413,17 @@
 Block())
+)
 ;%0A%09%09%09%09%7D%0A
@@ -1437,25 +1437,30 @@
 %7D%0A%09%09%0A%09%09this.
-b
+savedB
 locks = bloc
@@ -1477,24 +1477,29 @@
 public List%3C
+Saved
 Block%3E getBl
@@ -1496,16 +1496,21 @@
 ock%3E get
+Saved
 Blocks()
@@ -1526,17 +1526,22 @@
 rn this.
-b
+savedB
 locks;%0A%09
@@ -1568,23 +1568,8 @@
 gs(%7B
- %22deprecation%22,
  %22un
@@ -1665,16 +1665,21 @@
 %0A%09%09List%3C
+Saved
 Block%3E b
@@ -1690,24 +1690,29 @@
 s = this.get
+Saved
 Blocks();%0A%09%09
@@ -1719,16 +1719,21 @@
 %0A%09%09for (
+Saved
 Block bl
@@ -1823,35 +1823,24 @@
 lock.get
-Location().getBlock
+Relative
 X());%0A%09%09
@@ -1868,35 +1868,24 @@
 lock.get
-Location().getBlock
+Relative
 Y());%0A%09%09
@@ -1917,27 +1917,16 @@
 .get
-Location().getBlock
+Relative
 Z())

Add feature: Selection saves BlockData rather than literal blocks