package mi.hdm.controllers;

import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import mi.hdm.components.CategoryPreviewLabel;
import mi.hdm.components.IngredientLabel;
import mi.hdm.recipes.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

//TODO: display nutrition table in here (using "fxml/components/nutrition-table.fxml", see ViewComponent and MealPlan)
public class RecipeViewController extends BaseController {
    private static final Logger log = LogManager.getLogger(RecipeViewController.class);

    private final Recipe recipe;
    private final RecipeManager recipeManager;
    private final IngredientManager ingredientManager;
    private final CategoryManager categoryManager;
    @FXML
    private AnchorPane parent;
    @FXML
    private Label recipeName;
    @FXML
    private Label recipeDescription;
    @FXML
    private Label recipePreparation;
    @FXML
    private VBox ingredientVBox;
    @FXML
    private ImageView recipeImage;
    @FXML
    private HBox categoryHBox;
    @FXML
    private AnchorPane nutritionTableContainer;

    public RecipeViewController(Recipe recipe, RecipeManager recipeManager, IngredientManager ingredientManager, CategoryManager categoryManager) {
        this.recipe = recipe;
        this.recipeManager = recipeManager;
        this.ingredientManager = ingredientManager;
        this.categoryManager = categoryManager;
    }

    @FXML
    public void initialize() {
        loadHeader(parent);

        recipeName.setText(recipe.getName());
        recipeDescription.setText(recipe.getDescription());
        recipePreparation.setText(String.join("\n", recipe.getPreparation()));

        recipe.getIngredients().forEach((code, amount) -> {
            Ingredient i = ingredientManager.getIngredient(code).orElseThrow();
            IngredientLabel ingredientLabel = new IngredientLabel(amount, i);
            ingredientVBox.getChildren().add(ingredientLabel);
            log.debug("Added ingredient label with the following values: {}, {}, {}", i.getName(), amount, i.getMeasurement());
        });

        categoryManager.getCategoriesFromKeys(recipe.getCategoryCodes()).forEach(category -> {
            CategoryPreviewLabel label = new CategoryPreviewLabel(category);
            categoryHBox.getChildren().add(label);
        });

        //idea for solution from: https://stackoverflow.com/questions/22710053/how-can-i-show-an-image-using-the-imageview-component-in-javafx-and-fxml
        log.debug("Trying to load image for recipe from '{}'", recipe.getImageURL());
        recipeImage.setImage(new Image(recipe.getImageURL().toString()));
        recipeImage.setPreserveRatio(true);
        recipeImage.setFitHeight(180);
        putNutritionTable(nutritionTableContainer, recipe.getNutritionTable(), "Nutrition score for this recipe", 1);
    }

    @FXML
    public void changeSceneToMain() {
        try {
            changeScene(View.MAIN);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("Something went wrong when changing the scene.");
        }
    }

    @FXML
    public void handleEditRecipe() {
        log.info("User is trying to edit recipe");
        changeScene(View.RECIPE_EDITOR, recipe);
    }

    @FXML
    public void handleDeleteRecipe() {
        log.info("User is trying to delete recipe");
        recipeManager.deleteRecipe(recipe);
        changeSceneToMain();
    }
}