package me.academeg.controllers;

import me.academeg.entity.Account;
import me.academeg.entity.Avatar;
import me.academeg.service.AccountService;
import me.academeg.service.AvatarService;
import me.academeg.utils.ImageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.User;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

@RestController
@RequestMapping("/api")
@Validated
public class AvatarController {

    private AvatarService avatarService;
    private AccountService accountService;

    @Autowired
    public AvatarController(AvatarService avatarService, AccountService accountService) {
        this.avatarService = avatarService;
        this.accountService = accountService;
    }

    @RequestMapping(value = "/account/set-avatar", method = RequestMethod.POST)
    public Avatar setAvatar(@AuthenticationPrincipal User user, @RequestParam("file") MultipartFile file) {
        if (!file.getContentType().startsWith("image/")) {
            throw new IllegalArgumentException("You can upload only images");
        }

        if (file.isEmpty()) {
            throw new IllegalArgumentException("File cannot be empty");
        }

        File dir = new File("avatars");
        if (!dir.exists()) {
            dir.mkdir();
        }

        Avatar avatar = new Avatar();
        String originalImageName = ImageUtils.saveImage(dir, file);
        avatar.setOriginalPath("avatar/" + originalImageName);
        String thumbnailImageName = ImageUtils.compressImage(new File(dir, originalImageName), dir);
        avatar.setThumbnailPath("avatar/" + thumbnailImageName);

        Account account = accountService.getByEmail(user.getUsername());
        return avatarService.set(avatar, account);
    }

    @RequestMapping(value = "/account/delete-avatar", method = RequestMethod.DELETE)
    public void deleteAvatar() {

    }
}

@@ -277,24 +277,116 @@
 .Autowired;%0A
+import org.springframework.http.HttpStatus;%0Aimport org.springframework.http.ResponseEntity;%0A
 import org.s
@@ -620,208 +620,9 @@
 ion.
-RequestMapping;%0Aimport org.springframework.web.bind.annotation.RequestMethod;%0Aimport org.springframework.web.bind.annotation.RequestParam;%0Aimport org.springframework.web.bind.annotation.RestController
+*
 ;%0Aim
@@ -778,24 +778,77 @@
 ntroller %7B%0A%0A
+    private static String AVATAR_PATH = %22avatars/%22;%0A%0A
     private 
@@ -1563,32 +1563,33 @@
 y%22);%0A        %7D%0A%0A
+%0A
         File dir
@@ -1585,17 +1585,24 @@
    File 
-d
+avatarsD
 ir = new
@@ -1611,17 +1611,19 @@
 ile(
-%22avatars%22
+AVATAR_PATH
 );%0A 
@@ -1634,17 +1634,24 @@
    if (!
-d
+avatarsD
 ir.exist
@@ -1669,17 +1669,24 @@
         
-d
+avatarsD
 ir.mkdir
@@ -1794,17 +1794,24 @@
 veImage(
-d
+avatarsD
 ir, file
@@ -1844,25 +1844,27 @@
 nalPath(
-%22avatar/%22
+AVATAR_PATH
  + origi
@@ -1948,17 +1948,24 @@
 ew File(
-d
+avatarsD
 ir, orig
@@ -1980,17 +1980,24 @@
 eName), 
-d
+avatarsD
 ir);%0A   
@@ -2029,17 +2029,19 @@
 ath(
-%22avatar/%22
+AVATAR_PATH
  + t
@@ -2134,16 +2134,70 @@
 ame());%0A
+        deleteAvatarFromStorage(account.getAvatar());%0A
         
@@ -2346,30 +2346,764 @@
 lic 
-void deleteAvatar() %7B%0A
+ResponseEntity%3CString%3E deleteAvatar(@AuthenticationPrincipal User user) %7B%0A        Account account = accountService.getByEmail(user.getUsername());%0A        if (account.getAvatar() != null) %7B%0A            avatarService.delete(account.getAvatar());%0A            deleteAvatarFromStorage(account.getAvatar());%0A        %7D%0A        return new ResponseEntity%3C%3E(%22Avatar delete successful%22, HttpStatus.OK);%0A    %7D%0A%0A    @RequestMapping(value = %22/avatars/%7Bid%7D%22, method = RequestMethod.GET)%0A    public void getAvatar(@PathVariable long id) %7B%0A%0A    %7D%0A%0A    private void deleteAvatarFromStorage(Avatar avatar) %7B%0A        if (avatar == null) %7B%0A            return;%0A        %7D%0A        new File(avatar.getOriginalPath()).delete();%0A        new File(avatar.getThumbnailPath()).delete();
 %0A   

Delete file when delete avatar