package ru.kayashov.bar.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import ru.kayashov.bar.controller.dto.cocktail.CocktailFilterRequestDto;
import ru.kayashov.bar.controller.dto.cocktail.CocktailForListResponseDto;
import ru.kayashov.bar.controller.dto.ErrorDto;
import ru.kayashov.bar.controller.dto.cocktail.CocktailModalDto;
import ru.kayashov.bar.controller.dto.cocktail.CocktailSimpleResponseDto;
import ru.kayashov.bar.controller.dto.cocktail.ReceiptResponseDto;
import ru.kayashov.bar.model.entity.Visitor;
import ru.kayashov.bar.service.CocktailService;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@Slf4j
@CrossOrigin(origins = {"*"})
@RestController
@RequestMapping("/api/cocktail")
@RequiredArgsConstructor
public class CocktailController {

    private final CocktailService cocktailService;

    //получить все
    @PostMapping("menu")
    public List<CocktailForListResponseDto> menu(@RequestBody CocktailFilterRequestDto dto) {
        return cocktailService.getMenu(dto);
    }

    @PostMapping("/photo")
    public String savePhoto(@RequestBody MultipartFile file) throws IOException {
        if(file == null) {
            return "";
        }
        return cocktailService.savePhoto(file);
    }

    @GetMapping
    public CocktailForListResponseDto getOne(@RequestParam Long id) {
        return cocktailService.findById(id);
    }

    @GetMapping("/simple")
    public List<CocktailSimpleResponseDto> getSimple() {
        return cocktailService.getSimple();
    }

    @GetMapping("/receipts")
    public List<ReceiptResponseDto> getReceipts(@RequestParam Long id) {
        return cocktailService.getReceipts(id);
    }

    @PostMapping("menuEdit")
    public ResponseEntity<ErrorDto> inMenuEdit(@RequestParam Long id, @RequestParam Boolean value) {
        try {
            cocktailService.inMenuEdit(id, value);
            return ResponseEntity.ok(new ErrorDto(null));
        } catch (Exception e) {
            return ResponseEntity.ok(new ErrorDto(e.getMessage()));
        }
    }

    @GetMapping("/modal")
    public CocktailModalDto getForModal(@RequestParam Long id) {
        return cocktailService.getForModal(id);
    }

    @PatchMapping()
    public ResponseEntity<ErrorDto> editCocktail(@RequestBody CocktailForListResponseDto dto) {
        try {
            cocktailService.edit(dto);
            return ResponseEntity.ok(new ErrorDto(null));
        } catch (Exception e) {
            log.error(e.getMessage(), Arrays.toString(e.getStackTrace()));
            return ResponseEntity.ok(new ErrorDto(e.getMessage()));
        }
    }

    @DeleteMapping
    public void deleteCocktail(@RequestParam Long id) {
        cocktailService.delete(id);
    }

    @PutMapping("/favourite")
    public void addInFavourite(@RequestParam("id") Long id) {
        Long visitorId = ((Visitor) SecurityContextHolder.getContext().getAuthentication().getPrincipal())
                .getId();
        cocktailService.editFavourite(id, visitorId, true);
    }

    @DeleteMapping("/favourite")
    public void deleteFromFavourite(@RequestParam("id") Long id) {
        Long visitorId = ((Visitor) SecurityContextHolder.getContext().getAuthentication().getPrincipal())
                .getId();
        cocktailService.editFavourite(id, visitorId, false);
    }

    @PostMapping("/rating")
    public void addRating(@RequestParam("id") Long id, @RequestParam("rating") Integer rating) {
        cocktailService.setRating(id, rating);
    }
}
