Skip to content
Snippets Groups Projects
RecipeController.java 3.00 KiB
package no.ntnu.idatt1002.demo.controller;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.geometry.Insets;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.GridPane;
import no.ntnu.idatt1002.demo.data.recipes.*;

import java.io.IOException;
import java.net.URL;
import java.util.Comparator;
import java.util.List;
import java.util.ResourceBundle;

public class RecipeController implements Initializable {


    @FXML
    private GridPane recipeTiles;


    private List<Recipe> recipes;

    // For testing purposes at frontend:

    private List<Recipe> getSuggestions() {

        RecipeRegister recipeRegister = new RecipeRegister();
        List<Recipe> suggestedRecipes = null;

        IngredientsAtHand ingredientsAtHand = new IngredientsAtHand();
        ingredientsAtHand.addIngredient(FoodItem.LEMON);
        ingredientsAtHand.addIngredient(FoodItem.MINCED_MEAT);
        ingredientsAtHand.addIngredient(FoodItem.POTATO);
        ingredientsAtHand.addIngredient(FoodItem.MILK);



        Recipe recipe1 = new Recipe("Recipe no. 1", "Description");
        recipe1.addIngredient(FoodItem.LEMON, 5, MeasuringUnit.PC);
        recipe1.addIngredient(FoodItem.POTATO, 5, MeasuringUnit.PC);

        Recipe recipe2 = new Recipe("Recipe no. 2", "Description" );
        recipe2.addIngredient(FoodItem.POTATO, 5, MeasuringUnit.PC);
        recipe2.addIngredient(FoodItem.TOMATO, 6, MeasuringUnit.PC);
        recipe2.addIngredient(FoodItem.ORANGE, 1, MeasuringUnit.PC);

        recipeRegister.addRecipe(recipe1);
        recipeRegister.addRecipe(recipe2);

        recipeRegister.getRecipes().forEach((recipe) -> recipe.updateIngredientStatus(ingredientsAtHand));

        recipeRegister.getRecipes().sort(Comparator.comparing(Recipe::getMissingIngredients));

        List<Recipe> sortedRecipes = recipeRegister.getRecipes();

        for(int i =0; i<4; i++) {
            if(sortedRecipes.get(i) != null) {
                suggestedRecipes.add(sortedRecipes.get(i));
            } else {
                suggestedRecipes.add(null);
            }

        }

        return suggestedRecipes;

    }


    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        recipes.addAll(getSuggestions());

        int column = 0;
        int row = 0;

        try {
        for(int i = 0; i<4; i++) {
            FXMLLoader fxmlLoader = new FXMLLoader();
            fxmlLoader.setLocation(getClass().getResource("/src/main/resources/view/RecipeTile.fxml"));

            AnchorPane anchorPane = fxmlLoader.load();

            RecipeTileController recipeTileController = fxmlLoader.getController();

            recipeTileController.setData(recipes.get(i));

            if(column == 3){
                column = 0;
                row++;
            }
            recipeTiles.add(anchorPane, column++, row);
            GridPane.setMargin(anchorPane, new Insets(10));
        }

        } catch (IOException e) {
                e.printStackTrace();
        }

    }
}