package me.jacobcrofts.simplestructureloader.util;

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

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

public class Selection {
	
	private Location leftClickLocation;
	private Location rightClickLocation;
	private List<SavedBlock> savedBlocks;
	
	public Selection() {
		this.savedBlocks = new ArrayList<SavedBlock>();
	}
	
	public Location getLeftClickLocation() {
		return this.leftClickLocation;
	}
	
	public Location getRightClickLocation() {
		return this.rightClickLocation;
	}
	
	public void setLeftClickLocation(Location location) {
		this.leftClickLocation = location;
	}
	
	public void setRightClickLocation(Location location) {
		this.rightClickLocation = location;
	}
	
	public void save() {
		List<SavedBlock> blocks = new ArrayList<SavedBlock>();
		
		int xMin = Math.min(leftClickLocation.getBlockX(), rightClickLocation.getBlockX());
		int xMax = Math.max(leftClickLocation.getBlockX(), rightClickLocation.getBlockX());
		int yMin = Math.min(leftClickLocation.getBlockY(), rightClickLocation.getBlockY());
		int yMax = Math.max(leftClickLocation.getBlockY(), rightClickLocation.getBlockY());
		int zMin = Math.min(leftClickLocation.getBlockZ(), rightClickLocation.getBlockZ());
		int zMax = Math.max(leftClickLocation.getBlockZ(), rightClickLocation.getBlockZ());
		
		World world = leftClickLocation.getWorld();
		Location baseBlock = new Location(world, xMin, yMin, zMin);
		
		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(new SavedBlock(baseBlock, l.getBlock()));
				}
			}
		}
		
		this.savedBlocks = blocks;
	}
	
	public List<SavedBlock> getSavedBlocks() {
		return this.savedBlocks;
	}
	
	@SuppressWarnings({ "unchecked" })
	public JSONArray toJSON() {
		JSONArray blockDataArray = new JSONArray();
		
		for (SavedBlock block : this.savedBlocks) {
			blockDataArray.add(block.toJSON());
		}
		
		return blockDataArray;
	}
	
	public void saveFromJSON(JSONArray json) {
		// TODO
	}

}

@@ -73,16 +73,43 @@
 ayList;%0A
+import java.util.Iterator;%0A
 import j
@@ -123,16 +123,16 @@
 l.List;%0A
-
 %0Aimport 
@@ -463,24 +463,271 @@
 ck%3E();%0A%09%7D%0A%09%0A
+%09@SuppressWarnings(%22unchecked%22)%0A%09public Selection(JSONArray savedBlockData) %7B%0A%09%09this();%0A%09%09Iterator%3CJSONObject%3E iterator = savedBlockData.iterator();%0A%09%09while (iterator.hasNext()) %7B%0A%09%09%09this.savedBlocks.add(new SavedBlock(iterator.next()));%0A%09%09%7D%0A%09%7D%0A%09%0A
 %09public Loca
@@ -1064,32 +1064,32 @@
  location;%0A%09%7D%0A%09%0A
-
 %09public void sav
@@ -1089,16 +1089,32 @@
 oid save
+CurrentSelection
 () %7B%0A%09%09L
@@ -2391,67 +2391,8 @@
 ;%0A%09%7D
-%0A%09%0A%09public void saveFromJSON(JSONArray json) %7B%0A%09%09// TODO%0A%09%7D
 %0A%0A%7D%0A

Add feature: Selection may be initialized with a JSONObject