package com.royalrangers.controller.achievement;

import com.dropbox.core.DbxException;
import com.royalrangers.dto.ResponseResult;
import com.royalrangers.enums.ImageType;
import com.royalrangers.model.achievement.Reward;
import com.royalrangers.service.DropboxService;
import com.royalrangers.service.achievement.RewardService;
import com.royalrangers.utils.ResponseBuilder;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Slf4j
@RestController
@RequestMapping("/achievements/reward")
public class RewardController {

    @Autowired
    private RewardService rewardService;

    @Autowired
    private DropboxService dropboxService;

    @GetMapping
    public ResponseResult getAllReward() {
        try {
            return ResponseBuilder.success(rewardService.getAllReward());
        } catch (Exception ex) {
            log.error(ex.getMessage());
            return ResponseBuilder.fail("Failed get all rewards");
        }
    }

    @PostMapping
    public ResponseResult addReward(@RequestBody Reward reward) {
        try {
            rewardService.addReward(reward);
            return ResponseBuilder.success("Successful addition of a reward");
        } catch (Exception ex) {
            log.error(ex.getMessage());
            return ResponseBuilder.fail("Failed add reward");
        }
    }

    @GetMapping("/{rewardId}")
    public ResponseResult getRewardById(@PathVariable Long rewardId) {
        try {
            return ResponseBuilder.success(rewardService.getRewardById(rewardId));
        } catch (Exception ex) {
            log.error(ex.getMessage());
            return ResponseBuilder.fail("Failed get reward by id");
        }
    }

    @DeleteMapping("/{rewardId}")
    public ResponseResult deleteReward(@PathVariable Long rewardId) {
        try {
            rewardService.deleteReward(rewardId);
            return ResponseBuilder.success("Delete Reward was a success");
        } catch (Exception ex) {
            log.error(ex.getMessage());
            return ResponseBuilder.fail("Failed deleted Reward");
        }
    }

    @PutMapping("/{rewardId}")
    public ResponseResult editReward(@RequestBody Reward reward, @PathVariable Long rewardId) {
        try {
            return ResponseBuilder.success(rewardService.editReward(reward, rewardId));
        } catch (Exception ex) {
            log.error(ex.getMessage());
            return ResponseBuilder.fail("Failed edit Reward");
        }
    }

    @PostMapping("/logo")
    @ApiOperation(value = "Upload and set Reward logo")
    public ResponseResult uploadLogo(@RequestParam("rewardId") Long rewardId, @RequestParam("file") MultipartFile file) {
        try {
            String logoUrl = dropboxService.imageUpload(file, ImageType.REWARD_LOGO);
            rewardService.setLogoUrl(logoUrl, rewardId);
            return ResponseBuilder.success("LogoUrl", logoUrl);
        } catch (IOException | DbxException e) {
            return ResponseBuilder.fail(e.getMessage());
        }
    }

    @DeleteMapping("/logo")
    @ApiOperation(value = "Delete logo")
    public ResponseResult delete(@RequestParam("rewardId") Long rewardId) {
        try {
            rewardService.deleteLogo(rewardId);
            return ResponseBuilder.success("Logo deleted.");
        } catch (DbxException e) {
            return ResponseBuilder.fail(e.getMessage());
        }
    }
}

@@ -867,32 +867,77 @@
     @GetMapping%0A
+    @ApiOperation(value = %22Get all rewards%22)%0A
     public Respo
@@ -959,24 +959,24 @@
 lReward() %7B%0A
-
         try 
@@ -1221,24 +1221,64 @@
 PostMapping%0A
+    @ApiOperation(value = %22Add reward%22)%0A
     public R
@@ -1644,32 +1644,78 @@
 (%22/%7BrewardId%7D%22)%0A
+    @ApiOperation(value = %22Get reward by id%22)%0A
     public Respo
@@ -2050,32 +2050,81 @@
 (%22/%7BrewardId%7D%22)%0A
+    @ApiOperation(value = %22Delete reward by id%22)%0A
     public Respo
@@ -2499,24 +2499,73 @@
 rewardId%7D%22)%0A
+    @ApiOperation(value = %22Update reward by id%22)%0A
     public R
@@ -2971,17 +2971,17 @@
 and set 
-R
+r
 eward lo
@@ -3471,32 +3471,32 @@
 apping(%22/logo%22)%0A
-
     @ApiOperatio
@@ -3513,16 +3513,23 @@
 %22Delete 
+reward 
 logo%22)%0A 

Add API descriptions for RewardController