package de.htwsaar.webshop.controller;

import de.htwsaar.webshop.model.ImageModel;
import de.htwsaar.webshop.repository.entities.Image;
import de.htwsaar.webshop.service.ArticleService;
import de.htwsaar.webshop.service.ImageService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import static de.htwsaar.webshop.config.ControllerPathConfig.*;
import static de.htwsaar.webshop.config.ParameterConfig.*;
import static de.htwsaar.webshop.util.LoggerUtil.logRequest;

@RestController
@Slf4j
public class ImageController {
    private final ImageService imageService;
    private final ArticleService articleService;

    @Autowired
    public ImageController(ImageService imageService, ArticleService articleService) {
        this.imageService = imageService;
        this.articleService = articleService;
    }

    @RequestMapping(path = IMAGE_ALL, method = RequestMethod.GET, produces = "application/json")
    public ResponseEntity<List<String>> getAll(HttpServletRequest request,
                                               @RequestParam(value = PARAM_UUID) UUID uuid) {
        logRequest(request);
        List<Image> images = imageService.getImagesByUUID(uuid);
        if (images.isEmpty()) {
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }
        List<String> images_base = new ArrayList<>();
        images.forEach(image -> images_base.add(image.getBase64()));

        return ResponseEntity.ok(images_base);
    }

    @RequestMapping(path = IMAGE_BASE, method = RequestMethod.GET, produces = "text/plain")
    public ResponseEntity<String> getFirst(HttpServletRequest request,
                                           @RequestParam(value = PARAM_UUID) UUID uuid) {
        logRequest(request);
        Image image = imageService.getImageByUUID(uuid);
        if (image == null) {
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }
        return ResponseEntity.ok(image.getBase64());
    }

    @RequestMapping(path = IMAGE_BASE, method = RequestMethod.POST, produces = "application/json")
    public ResponseEntity<Boolean> add(HttpServletRequest request,
                                           @RequestParam(value = PARAM_UUID) UUID articleUuid,
                                           @RequestBody String base64) {
        logRequest(request);

        if (articleUuid == null || articleService.findByUUID(articleUuid) == null
                || base64 == null || base64.isEmpty()) {
            log.warn("[{}] failed Validation, sending bad request", request.getRequestURI());
            return ResponseEntity.badRequest().body(false);
        }

        return ResponseEntity.ok(imageService.save(articleUuid, base64) != null);
    }

    @RequestMapping(path = IMAGE_BASE, method = RequestMethod.PUT, produces = "application/json")
    public ResponseEntity<Boolean> update(HttpServletRequest request,
                                          @RequestParam(value = PARAM_ID) Long imageId,
                                          @RequestBody ImageModel image) {
        logRequest(request);
        if (imageId == null || imageService.getImageById(imageId) == null) {
            return ResponseEntity.badRequest().body(false);
        }
        image.setId(imageService.getImageById(imageId).getId());
        return ResponseEntity.ok(imageService.save(image) != null);
    }

    @RequestMapping(path = IMAGE_BASE, method = RequestMethod.DELETE, produces = "application/json")
    public ResponseEntity<Boolean> delete(HttpServletRequest request,
                                          @RequestParam(value = PARAM_ID) Long imageId) {
        logRequest(request);
        if (imageId == null) {
            log.warn("[{}] got invalid imageId", request.getRequestURI());
            return ResponseEntity.badRequest().body(false);
        }
        if (imageService.getImageById(imageId) != null) {
            log.warn("[{}] got invalid imageId", request.getRequestURI());
            return ResponseEntity.badRequest().body(false);
        }
        imageService.deleteById(imageId);
        return ResponseEntity.ok().build();
    }

    @RequestMapping(path = IMAGE_FORM, method = RequestMethod.POST, produces = "application/json")
    public ResponseEntity<Boolean> addForm(HttpServletRequest request,
                                           @RequestParam(value = PARAM_UUID) UUID articleUuid,
                                           @RequestParam(value = PARAM_IMAGE) MultipartFile file) {
        logRequest(request);

        if (articleUuid == null || articleService.findByUUID(articleUuid) == null
                || file == null || file.isEmpty()) {
            log.warn("[{}] failed Validation, sending bad request", request.getRequestURI());
            return ResponseEntity.badRequest().body(false);
        }

        Image a = imageService.save(articleUuid, file);
        return ResponseEntity.ok(a != null);
    }
}
