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

import dev.fyloz.trial.colorrecipesexplorer.web.response.ModelResponseBuilder;
import dev.fyloz.trial.colorrecipesexplorer.web.response.ResponseDataType;
import dev.fyloz.trial.colorrecipesexplorer.service.files.TouchUpKitService;
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.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
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 TouchUpKitController {

    private TouchUpKitService touchUpKitService;
    private RecipeService recipeService;

    @Autowired
    public TouchUpKitController(TouchUpKitService touchUpKitService, RecipeService recipeService) {
        this.touchUpKitService = touchUpKitService;
        this.recipeService = recipeService;
    }

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

    @PostMapping(value = TOUCHUP_PDF, produces = MediaType.APPLICATION_PDF_VALUE)
    public ResponseEntity<byte[]> getTouchUpKitPdf(String jobNumber) {
        return new ResponseEntity<>(touchUpKitService.generatePdfForJobNumber(jobNumber), HttpStatus.FOUND);
    }

    @PostMapping(value = TOUCHUP_PTOUCH)
    public ModelAndView getTouchUpKitPtouch(String jobNumber) {
        return new ModelResponseBuilder(TOUCHUP_PTOUCH_PAGE)
                .addAttribute("jobNumber", jobNumber)
                .build();
    }

}
