package ch.sahits.game.graphic.display.dialog;
import ch.sahits.game.graphic.display.model.ICityPlayerProxy;
import ch.sahits.game.openpatrician.model.building.ITradingOffice;
import ch.sahits.game.openpatrician.model.product.IWare;
/**
* This action handles the selling of wares from the storage to the city.
* @author Andi Hotz, (c) Sahits GmbH, 2011
* Created on Dec 24, 2011
*
*/
class Storage2CityAction implements Runnable {
private final IWare ware;
private final ITransferable dialog;
// private static final Logger logger = Logger.getLogger(Storage2CityAction.class.getName());
public Storage2CityAction(IWare ware, ITransferable dialog) {
super();
this.ware = ware;
this.dialog=dialog;
}
@Override
public void run() {
ICityPlayerProxy city = dialog.getCityPlayerProxy();
ITradingOffice office = city.getPlayer().findTradingOffice(city.getCity());
if (office!=null) {
int amountInStorage = office.getWare(ware).getAmount();
if (amountInStorage > 0) {
int availableAmountCity = city.getCity().getWare(ware).getAmount();
int amount2Move = dialog.getAmount(amountInStorage);
final int avgPrice;
if (dialog.getMovableAmount() == ETransferAmount.MAX) {
avgPrice = ware.getMaxValueSell();
} else {
avgPrice = ware.computeSellPrice(availableAmountCity,amount2Move);
}
city.getCity().move(ware, amount2Move,city.getPlayer()); // The price here is irrelevant
int sold = office.move(ware, -amount2Move);
city.getPlayer().updateCash(avgPrice * sold);
} // end stored amount
} // end office
}
}