package biz.plexers.sparrow.core;

import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import biz.plexers.sparrow.core.Resource.Choices;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

@JsonSerialize(using = ResourcesManager.Serializer.class)
public class ResourcesManager {
	
	public ResourcesManager() {
		resources = new HashMap<>();
		resources.put(Choices.Cannons, new Resource(Choices.Cannons));
		resources.put(Choices.Crew, new Resource(Choices.Crew));
		resources.put(Choices.Lumber, new Resource(Choices.Lumber));
		resources.put(Choices.Metal, new Resource(Choices.Metal));
	}

	private HashMap<Resource.Choices, Resource> resources;

	public Map<Resource.Choices, Resource> getResources() {
		return Collections.unmodifiableMap(resources);
	}

	public void changeResourceBy(Resource.Choices resourceType, int offset) {

		Resource tempRes = resources.get(resourceType);

		if (tempRes != null) {
			tempRes.changeQ(offset);
		}
	}

	public void consume(ResourcesManager other) {

		for (Resource.Choices choice : Resource.Choices.values()) {
			Resource thisTempRes = resources.get(choice);
			Resource otherTempRes = resources.get(choice);

			if (thisTempRes != null) {
				thisTempRes.consume(otherTempRes);
			} else {
				resources.put(choice, otherTempRes);
			}
		}
	}

	@SuppressWarnings("unchecked")
	private ResourcesManager(Map<String, Object> props) {
		resources = (HashMap<Resource.Choices, Resource>) props
				.get("resources");
	}

	@JsonCreator
	public static ResourcesManager factory(Map<String, Object> props) {
		return new ResourcesManager(props);
	}

	public static class Serializer extends JsonSerializer<ResourcesManager> {

		@Override
		public void serialize(ResourcesManager value, JsonGenerator jgen,
				SerializerProvider provider) throws IOException,
				JsonProcessingException {
			jgen.writeStartObject();
			jgen.writeObjectField("resources", value.resources);
			jgen.writeEndObject();

		}

	}

}

@@ -192,16 +192,57 @@
 hoices;%0D
+%0Aimport biz.plexers.sparrow.db.DbHelper;%0D
 %0A%0D%0Aimpor
@@ -396,24 +396,79 @@
 Exception;%0D%0A
+import com.fasterxml.jackson.core.type.TypeReference;%0D%0A
 import com.f
@@ -732,11 +732,10 @@
  %7B%0D%0A
-%09
 %0D%0A
+
 %09pub
@@ -1468,18 +1468,84 @@
 her) %7B%0D%0A
+%09%09%0D%0A%09%09HashMap%3CChoices, Resource%3E otherResources = other.resources;
 %0D%0A
-
 %09%09for (R
@@ -1668,33 +1668,38 @@
  otherTempRes = 
-r
+otherR
 esources.get(cho
@@ -1945,21 +1945,168 @@
 %0D%0A%09%09
-resources = (
+Map%3CString, Object%3E mapResources = (Map%3CString, Object%3E) props%0D%0A%09%09%09%09.get(%22resources%22);%0D%0A%09%09resources = DbHelper.mapAsObject(mapResources,%0D%0A%09%09%09%09new TypeReference%3C
 Hash
@@ -2140,37 +2140,20 @@
 rce%3E
-) props%0D%0A%09%09%09%09.get(%22resources%22
+%3E() %7B%0D%0A%09%09%09%09%7D
 );%0D%0A

Fix bugs in ResourceManager