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

import dev.fyloz.trial.colorrecipesexplorer.exception.model.EntityNotFoundException;
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.RecipeEditorFormDto;
import dev.fyloz.trial.colorrecipesexplorer.service.model.CompanyService;
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.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.ModelAndView;

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

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

    private RecipeService recipeService;
    private CompanyService companyService;

    @Autowired
    public RecipeEditorController(RecipeService recipeService, CompanyService companyService) {
        this.recipeService = recipeService;
        this.companyService = companyService;
    }

    @GetMapping(EDITOR_RECIPE)
    public ModelAndView getPage(ModelAndView model) {
        return new ModelResponseBuilder(model)
                .withView(EDITOR_RECIPE)
                .addResponseData(ResponseDataType.RECIPE_MAP, recipeService.getAllMappedByCompany())
                .build();
    }

    @GetMapping(EDITOR_RECIPE_SPECIFIC)
    public ModelAndView getEditPage(ModelAndView model, @PathVariable Long id, Recipe recipe) {
        ModelResponseBuilder modelResponseBuilder = new ModelResponseBuilder(model).withView(EDITOR_RECIPE_EDITOR);

        try {
            if (recipe.getName() == null) recipe = recipeService.getById(id);

            modelResponseBuilder
                    .addResponseData(ResponseDataType.RECIPE, recipe)
                    .addResponseData(ResponseDataType.COMPANIES, companyService.getAll())
                    .addResponseData(ResponseDataType.MIXES, recipe.getMixesSortedById())
                    .addResponseData(ResponseDataType.IMAGES, recipeService.getImageFiles(recipe))
                    .addResponseData(ResponseDataType.RECIPE_JSON, recipeService.asJson(recipe));
        } catch (EntityNotFoundException ex) {
            return getPage(modelResponseBuilder.addResponseCode(ResponseCode.RECIPE_NOT_FOUND, id).build());
        }

        return modelResponseBuilder.build();
    }

    @PostMapping(value = EDITOR_RECIPE, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @Transactional
    public ModelAndView updateRecipe(RecipeEditorFormDto recipeDto) {
        ModelResponseBuilder modelResponseBuilder = new ModelResponseBuilder();

        try {
            recipeService.updateRecipeAndSteps(recipeDto);

            modelResponseBuilder.addResponseCode(ResponseCode.SUCCESS_SAVING_RECIPE, recipeDto.getRecipe().getName());
        } catch (EntityNotFoundException ex) {
            modelResponseBuilder.addResponseCode(ResponseCode.RECIPE_NOT_FOUND, recipeDto.getRecipe().getId()).build();
        }

        return getPage(modelResponseBuilder.build());
    }
}
