package de.siphalor.was.game;

import de.siphalor.was.content.product.Product;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

/**
 * Represents the current budget and a transaction history.
 */
public class Balance {
	/**
	 * The current budget
	 */
	private int budget = 0;
	/**
	 * The total money gained
	 */
	private int totalIncome = 0;
	/**
	 * The total money lost
	 */
	private int totalLoss = 0;
	/**
	 * The transaction history
	 */
	private final Queue<Transaction> history = new ConcurrentLinkedQueue<>();

	/**
	 * Adds a new transaction to the history.
	 * @param type The transaction type
	 * @param change The amount of money received/lost
	 * @param product The product that this was about or <code>null</code> if none
	 * @return The added transaction
	 */
	public Transaction add(@NotNull Transaction.Type type, int change, @Nullable Product product) {
		return add(new Transaction(type, change, product != null ? product.getTranslationKey() : ""));
	}

	/**
	 * Adds a new transaction to the history.
	 * @param transaction The transaction
	 * @return the added transaction
	 */
	public Transaction add(@NotNull Transaction transaction) {
		history.add(transaction);

		budget += transaction.getChange();

		if (transaction.getChange() < 0) {
			totalLoss -= transaction.getChange();
		} else {
			totalIncome += transaction.getChange();
		}
		return transaction;
	}

	/**
	 * Gets the complete transaction history.
	 * @return The history
	 */
	@NotNull
	public Queue<Transaction> getHistory() {
		return history;
	}

	/**
	 * Gets the current budget.
	 * @return The current budget
	 */
	public int getBudget() {
		return budget;
	}

	/**
	 * Gets the total amount of money gained.
	 * @return the total income
	 */
	public int getTotalIncome() {
		return totalIncome;
	}

	/**
	 * Gets the total amount of money lost
	 * @return the total loss
	 */
	public int getTotalLoss() {
		return totalLoss;
	}

}
