package biz.plexers.sparrow.sp;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonCreator;

import biz.plexers.sparrow.core.Pirate;
import biz.plexers.sparrow.core.Ship;
import biz.plexers.sparrow.core.UserManager;
import biz.plexers.sparrow.db.DbManager;

public class ShipMarket extends Market {
	List<Ship> ships;

	public ShipMarket() {
		super();
		ships = Arrays.asList(new Ship[]{new Ship(240, "Marina"), new Ship(400, "Giorgos")});
	}
	private ShipMarket(List<Ship> ships) {
		this.ships = ships;
	}
	@JsonCreator
	public static ShipMarket factory(List<Ship> ships) {
		return new ShipMarket(ships);
	}

	public void disassembleShip() {

	}

	public boolean buyShip(int shipIndex) throws Exception {
		Ship ship = ships.get(shipIndex);
		Pirate pirate = UserManager.getPirate();

		if (pirate.getShip() == null) {
			double dept = - ship.getGoldValue();
			if (pirate.changeGoldBy(dept)) {
				pirate.setShip(ship);
				return true;
			}
			throw new Exception("Error! Insufficient gold to buy this ship!");
		}
		throw new Exception("Error! You already have a ship!");
	}
	
	public static ShipMarket getInstance() {
		//TODO Add implementation
		return (ShipMarket) DbManager.read(ShipMarket.class, "shipMarket");
	}

	public List<Ship> getShips() {
		return Collections.unmodifiableList(ships);
	}
	
	
}

@@ -108,60 +108,28 @@
 st;%0A
-%0A
 import 
-com.fasterxml.jackson.annotation.JsonCreator
+java.util.Map
 ;%0A%0Ai
@@ -292,16 +292,70 @@
 nager;%0A%0A
+import com.fasterxml.jackson.annotation.JsonCreator;%0A%0A
 public c
@@ -441,16 +441,16 @@
 uper();%0A
+
 %09%09ships 
@@ -535,176 +535,8 @@
 ;%0A%09%7D
-%0A%09private ShipMarket(List%3CShip%3E ships) %7B%0A%09%09this.ships = ships;%0A%09%7D%0A%09@JsonCreator%0A%09public static ShipMarket factory(List%3CShip%3E ships) %7B%0A%09%09return new ShipMarket(ships);%0A%09%7D
 %0A%0A%09p
@@ -1231,12 +1231,219 @@
 );%0A%09%7D%0A%09%0A
+%09private ShipMarket(Map%3CString, Object%3E props) %7B%0A%09%09ships = (List%3CShip%3E) props.get(%22ships%22);%0A%09%7D%0A%09@JsonCreator%0A%09public static ShipMarket factory(Map%3CString, Object%3E props) %7B%0A%09%09return new ShipMarket(props);%0A%09%7D%0A
 %09%0A%7D%0A

Improve ShipMarket deserialization method