/*******************************************************************************
 * Copyright (c) 2015
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *******************************************************************************/
package jsettlers.logic.buildings.military;

import java.util.List;

import jsettlers.common.buildings.EBuildingType;
import jsettlers.common.mapobject.EMapObjectType;
import jsettlers.common.material.EMaterialType;
import jsettlers.common.movable.EMovableType;
import jsettlers.common.position.ShortPoint2D;
import jsettlers.logic.buildings.WorkAreaBuilding;
import jsettlers.logic.map.grid.partition.manager.manageables.interfaces.IBarrack;
import jsettlers.common.player.EManaType;
import jsettlers.logic.player.ManaInformation;
import jsettlers.logic.player.Player;
import jsettlers.logic.stack.IRequestStackListener;
import jsettlers.logic.stack.RequestStack;

/**
 * This is the barrack building. It requests weapons and bearers to make them to soldiers.
 * 
 * @author Andreas Eberle
 */
public final class Barrack extends WorkAreaBuilding implements IBarrack, IRequestStackListener {
	private static final long serialVersionUID = -6541972855836598068L;
	private ManaInformation manaInformation;

	public Barrack(Player player) {
		super(EBuildingType.BARRACK, player);
		manaInformation = player.getManaInformation();
	}

	@Override
	protected EMapObjectType getFlagType() {
		return EMapObjectType.FLAG_DOOR;
	}

	@Override
	public EMovableType popWeaponForBearer() {
		List<RequestStack> stacks = super.getStacks();
		for (RequestStack stack : stacks) {
			if (stack.getMaterialType() == EMaterialType.BOW || stack.getMaterialType() == EMaterialType.SWORD
					|| stack.getMaterialType() == EMaterialType.SPEAR) {
				if (stack.pop()) {
					return getSoldierType(stack.getMaterialType());
				}
			}
		}

		return null;
	}

	private EMovableType getSoldierType(EMaterialType materialType) {
		switch (materialType) {
		case SWORD:
			return manaInformation.getMovableTypeOf(EManaType.SWORDSMEN);
		case BOW:
			return manaInformation.getMovableTypeOf(EManaType.BOWMEN);
		case SPEAR:
			return manaInformation.getMovableTypeOf(EManaType.PIKEMEN);
		default:
			return null;
		}
	}

	@Override
	public final boolean isOccupied() {
		return true;
	}

	@Override
	public void bearerRequestFailed() {
		super.getGrid().requestSoilderable(this);
	}

	@Override
	protected int constructionFinishedEvent() {
		for (RequestStack curr : super.getStacks()) {
			curr.setListener(this);
		}
		return -1;
	}

	@Override
	protected int subTimerEvent() {
		assert false : "This should never be called.";
		return -1;
	}

	@Override
	public ShortPoint2D getSoldierTargetPosition() {
		return super.getWorkAreaCenter();
	}

	@Override
	public void materialDelivered(RequestStack stack) {
		getGrid().requestSoilderable(Barrack.this);
	}
}

@@ -2196,50 +2196,8 @@
 68L;
-%0A%09private ManaInformation manaInformation;
 %0A%0A%09p
@@ -2271,57 +2271,8 @@
 r);%0A
-%09%09manaInformation = player.getManaInformation();%0A
 %09%7D%0A%0A
@@ -2885,33 +2885,48 @@
 WORD:%0A%09%09%09return 
-m
+getPlayer().getM
 anaInformation.g
@@ -2915,32 +2915,34 @@
 tManaInformation
+()
 .getMovableTypeO
@@ -2979,33 +2979,48 @@
  BOW:%0A%09%09%09return 
-m
+getPlayer().getM
 anaInformation.g
@@ -3009,32 +3009,34 @@
 tManaInformation
+()
 .getMovableTypeO
@@ -3080,17 +3080,32 @@
 %09return 
-m
+getPlayer().getM
 anaInfor
@@ -3110,16 +3110,18 @@
 ormation
+()
 .getMova

remove member to reduce memory consumption