package biz.plexers.sparrow.core;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import biz.plexers.sparrow.db.Arggg;
import biz.plexers.sparrow.db.DbHelper;
import biz.plexers.sparrow.db.DbManager;
import biz.plexers.sparrow.sp.ResourceMarket;
import biz.plexers.sparrow.sp.ShipMarket;

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 = Pirate.Serializer.class)
public class Pirate extends Arggg {

	private double experience;
	private ResourcesManager resourcesManager;
	private Ship ship;
	private String name;
	private double gold;

	public boolean changeGoldBy(double offset) {
		if (this.gold + offset > 0) {
			this.gold += offset;
			return true;
		}
		return false;
	}

	public boolean buyShip(ShipMarket shipMarket, int shipIndex)
			throws Exception {
		List<Ship> ships = shipMarket.getShips();
		Ship ship = ships.get(shipIndex);

		if (this.ship == null) {
			double dept = -ship.getGoldValue();
			if (this.changeGoldBy(dept)) {
				this.ship = ship;
				DbManager.savePirate();
				return true;
			}
			throw new Exception("Error! Insufficient gold to buy this ship!");
		}
		throw new Exception("Error! You already have a ship!");
	}

	public boolean buyResources(ResourceMarket resourceMarket) throws Exception {

		if (pay(resourceMarket.getTotalCost())) {
			this.resourcesManager.consume(resourceMarket.getResourcesManager());
			return true;
		} else {
			throw new Exception("Insufficient gold!");
		}

	}

	public boolean pay(double money) {

		return changeGoldBy(-money);
	}

	public boolean hasShip() {
		return ship != null;
	}

	public double getExperience() {
		return experience;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public double getGold() {
		return gold;
	}

	@SuppressWarnings("unchecked")
	private Pirate(Map<String, Object> props) {
		experience = (double) props.get("experience");
		name = (String) props.get("name");
		gold = (double) props.get("gold");
		Map<String, Object> mapShip = (Map<String, Object>) props.get("ship");
		ship = DbHelper.mapAsObject(mapShip, Ship.class);
		resourcesManager = (ResourcesManager) props.get("resourcesManager");
	}

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

	public static class Serializer extends JsonSerializer<Pirate> {

		@Override
		public void serialize(Pirate value, JsonGenerator jgen,
				SerializerProvider provider) throws IOException,
				JsonProcessingException {
			jgen.writeStartObject();
			
			jgen.writeNumberField("experience", value.experience);
			jgen.writeStringField("name", value.name);
			jgen.writeNumberField("gold", value.gold);
			jgen.writeObjectField("ship", value.ship);
			jgen.writeObjectField("resourceManager", value.resourcesManager);
			jgen.writeEndObject();

		}

	}

}

@@ -1737,16 +1737,44 @@
 er());%0D%0A
+%09%09%09DbManager.savePirate();%0D%0A
 %09%09%09retur
@@ -1782,16 +1782,16 @@
  true;%0D%0A
-
 %09%09%7D else
@@ -2550,16 +2550,77 @@
 ss);%0D%0A%09%09
+Map%3CString, Object%3E mapRM = (Map%3CString, Object%3E) props.get(%22
 resource
@@ -2631,13 +2631,16 @@
 ager
- = (R
+%22);%0D%0A%09%09r
 esou
@@ -2650,30 +2650,48 @@
 sManager
-) props.get(%22r
+ = DbHelper.mapAsObject(mapRM, R
 esources
@@ -2693,25 +2693,30 @@
 urcesManager
-%22
+.class
 );%0D%0A%09%7D%0D%0A%0D%0A%09@
@@ -3278,24 +3278,24 @@
 lue.ship);%0D%0A
-
 %09%09%09jgen.writ
@@ -3316,16 +3316,17 @@
 resource
+s
 Manager%22

Add pirate save after resources buy