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

import jsettlers.ai.construction.BestConstructionPositionFinderFactory;
import jsettlers.common.buildings.EBuildingType;
import jsettlers.common.position.ShortPoint2D;
import jsettlers.input.tasks.ConstructBuildingTask;
import jsettlers.input.tasks.EGuiAction;
import jsettlers.logic.map.grid.MainGrid;
import jsettlers.network.client.interfaces.ITaskScheduler;

public class WhatToDoAi implements IWhatToDoAi {

	private final MainGrid mainGrid;
	private final byte playerId;
	private final ITaskScheduler taskScheduler;
	private final AiStatistics aiStatistics;
	BestConstructionPositionFinderFactory bestConstructionPositionFinderFactory;

	public WhatToDoAi(byte playerId, AiStatistics aiStatistics, MainGrid mainGrid, ITaskScheduler taskScheduler) {
		this.playerId = playerId;
		this.mainGrid = mainGrid;
		this.taskScheduler = taskScheduler;
		this.aiStatistics = aiStatistics;
		bestConstructionPositionFinderFactory = new BestConstructionPositionFinderFactory();
	}

	@Override
	public void applyRules() {
		long startTime = System.currentTimeMillis();
		int numberOfNotFinishedBuildings = aiStatistics.getNumberOfNotFinishedBuildingsForPlayer(playerId);
		int numberOfTotalStonecutters = aiStatistics.getTotalNumberOfBuildingTypeForPlayer(EBuildingType.STONECUTTER, playerId);
		int numberOfTotalLumberJacks = aiStatistics.getTotalNumberOfBuildingTypeForPlayer(EBuildingType.LUMBERJACK, playerId);
		int numberOfTotalSawMills = aiStatistics.getTotalNumberOfBuildingTypeForPlayer(EBuildingType.SAWMILL, playerId);
		int numberOfTotalForesters = aiStatistics.getTotalNumberOfBuildingTypeForPlayer(EBuildingType.FORESTER, playerId);

		if (numberOfNotFinishedBuildings < 5 && numberOfTotalStonecutters < 1) {
			construct(EBuildingType.STONECUTTER);
		}
		if (numberOfNotFinishedBuildings < 5 && numberOfTotalLumberJacks < 1) {
			construct(EBuildingType.LUMBERJACK);
		}
		if (numberOfNotFinishedBuildings < 5 && numberOfTotalSawMills < 1) {
			construct(EBuildingType.SAWMILL);
		}
		if (numberOfNotFinishedBuildings < 5 && numberOfTotalForesters < 1) {
			construct(EBuildingType.FORESTER);
		}
		if (numberOfNotFinishedBuildings < 5 &&
				numberOfTotalStonecutters < 3 &&
				numberOfTotalLumberJacks >= 1 &&
				numberOfTotalSawMills >= 1 &&
				numberOfTotalForesters >= 1) {
			construct(EBuildingType.STONECUTTER);
		}
		if (numberOfNotFinishedBuildings < 5 &&
				numberOfTotalLumberJacks < 4 &&
				numberOfTotalStonecutters >= 3 &&
				numberOfTotalSawMills >= 1 &&
				numberOfTotalForesters >= 1) {
			construct(EBuildingType.LUMBERJACK);
		}
		if (numberOfNotFinishedBuildings < 5 &&
				numberOfTotalLumberJacks >= 4 &&
				numberOfTotalStonecutters >= 3 &&
				numberOfTotalSawMills < 2 &&
				numberOfTotalForesters >= 1) {
			construct(EBuildingType.SAWMILL);
		}
		System.out.println("WhatToDoAi took " + (System.currentTimeMillis() - startTime) + " ms");
	}

	private void construct(EBuildingType type) {
		ShortPoint2D position = bestConstructionPositionFinderFactory.getBestConstructionPositionFinderFor(type).findBestConstructionPosition(
				aiStatistics, mainGrid.getConstructionMarksGrid(), playerId);
		if (position != null) {
			taskScheduler.scheduleTask(new ConstructBuildingTask(EGuiAction.BUILD, playerId, position, type));
		}
	}

}

@@ -2905,17 +2905,16 @@
 yerId);%0A
-%0A
 %09%09if (nu
@@ -2938,33 +2938,37 @@
 Buildings %3C 5 &&
- 
+%0A%09%09%09%09
 numberOfTotalSto
@@ -3062,33 +3062,37 @@
 Buildings %3C 5 &&
- 
+%0A%09%09%09%09
 numberOfTotalLum
@@ -3184,33 +3184,37 @@
 Buildings %3C 5 &&
- 
+%0A%09%09%09%09
 numberOfTotalSaw
@@ -3220,16 +3220,53 @@
 wMills %3C
+ 1 &&%0A%09%09%09%09numberOfTotalLumberJacks %3E=
  1) %7B%0A%09%09
@@ -3345,17 +3345,21 @@
 s %3C 5 &&
- 
+%0A%09%09%09%09
 numberOf
@@ -3370,24 +3370,133 @@
 lForesters %3C
+ 1 &&%0A%09%09%09%09numberOfTotalLumberJacks %3E= 1 &&%0A%09%09%09%09numberOfTotalStonecutters %3E= 1 &&%0A%09%09%09%09numberOfTotalSawMills %3E=
  1) %7B%0A%09%09%09con

fix start building order because we do not need a forester if stonecutter, lumberjack and sawmill are not there yet. the stone cutter could consume stone and wood which would be needed for the other buildings