RecieptComponent.java

package edu.ntnu.idi.idatt.view.components.elements;

import java.util.List;

import edu.ntnu.idi.idatt.model.transaction.Purchase;
import edu.ntnu.idi.idatt.model.transaction.Sale;
import edu.ntnu.idi.idatt.model.transaction.Transaction;
import edu.ntnu.idi.idatt.service.transaction.PurchaseCalculator;
import edu.ntnu.idi.idatt.service.transaction.SaleCalculator;
import edu.ntnu.idi.idatt.view.components.primitives.ActionEventHandler;
import edu.ntnu.idi.idatt.view.components.ui.UICompositor;
import edu.ntnu.idi.idatt.view.util.CssUtils;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;

/**
 * Reciept component class
 *
 * <p>
 * JavaFX Component for handeling repetative tasks.
 * Creates a reciept based on a transaction.
 * </p>
 * 
 * @see Transaction
 */
public class RecieptComponent extends VBox {

  private Button doneButton;

  /**
   * Constructor for RecieptComponent
   * 
   * @param transaction - transaction object to create a reciept for.
   */
  public RecieptComponent(Transaction transaction) {

    Label title = new Label("Reciept");
    CssUtils.apply(title, CssUtils.BIG_TEXT_32);

    Label operation = new Label();
    String operationString = "Transaction type: %s";

    Label week = new Label(String.format("Week: %s", transaction.getWeek()));

    Label shareName = new Label(String.format("Share: %s", transaction.getShare().getStock().toString()));
    Label shareAmount = new Label(String.format("Amount: %.3f", transaction.getShare().getQuantity()));

    Label gross = new Label();
    String grossString = "Gross: %.2f $";

    Label tax = new Label();
    String taxString = "Taxes: %.2f $";

    Label comission = new Label();
    String comissionString = "Comission: %.2f $";

    Label total = new Label();
    String totalString = "Total: %.2f $";

    Label profits = new Label();

    List<Label> labels = List.of(week, shareName, operation, shareAmount,
        gross, tax, comission, total, profits);

    labels.forEach(l -> CssUtils.apply(l, CssUtils.MED_TEXT_16));

    if (transaction instanceof Purchase) {
      PurchaseCalculator calc = (PurchaseCalculator) transaction.getCalculator();
      operation.setText(String.format(operationString, "Purchase"));
      gross.setText(String.format(grossString, calc.calculateGross()));
      tax.setText(String.format(taxString, calc.calculateTax()));
      comission.setText(String.format(comissionString, calc.calculateCommision()));
      total.setText(String.format(totalString, calc.calculateTotal()));
    } else if (transaction instanceof Sale) {
      SaleCalculator calc = (SaleCalculator) transaction.getCalculator();
      operation.setText(String.format(operationString, "Sale", transaction.getWeek()));
      gross.setText(String.format(grossString, calc.calculateGross()));
      tax.setText(String.format(taxString, calc.calculateTax()));
      comission.setText(String.format(comissionString, calc.calculateCommision()));
      total.setText(String.format(totalString, calc.calculateTotal()));
      profits.setText(String.format("Profits: %.2f $", calc.calculateProfit()));
      CssUtils.apply(profits, CssUtils.generateValueColors(calc.calculateProfit()));
    }

    doneButton = new Button("Alright!");
    doneButton.getStyleClass().add("button");

    UICompositor uiReciept = new UICompositor.Builder()
        .parent(new VBox())
        .growWithAlignment(Pos.CENTER)
        .addAllContent(title,
            operation,
            week,
            new Label(" "), // Small filler
            shareName,
            shareAmount,
            gross,
            tax,
            comission,
            total,
            profits,
            doneButton)
        .build();

    this.getChildren().add(uiReciept.makeUI());
    this.getStyleClass().add("dark");
    this.setStyle("-fx-background-radius: 20;");
    this.setMaxSize(700, 500);
    this.setPrefWidth(700);
    StackPane.setAlignment(this, Pos.CENTER);

  }

  /**
   * Method for handeling exit button click.
   * 
   * @param handler - Functional interface.
   */
  public void onExitButton(ActionEventHandler handler) {
    doneButton.setOnMouseClicked((e) -> handler.handle());
  }

}