From b8d9b0b5e54de5b3aacc376f66bb15443bad4544 Mon Sep 17 00:00:00 2001
From: HSoreide <sofie.scisly@gmail.com>
Date: Fri, 14 Apr 2023 17:59:40 +0200
Subject: [PATCH] Make Recipe controllers to provide test data and fill in grid
 with suggested recipes

---
 .../demo/controller/RecipeController.java     | 103 +++++++++++++++++-
 .../demo/controller/RecipeTileController.java |  20 ++++
 .../idatt1002/demo/data/recipes/Recipe.java   |   4 +
 3 files changed, 126 insertions(+), 1 deletion(-)

diff --git a/src/main/java/no/ntnu/idatt1002/demo/controller/RecipeController.java b/src/main/java/no/ntnu/idatt1002/demo/controller/RecipeController.java
index 2b655cd7..0f753129 100644
--- a/src/main/java/no/ntnu/idatt1002/demo/controller/RecipeController.java
+++ b/src/main/java/no/ntnu/idatt1002/demo/controller/RecipeController.java
@@ -1,4 +1,105 @@
 package no.ntnu.idatt1002.demo.controller;
 
-public class RecipeController {
+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.ArrayList;
+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(new Ingredient(FoodItem.LEMON, 6, MeasuringUnit.PC));
+        ingredientsAtHand.addIngredient(new Ingredient(FoodItem.MINCED_MEAT, 400, MeasuringUnit.GR));
+        ingredientsAtHand.addIngredient(new Ingredient(FoodItem.POTATO, 4, MeasuringUnit.PC));
+        ingredientsAtHand.addIngredient(new Ingredient(FoodItem.MILK, 8, MeasuringUnit.DL));
+
+
+
+        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();
+        }
+
+    }
 }
diff --git a/src/main/java/no/ntnu/idatt1002/demo/controller/RecipeTileController.java b/src/main/java/no/ntnu/idatt1002/demo/controller/RecipeTileController.java
index 3c29d62c..cb23f567 100644
--- a/src/main/java/no/ntnu/idatt1002/demo/controller/RecipeTileController.java
+++ b/src/main/java/no/ntnu/idatt1002/demo/controller/RecipeTileController.java
@@ -1,4 +1,24 @@
 package no.ntnu.idatt1002.demo.controller;
 
+import javafx.fxml.FXML;
+import javafx.scene.control.Label;
+import no.ntnu.idatt1002.demo.data.recipes.Recipe;
+
 public class RecipeTileController {
+
+    @FXML
+    private Label nameTag;
+
+    @FXML
+    private Label missingTag;
+
+    private Recipe recipe;
+
+    public void setData(Recipe recipe) {
+
+        this.recipe = recipe;
+        nameTag.setText(recipe.getName());
+        missingTag.setText(Integer.toString(recipe.getMissingIngredients()));
+
+    }
 }
diff --git a/src/main/java/no/ntnu/idatt1002/demo/data/recipes/Recipe.java b/src/main/java/no/ntnu/idatt1002/demo/data/recipes/Recipe.java
index def23387..edfd6240 100644
--- a/src/main/java/no/ntnu/idatt1002/demo/data/recipes/Recipe.java
+++ b/src/main/java/no/ntnu/idatt1002/demo/data/recipes/Recipe.java
@@ -101,6 +101,7 @@ public class Recipe {
                    if(inRecipe.getFoodType() == atHand.getFoodType()) {
                        inRecipe.setAtHand(true);
                    } else {
+                       inRecipe.setAtHand(false);
                         missingIngredients += 1;
                    }
                });
@@ -108,6 +109,9 @@ public class Recipe {
        }
     }
 
+    public int getMissingIngredients() {
+        return missingIngredients;
+    }
 
 
     @Override
-- 
GitLab