package de.siphalor.was.visual.jfx.controller;

import de.siphalor.was.WhatAStorage;
import de.siphalor.was.content.lang.I18n;
import de.siphalor.was.game.Balance;
import de.siphalor.was.game.Transaction;
import de.siphalor.was.visual.jfx.util.BalanceEntry;
import javafx.collections.ObservableList;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;

public class BalanceController {
	public Label totalIncomeLabel;
	public Label totalLossLabel;
	public TableView<BalanceEntry> balanceHistoryTable;
	public LineChart<NumberAxis, NumberAxis> budgetChart;
	public XYChart.Series<Integer, Integer> budgetChartSeries = new XYChart.Series<>();

	public BalanceController() {
		budgetChartSeries.setName(I18n.getInstance().getString("game.balance.chart.line"));
	}

	public void init() {
		Balance balance = WhatAStorage.getInstance().getBalance();

		ObservableList<TableColumn<BalanceEntry, ?>> columns = balanceHistoryTable.getColumns();
		columns.get(0).setCellValueFactory(new PropertyValueFactory<>("index"));
		columns.get(1).setCellValueFactory(new PropertyValueFactory<>("change"));
		columns.get(2).setCellValueFactory(new PropertyValueFactory<>("description"));
		columns.get(3).setCellValueFactory(new PropertyValueFactory<>("product"));

		budgetChart.getData().clear();
		//noinspection unchecked
		budgetChart.getData().add((XYChart.Series<NumberAxis, NumberAxis>)(Object) budgetChartSeries);
		ObservableList<XYChart.Data<Integer, Integer>> chartItems = budgetChartSeries.getData();
		chartItems.add(new XYChart.Data<>(0, 0));

		ObservableList<BalanceEntry> tableItems = balanceHistoryTable.getItems();
		tableItems.clear();

		I18n i18n = I18n.getInstance();

		int i = 1;
		int budget = 0;

		for (Transaction entry : balance.getHistory()) {
			budget += entry.getChange();

			tableItems.add(new BalanceEntry(i, entry.getChange(), i18n.getString(entry.getType().getTranslationKey()), i18n.getString(entry.getProductKey())));

			chartItems.add(new XYChart.Data<>(i, budget));
			i++;
		}

		updateTotalValues(balance.getTotalIncome(), balance.getTotalLoss(), i18n);
	}

	public void onBalanceChanged(int budget, Transaction transaction, int totalIncome, int totalLoss) {
		I18n i18n = I18n.getInstance();

		updateTotalValues(totalIncome, totalLoss, i18n);

		if (transaction.getType() != Transaction.Type.NOOP) {
			ObservableList<BalanceEntry> items = balanceHistoryTable.getItems();
			items.add(new BalanceEntry(items.size(), transaction.getChange(), i18n.getString(transaction.getType().getTranslationKey()), i18n.getString(transaction.getProductKey())));
			balanceHistoryTable.sort();
		}

		ObservableList<XYChart.Data<Integer, Integer>> data = budgetChartSeries.getData();

		data.add(new XYChart.Data<>(data.size(), budget));
	}

	private void updateTotalValues(int totalIncome, int totalLoss, I18n i18n) {
		totalIncomeLabel.setText(i18n.format("game.balance.total-income", totalIncome));
		totalLossLabel.setText(i18n.format("game.balance.total-loss", totalLoss));
	}
}
