package com.arckenver.nations.task;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Optional;
import java.util.UUID;
import java.util.regex.Pattern;

import org.spongepowered.api.Sponge;
import org.spongepowered.api.service.economy.account.Account;
import org.spongepowered.api.service.economy.account.UniqueAccount;
import org.spongepowered.api.service.economy.transaction.ResultType;
import org.spongepowered.api.service.economy.transaction.TransactionResult;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.channel.MessageChannel;
import org.spongepowered.api.text.format.TextColors;

import com.arckenver.nations.ConfigHandler;
import com.arckenver.nations.DataHandler;
import com.arckenver.nations.LanguageHandler;
import com.arckenver.nations.NationsPlugin;
import com.arckenver.nations.object.Nation;

public class TaxesCollectRunnable implements Runnable
{
	public void run()
	{
		if (ConfigHandler.getNode("prices").getNode("upkeepPerCitizen").getDouble() == 0 && DataHandler.getNations().values().stream().allMatch(n -> n.getTaxes() == 0))
		{
			return;
		}
		MessageChannel.TO_ALL.send(Text.of(TextColors.AQUA, LanguageHandler.CL));
		ArrayList<UUID> nationsToRemove = new ArrayList<UUID>();
		for (Nation nation : DataHandler.getNations().values())
		{
			if (NationsPlugin.getEcoService() == null)
			{
				NationsPlugin.getLogger().error(LanguageHandler.DC);
				continue;
			}
			Optional<Account> optAccount = NationsPlugin.getEcoService().getOrCreateAccount("nation-" + nation.getUUID().toString());
			if (!optAccount.isPresent())
			{
				NationsPlugin.getLogger().error("Nation " + nation.getName() + " doesn't have an account on the economy plugin of this server");
				continue;
			}
			// nation taxes
			BigDecimal taxes = BigDecimal.valueOf(nation.getTaxes());
			for (UUID uuid : nation.getCitizens())
			{
				if (!nation.isStaff(uuid))
				{
					Optional<UniqueAccount> optCitizenAccount = NationsPlugin.getEcoService().getOrCreateAccount(uuid);
					TransactionResult result = optCitizenAccount.get().withdraw(NationsPlugin.getEcoService().getDefaultCurrency(), taxes, NationsPlugin.getCause());
					if (result.getResult() == ResultType.ACCOUNT_NO_FUNDS)
					{
						nation.removeCitizen(uuid);
						Sponge.getServer().getPlayer(uuid).ifPresent(p ->
									p.sendMessage(Text.of(TextColors.RED, LanguageHandler.HQ)));
					}
					else if (result.getResult() != ResultType.SUCCESS)
					{
						NationsPlugin.getLogger().error("Error while taking taxes from player " + uuid.toString() + " for nation " + nation.getName());
					}
					else
					{
						 TransactionResult res = optAccount.get().deposit(NationsPlugin.getEcoService().getDefaultCurrency(), taxes, NationsPlugin.getCause());
						 if (res.getResult() != ResultType.SUCCESS)
						 {
							 NationsPlugin.getLogger().error("Error while depositing taxes withdrawn from player " + uuid.toString() + " in nation " + nation.getName());
						 }
					}
				}
			}
			// nation upkeep
			BigDecimal upkeep = BigDecimal.valueOf(nation.getUpkeep());
			TransactionResult result = optAccount.get().withdraw(NationsPlugin.getEcoService().getDefaultCurrency(), upkeep, NationsPlugin.getCause());
			if (result.getResult() == ResultType.ACCOUNT_NO_FUNDS)
			{
				nationsToRemove.add(nation.getUUID());
			}
			else if (result.getResult() != ResultType.SUCCESS)
			{
				NationsPlugin.getLogger().error("Error while taking upkeep from nation " + nation.getName());
			}
		}
		for (UUID uuid : nationsToRemove)
		{
			String name = DataHandler.getNation(uuid).getName();
			DataHandler.removeNation(uuid);
			MessageChannel.TO_ALL.send(Text.of(TextColors.RED, LanguageHandler.CM.replaceAll(Pattern.quote("\\{NATION\\}"), name)));
		}
	}
}

@@ -1833,16 +1833,77 @@
 xes());%0A
+%09%09%09ArrayList%3CUUID%3E citizensToRemove = new ArrayList%3CUUID%3E();%0A
 %09%09%09for (
@@ -2311,28 +2311,28 @@
 %09%09%09%09
-nation.removeCitizen
+citizensToRemove.add
 (uui
@@ -3062,24 +3062,104 @@
 %0A%09%09%09%09%7D%0A%09%09%09%7D%0A
+%09%09%09for (UUID uuid : citizensToRemove)%0A%09%09%09%7B%0A%09%09%09%09nation.removeCitizen(uuid);%0A%09%09%09%7D%0A
 %09%09%09// nation

Fix #43