package com.royalrangers.controller;

import com.dropbox.core.DbxException;
import com.fasterxml.jackson.annotation.JsonView;
import com.royalrangers.dto.ResponseResult;
import com.royalrangers.dto.user.*;
import com.royalrangers.enums.ImageType;
import com.royalrangers.exception.UserRepositoryException;
import com.royalrangers.model.Views;
import com.royalrangers.service.DropboxService;
import com.royalrangers.service.UserService;
import com.royalrangers.utils.ResponseBuilder;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private DropboxService dropboxService;

    @JsonView(Views.Profile.class)
    @GetMapping
    @ApiOperation(value = "Get user info")
    public ResponseResult getAuthenticatedUserDetail() {

        String username = userService.getAuthenticatedUserEmail();
        log.info("Get details for user " + username);

        return ResponseBuilder.success(userService.getUserByEmail(username));
    }

    @JsonView(Views.Profile.class)
    @GetMapping("{id}")
    @PreAuthorize("hasRole('ADMIN')")
    @ApiOperation(value = "Get user info (for admin)")
    public ResponseResult getUserDetailById(@PathVariable("id") Long id) {
        try {
            log.info("Get details for user id " + id);
            return ResponseBuilder.success(userService.getUserById(id));
        } catch (UserRepositoryException e){
            return ResponseBuilder.fail(e.getMessage());
        }
    }

    @JsonView(Views.Profile.class)
    @GetMapping("/approve/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    @ApiOperation(value = "Get users for approve (for admin)")
    public ResponseResult getUserToApprove(@PathVariable("id") Long platoonId){
        return ResponseBuilder.success(userService.getUsersForApprove(platoonId));
    }

    @PostMapping("/approve")
    @PreAuthorize("hasRole('ADMIN')")
    @ApiOperation(value = "Approve users after registration (for admin)")
    public ResponseResult approveUser(@RequestBody IdsDto param) {
        List<Long> ids = param.getIds();
        userService.approveUsers(ids);
        return ResponseBuilder.success("Users successfully approved.");
    }

    @PostMapping("/reject")
    @PreAuthorize("hasRole('ADMIN')")
    @ApiOperation(value = "Reject user after registration (for admin)")
    public ResponseResult rejectUser(@RequestBody IdsDto param) {
        List<Long> ids = param.getIds();
        userService.rejectUsers(ids);
        return ResponseBuilder.success("Users successfully rejected.");
    }

    @PutMapping
    @ApiOperation(value = "Update user")
    public ResponseResult updateAuthorizedUser(@RequestBody UserUpdateDto update) {

        String email = userService.getAuthenticatedUserEmail();

        userService.updateUser(update);
        log.info("Update user " + email);

        return ResponseBuilder.success(String.format("User %s successful updated", email));
    }

    @PutMapping(value = "/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    @ApiOperation(value = "Update user (for admin)")
    public ResponseResult updateUserById(@PathVariable("id") Long id, @RequestBody UserUpdateDto userUpdate) {
        try {
            userService.updateUserById(id, userUpdate);
            log.info("Update user with id %d " + id);

            return ResponseBuilder.success(String.format("User with id %d successful updated", id));
        } catch (UserRepositoryException e){
            return ResponseBuilder.fail(e.getMessage());
        }
    }

    @PostMapping("/avatar")
    @ApiOperation(value = "Upload and set user avatar")
    public ResponseResult upload(@RequestParam("file") MultipartFile file) {
        try {
            String avatarUrl = dropboxService.imageUpload(file, ImageType.USER_AVATAR);
            log.info("Set user avatar public URL: " +avatarUrl);

            userService.setUserAvatarUrl(avatarUrl);

            return ResponseBuilder.success("avatarUrl", avatarUrl);
        } catch (IOException | DbxException e) {
            return  ResponseBuilder.fail(e.getMessage());
        }
    }

    @DeleteMapping("/avatar")
    @ApiOperation(value = "Delete avatar picture")
    public ResponseResult delete(@RequestBody AvatarUrlDto request ) {
        try {
            String avatarUrl = request.getAvatarUrl();
            dropboxService.deleteImage(avatarUrl, ImageType.USER_AVATAR);
            log.info("Delete avatar: " + avatarUrl);

            return ResponseBuilder.success(avatarUrl + " deleted.");
        } catch (DbxException e) {
            return ResponseBuilder.fail(e.getMessage());
        }
    }
}

@@ -1095,32 +1095,40 @@
 on(value = %22Get 
+current 
 user info%22)%0A    
@@ -1123,16 +1123,16 @@
  info%22)%0A
-
     publ
@@ -1526,16 +1526,27 @@
 er info 
+by user id 
 (for adm
@@ -2043,24 +2043,32 @@
 pprove (for 
+platoon 
 admin)%22)%0A   
@@ -2358,32 +2358,40 @@
 gistration (for 
+platoon 
 admin)%22)%0A    pub
@@ -2734,24 +2734,32 @@
 ration (for 
+platoon 
 admin)%22)%0A   
@@ -3025,24 +3025,32 @@
 e = %22Update 
+current 
 user%22)%0A    p
@@ -3486,16 +3486,26 @@
 te user 
+by user id
 (for adm
@@ -4036,26 +4036,38 @@
 and set 
-user 
 avata
+r for current use
 r%22)%0A    
@@ -4470,32 +4470,32 @@
 xException e) %7B%0A
+
             retu
@@ -4555,534 +4555,7 @@
    %7D
-%0A%0A    @DeleteMapping(%22/avatar%22)%0A    @ApiOperation(value = %22Delete avatar picture%22)%0A    public ResponseResult delete(@RequestBody AvatarUrlDto request ) %7B%0A        try %7B%0A            String avatarUrl = request.getAvatarUrl();%0A            dropboxService.deleteImage(avatarUrl, ImageType.USER_AVATAR);%0A            log.info(%22Delete avatar: %22 + avatarUrl);%0A%0A            return ResponseBuilder.success(avatarUrl + %22 deleted.%22);%0A        %7D catch (DbxException e) %7B%0A            return ResponseBuilder.fail(e.getMessage());%0A        %7D%0A    %7D
 %0A%7D%0A

Fix API description for UserController. Delete unused (and dangerous) delete avatar endpoint.