/*******************************************************************************
 * Copyright (c) 2016 - 2018
 *
 * 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.movable.strategies.trading;

import java8.util.Optional;
import java8.util.stream.Stream;
import jsettlers.common.material.EMaterialType;
import jsettlers.logic.buildings.trading.MarketBuilding;
import jsettlers.logic.movable.Movable;
import jsettlers.logic.movable.strategies.trading.ITradeBuilding.MaterialTypeWithCount;

/**
 *
 * @author Andreas Eberle
 *
 */
public class DonkeyStrategy extends TradingStrategy {
	private static final short WAYPOINT_SEARCH_RADIUS = 20;

	private Optional<EMaterialType> materialType1;
	private Optional<EMaterialType> materialType2;

	public DonkeyStrategy(Movable movable) {
		super(movable);
	}

	protected boolean loadUp(ITradeBuilding tradeBuilding) {
		Optional<EMaterialType> materialType1 = tradeBuilding.tryToTakeMaterial(1).map(MaterialTypeWithCount::getMaterialType);
		Optional<EMaterialType> materialType2 = tradeBuilding.tryToTakeMaterial(1).map(MaterialTypeWithCount::getMaterialType);

		if (!materialType1.isPresent() && !materialType2.isPresent()) {
			reset();
			return false;

		} else {
			super.setMaterial(EMaterialType.BASKET);

			this.materialType1 = materialType1;
			this.materialType2 = materialType2;

			return true;
		}
	}


	protected void dropMaterialIfPossible() {
		if (movable.getMaterial() != EMaterialType.NO_MATERIAL) {
			if (materialType1.isPresent()) {
				super.getGrid().dropMaterial(movable.getPosition(), materialType1.get(), true, true);
				materialType1 = Optional.empty();
			}
			if (materialType2.isPresent()) {
				super.getGrid().dropMaterial(movable.getPosition(), materialType2.get(), true, true);
				materialType2 = Optional.empty();
			}
			super.setMaterial(EMaterialType.NO_MATERIAL);
		}
	}

	protected Stream<MarketBuilding> getTradersWithWork() {
		return MarketBuilding.getAllMarkets(movable.getPlayer());
	}

	@Override
	protected boolean isAttackable() {
		return getState() == ETraderState.GOING_TO_TARGET;
	}

	@Override
	protected boolean receiveHit() {
		if (getState() == ETraderState.GOING_TO_TARGET) {
			reset();
			super.abortPath();
		}
		return false;
	}

	@Override
	protected short getWaypointSearchRadius() {
		return WAYPOINT_SEARCH_RADIUS;
	}
}

@@ -1744,33 +1744,24 @@
 ;%0A%0A%09private 
-Optional%3C
 EMaterialTyp
@@ -1753,33 +1753,32 @@
 te EMaterialType
-%3E
  materialType1;%0A
@@ -1786,25 +1786,16 @@
 private 
-Optional%3C
 EMateria
@@ -1791,33 +1791,32 @@
 te EMaterialType
-%3E
  materialType2;%0A
@@ -2372,16 +2372,29 @@
 ialType1
+.orElse(null)
 ;%0A%09%09%09thi
@@ -2420,24 +2420,37 @@
 aterialType2
+.orElse(null)
 ;%0A%0A%09%09%09return
@@ -2588,28 +2588,24 @@
 ialType1
-.isPresent()
+ != null
 ) %7B%0A%09%09%09%09
@@ -2669,22 +2669,16 @@
 ialType1
-.get()
 , true, 
@@ -2704,32 +2704,20 @@
 Type1 = 
-Optional.empty()
+null
 ;%0A%09%09%09%7D%0A%09
@@ -2731,36 +2731,32 @@
 aterialType2
-.isPresent()
+ != null
 ) %7B%0A%09%09%09%09supe
@@ -2820,14 +2820,8 @@
 ype2
-.get()
 , tr
@@ -2855,24 +2855,12 @@
 2 = 
-Optional.empty()
+null
 ;%0A%09%09

Replace optional attribute since optional is not serializable