package dev.fyloz.trial.colorrecipesexplorer.web.controller.thymeleaf;

import dev.fyloz.trial.colorrecipesexplorer.exception.model.EntityNotFoundException;
import dev.fyloz.trial.colorrecipesexplorer.web.response.JSONResponseBuilder;
import dev.fyloz.trial.colorrecipesexplorer.web.response.ModelResponseBuilder;
import dev.fyloz.trial.colorrecipesexplorer.web.response.ResponseCode;
import dev.fyloz.trial.colorrecipesexplorer.web.response.ResponseDataType;
import dev.fyloz.trial.colorrecipesexplorer.model.Recipe;
import dev.fyloz.trial.colorrecipesexplorer.model.dto.RecipeExplorerFormDto;
import dev.fyloz.trial.colorrecipesexplorer.service.model.RecipeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

import static dev.fyloz.trial.colorrecipesexplorer.web.WebsitePaths.*;

@Controller
@Profile("thymeleaf")
public class RecipeExplorerController {

    private RecipeService recipeService;

    @Autowired
    public RecipeExplorerController(RecipeService recipeService) {
        this.recipeService = recipeService;
    }

    @GetMapping(EXPLORER_RECIPE_SPECIFIC)
    public ModelAndView getPage(@PathVariable Long id) {
        ModelResponseBuilder modelResponseBuilder = new ModelResponseBuilder(EXPLORER_RECIPE);

        try {
            Recipe recipe = recipeService.getById(id);

            return modelResponseBuilder
                    .addResponseData(ResponseDataType.RECIPE, recipe)
                    .addResponseData(ResponseDataType.MIXES, recipe.getMixesSortedById())
                    .addResponseData(ResponseDataType.IMAGES, recipeService.getImageFiles(recipe))
                    .build();
        } catch (EntityNotFoundException e) {
            return modelResponseBuilder
                    .withView(INDEX)
                    .addResponseCode(ResponseCode.RECIPE_NOT_FOUND, id)
                    .build();
        }
    }

    @PostMapping(value = EXPLORER_RECIPE, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Map<String, Object> saveRecipeInformations(@RequestBody RecipeExplorerFormDto form) {
        JSONResponseBuilder responseBuilder = new JSONResponseBuilder();

        try {
            recipeService.updateRecipeExplorerInfos(form);

            responseBuilder.addResponseCode(ResponseCode.SUCCESS_SAVING_RECIPE_INFORMATIONS);
        } catch (EntityNotFoundException e) {
            responseBuilder.addResponseCode(
                    e.getType().equals(Recipe.class) ?
                            ResponseCode.RECIPE_NOT_FOUND :
                            ResponseCode.MIX_NOT_FOUND,
                    e.getRequestedId()
            );
        }

        return responseBuilder.build();
    }
}
