package control;

import java.util.ArrayList;

import boundary.GUIBoundary;
import entity.GameBoard;
import entity.Player;
import entity.PlayerList;
import entity.fields.Field;
import entity.language.LanguageHandler;

/**
 *
 */
public abstract class SequenceController {

    /**
     *
     */
    public static void buildSequence(Player player, GameBoard gameBoard, GUIBoundary boundary) {
        int i = 0;
        ArrayList<Field> buildableList = gameBoard.getBuildableList(player);
        String[] buildableLabels = new String[gameBoard.getBuildableList(player).size()];
        for (Field field : buildableList) {
            buildableLabels[i++] = field.getName();
        }
        if (buildableLabels.length == 0) {
            boundary.getButtonPressed("You have no buildable properties", "Ok!");
        } else {
            Field fieldToBuildOn = gameBoard.getField(gameBoard.getIndexByName(boundary.getUserSelection("Choose plot to build on", buildableLabels)));
        }
    }

	/**
	 *
	 */
	public static void demolitionSequence(Player player, GameBoard gameBoard, GUIBoundary boundary) {
		ArrayList<Field> demolitionableList = gameBoard.getDemolitionableList(player);
		String[] demolitionableLabels = new String[gameBoard.getDemolitionableList(player).size()];
		for (int i = 0; i < demolitionableLabels.length; i++) {
			demolitionableLabels[i] = demolitionableList.get(i).getName();
		}
		if (demolitionableLabels.length == 0)
		{
			boundary.getButtonPressed("You have no demolitionable properties", "Ok!");
		} else
		{
			String fieldToDemolishOn = boundary.getUserSelection("Choose plot to demolish on", demolitionableLabels);
			gameBoard.getField(gameBoard.getIndexByName(fieldToDemolishOn)).sellConstruction();
		}
	}

	/**
	 *
	 */
	public static void tradePropertiesSequence(Player owner, GameBoard gameBoard, GUIBoundary boundary, PlayerList playerList) {
		int i = 0;
		ArrayList<Field> sellableList = gameBoard.getPropertyList(owner);
		String[] sellableLabels = new String[sellableList.size()];
		String[] playerLabels = new String[playerList.getPlayers().length - 1];
		Field fieldToSellObject;
		Player buyerObject;

		for (Field field : sellableList) {
			sellableLabels[i++] = field.getName();
		}
		if (sellableLabels.length == 0) {
			boundary.getButtonPressed("Du har ingen felter at sælge", "Ok!");
		} else {
			i = 0;
			for (Player player : playerList.getPlayers()) {
				if (!player.getName().equals(owner.getName())) {
					playerLabels[i] = player.getName();
					i++;
				}
			}

			String fieldToSell = boundary.getUserSelection("Choose plot to trade", sellableLabels);
			String buyer = boundary.getUserSelection("Choose who is buying", playerLabels);

			fieldLoop:
				for (Field field : sellableList) {
					if (fieldToSell.equals(field.getName())) {
						fieldToSellObject = field;
						for (Player player : playerList.getPlayers()) {
							if (buyer.equals(player.getName())) {
								buyerObject = player;
								int price = boundary.getInteger("Which price?", 0, 30000);
								fieldToSellObject.tradeField(owner, buyerObject, price);
								boundary.setOwner(field.getID(), buyerObject.getName());
								break fieldLoop;
							}
						}
					}
				}
		}
	}

            fieldLoop:
            for (Field field : sellableList) {
                if (fieldToSell.equals(field.getName())) {
                    fieldToSellObject = field;
                    for (Player player : playerList.getPlayers()) {
                        if (buyer.equals(player.getName())) {
                            buyerObject = player;
                            int price = boundary.getInteger("Which price?", 0, 30000);
                            fieldToSellObject.tradeField(owner, buyerObject, price);
                            boundary.setOwner(field.getID(), buyerObject.getName());
                            boundary.updateBalance(owner.getName(), owner.getBankAccount().getBalance());
                            boundary.updateBalance(buyerObject.getName(), buyerObject.getBankAccount().getBalance());
                            break fieldLoop;
                        }
                    }
                }
            }
        }
    }

	/**
	 *
	 */
	public static void pawnSequence(Player player, GameBoard gameBoard, GUIBoundary boundary) {

	}

	/**
	 *
	 */
	public static void buyPropertySequence(Player player, Field field, GUIBoundary boundary, LanguageHandler language) {
		int priceOfField = field.getPrice();
		if (boundary.getBoolean(language.buyingOfferMsg(priceOfField), language.yes(), language.no())) {
			if (player.getBankAccount().getBalance() > priceOfField) {
				field.buyField(player);
				boundary.updateBalance(player.getName(), player.getBankAccount().getBalance());
				boundary.setOwner(player.getOnField(), player.getName());
				boundary.getButtonPressed(language.purchaseConfirmation());
			} else {
				boundary.getButtonPressed(language.notEnoughMoney());
			}
		}
	}
}

@@ -2705,531 +2705,8 @@
 );%0A%0A
-%09%09%09fieldLoop:%0A%09%09%09%09for (Field field : sellableList) %7B%0A%09%09%09%09%09if (fieldToSell.equals(field.getName())) %7B%0A%09%09%09%09%09%09fieldToSellObject = field;%0A%09%09%09%09%09%09for (Player player : playerList.getPlayers()) %7B%0A%09%09%09%09%09%09%09if (buyer.equals(player.getName())) %7B%0A%09%09%09%09%09%09%09%09buyerObject = player;%0A%09%09%09%09%09%09%09%09int price = boundary.getInteger(%22Which price?%22, 0, 30000);%0A%09%09%09%09%09%09%09%09fieldToSellObject.tradeField(owner, buyerObject, price);%0A%09%09%09%09%09%09%09%09boundary.setOwner(field.getID(), buyerObject.getName());%0A%09%09%09%09%09%09%09%09break fieldLoop;%0A%09%09%09%09%09%09%09%7D%0A%09%09%09%09%09%09%7D%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D%0A%09%09%7D%0A%09%7D%0A%0A
     

Update GUI