/*******************************************************************************
 * 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.player;

import jsettlers.common.movable.EMovableType;

import java.io.Serializable;

import static jsettlers.common.movable.EMovableType.*;

/**
 * @author codingberlin
 */
public class ManaInformation implements Serializable {

	private short mana = 0;
	private short numberOfUpgradesExecuted = 0;

	private byte[] levelOfTypes = {0, 0, 0};
	private int[] bowmenTypes = {EMovableType.BOWMAN_L1.ordinal(), EMovableType.BOWMAN_L2.ordinal(), EMovableType.BOWMAN_L3.ordinal()};
	private int[] swordsmenTypes = {EMovableType.SWORDSMAN_L1.ordinal(), EMovableType.SWORDSMAN_L2.ordinal(), EMovableType.SWORDSMAN_L3.ordinal()};
	private int[] pikemenTypes = {EMovableType.PIKEMAN_L1.ordinal(), EMovableType.PIKEMAN_L2.ordinal(), EMovableType.PIKEMAN_L3.ordinal()};
	private static final byte MAXIMUM_LEVEL = 2;
	private static final short[] NECESSARY_MANA_FOR_UPGRADE = {10, 30, 60, 110, 170, 200};

	public void increaseMana() {
		mana++;
	}

	public boolean isUpgradePossible(EManaType type) {
		return getLevel(type) != MAXIMUM_LEVEL
				&& isManaAvailableForUpgrade()
				&& noLevelBelow(getLevel(type));
	}

	private boolean noLevelBelow(byte level) {
		for (byte levelOfType : levelOfTypes) {
			if (levelOfType < level) {
				return false;
			}
		}
		return true;
	}

	private byte getLevel(EManaType type) {
		return levelOfTypes[type.ordinal()];
	}

	public void upgrade(EManaType type) {
		if (isUpgradePossible(type)) {
			numberOfUpgradesExecuted++;
			levelOfTypes[type.ordinal()]++;
		}
	}

	public EMovableType getMovableTypeOf(EManaType type) {
		switch (type) {
		case BOWMEN:
			return EMovableType.values[bowmenTypes[getLevel(type)]];
		case SWORDSMEN:
			return EMovableType.values[swordsmenTypes[getLevel(type)]];
		default:
			return EMovableType.values[pikemenTypes[getLevel(type)]];
		}
	}

	private boolean isManaAvailableForUpgrade() {
		return mana >= NECESSARY_MANA_FOR_UPGRADE[numberOfUpgradesExecuted];
	}

}

@@ -1608,19 +1608,28 @@
 private 
-int
+EMovableType
 %5B%5D bowme
@@ -1656,34 +1656,24 @@
 pe.BOWMAN_L1
-.ordinal()
 , EMovableTy
@@ -1680,34 +1680,24 @@
 pe.BOWMAN_L2
-.ordinal()
 , EMovableTy
@@ -1704,34 +1704,24 @@
 pe.BOWMAN_L3
-.ordinal()
 %7D;%0A%09private 
@@ -1720,19 +1720,28 @@
 private 
-int
+EMovableType
 %5B%5D sword
@@ -1774,34 +1774,24 @@
 SWORDSMAN_L1
-.ordinal()
 , EMovableTy
@@ -1801,34 +1801,24 @@
 SWORDSMAN_L2
-.ordinal()
 , EMovableTy
@@ -1828,34 +1828,24 @@
 SWORDSMAN_L3
-.ordinal()
 %7D;%0A%09private 
@@ -1848,11 +1848,20 @@
 ate 
-int
+EMovableType
 %5B%5D p
@@ -1898,26 +1898,16 @@
 KEMAN_L1
-.ordinal()
 , EMovab
@@ -1923,26 +1923,16 @@
 KEMAN_L2
-.ordinal()
 , EMovab
@@ -1948,26 +1948,16 @@
 KEMAN_L3
-.ordinal()
 %7D;%0A%09priv
@@ -2790,36 +2790,16 @@
 %09return 
-EMovableType.values%5B
 bowmenTy
@@ -2813,25 +2813,24 @@
 Level(type)%5D
-%5D
 ;%0A%09%09case SWO
@@ -2847,36 +2847,16 @@
 %09return 
-EMovableType.values%5B
 swordsme
@@ -2873,25 +2873,24 @@
 Level(type)%5D
-%5D
 ;%0A%09%09default:
@@ -2904,28 +2904,8 @@
 urn 
-EMovableType.values%5B
 pike
@@ -2928,17 +2928,16 @@
 l(type)%5D
-%5D
 ;%0A%09%09%7D%0A%09%7D

make code shorter