package org.egl_cepgl.pm.controller.admin;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.egl_cepgl.pm.dto.ProjectDto;
import org.egl_cepgl.pm.dto.SettingDto;
import org.egl_cepgl.pm.model.Project;
import org.egl_cepgl.pm.model.Setting;
import org.egl_cepgl.pm.service.FileService;
import org.egl_cepgl.pm.service.ProjectService;
import org.egl_cepgl.pm.service.SettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Api("api/admin/settings")
@RestController
@RequestMapping("api/admin/settings")
public class SettingController
{
    private SettingService settingService;
    private FileService fileService;

    @Autowired
    public SettingController(SettingService settingService, FileService fileService) {
        this.fileService= fileService;
        this.settingService = settingService;
    }

    @GetMapping(value = "/getSettings",produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value="List ..", notes="Cette méthode permet..", responseContainer = "List<SettingDto>")
    @ApiResponses(value= { @ApiResponse(code= 200, message = "Paramètres ..") })
    public ResponseEntity<SettingDto> getSettings()
    {
        return ResponseEntity.ok(SettingDto.fromEntity(settingService.getSetting()));
    }

    @PutMapping(value = "/updateSettings",consumes= MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value="Update ..", notes="Cette méthode permet de..", response = Setting.class)
    @ApiResponses(value= { @ApiResponse(code= 200, message = "créé/modifié"), @ApiResponse(code= 400, message = "Non valide") })
    public ResponseEntity<SettingDto> updateSettings(@RequestBody SettingDto setting)
    {    return ResponseEntity.ok(settingService.updateSetting(setting));   }

    @PostMapping(value = "/updateAppLogo")
    public ResponseEntity<String> updateAppLogo(
            @RequestBody byte[] fileContent,
            @RequestHeader("X-File-Name") String fileName
        ) throws IOException {
            return ResponseEntity.ok(fileService.uploadFile(fileContent, fileName, true));
    }

}
