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

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.service.files.ImagesService;
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.HttpHeaders;
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.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.util.Map;

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

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

    private RecipeService recipeService;
    private ImagesService imagesService;

    @Autowired
    public ImageFilesController(RecipeService recipeService, ImagesService imagesService) {
        this.recipeService = recipeService;
        this.imagesService = imagesService;
    }

    @GetMapping(IMAGES_FILES)
    public ResponseEntity<byte[]> getImage(@PathVariable String image) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.IMAGE_JPEG);
        return new ResponseEntity<>(imagesService.read(image), headers, HttpStatus.OK);
    }

    @GetMapping(ADD_IMAGE_SPECIFIC)
    public ModelAndView getPage(ModelAndView model, @PathVariable Long id) {
        return new ModelResponseBuilder(model)
                .withView(ADD_IMAGE)
                .addResponseData(ResponseDataType.RECIPE_ID, id)
                .build();
    }

    @PostMapping(ADD_IMAGE)
    public ModelAndView addImage(Long id, MultipartFile image) throws IOException {
        ModelResponseBuilder modelResponseBuilder = new ModelResponseBuilder().withRedirect(EDITOR_RECIPE_SPECIFIC, id);

        try {
            if (!imagesService.isImage(image.getInputStream())) {
                modelResponseBuilder.addResponseCode(ResponseCode.FILE_NOT_IMAGE);
            } else {
                Recipe recipe = recipeService.getById(id);

                if (imagesService.write(image, recipeService.getImageFileNameWithIndex(recipe)))
                    return modelResponseBuilder.build();
                else modelResponseBuilder.addResponseCode(ResponseCode.ERROR_SAVING_IMAGE);
            }
        } catch (EntityNotFoundException ex) {
            modelResponseBuilder.addResponseCode(ResponseCode.RECIPE_NOT_FOUND, id);
        }

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

    @GetMapping(value = DELETE_IMAGE, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Map<String, Object> deleteImage(@PathVariable String image) {
        imagesService.delete(image);

        return new JSONResponseBuilder().build();
    }
}
