package cn.reghao.oss.web.app.controller;

import cn.reghao.jutil.jdk.result.WebResult;
import cn.reghao.oss.api.dto.media.ImageUrlDto;
import cn.reghao.oss.web.app.service.media.ImageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * @author reghao
 * @date 2024-02-23 09:27:15
 */
@Api(tags = "图片文件接口")
@RestController
@RequestMapping("/api/oss/media/image")
public class ImageFileController {
    private final ImageService imageService;

    public ImageFileController(ImageService imageService) {
        this.imageService = imageService;
    }

    @ApiOperation(value = "根据 object_name 删除图片文件")
    @PostMapping(value = "/delete/name", produces = MediaType.APPLICATION_JSON_VALUE)
    public String deleteByObjectNames(@RequestParam("objectNames") List<String> objectNames) throws Exception {
        imageService.deleteByObjectNames(objectNames);
        return WebResult.success();
    }

    @ApiOperation(value = "根据 image_file_id 删除图片文件")
    @PostMapping(value = "/delete/id", produces = MediaType.APPLICATION_JSON_VALUE)
    public String deleteByImageIds(@RequestParam("imageFileIds") List<String> imageFileIds) throws Exception {
        imageService.deleteByImageFileIds(imageFileIds);
        return WebResult.success();
    }

    @ApiOperation(value = "获取图片文件 url")
    @GetMapping(value = "/url", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getImageUrl(@RequestParam("channelName") String channelName,
                              @RequestParam("imageFileId") String imageFileId) {
        ImageUrlDto imageUrlDto = imageService.getImageUrl(channelName, imageFileId);
        return WebResult.success(imageUrlDto);
    }

    @ApiOperation(value = "获取图片文件 url")
    @GetMapping(value = "/urls", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getImageUrls(@RequestParam("imageFileIds") Set<String> imageFileIds) {
        List<ImageUrlDto> list = imageService.getImageUrls(imageFileIds);
        return WebResult.success(list);
    }

    @ApiOperation(value = "获取图片签名 url")
    @GetMapping(value = "/signedurl", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getImageSignedUrls(@RequestParam("channelId") int channelId,
                                     @RequestParam("url") String url,
                                     @RequestParam(value = "loginUser") Long loginUser) {
        String signedUrl = imageService.getSignedUrl(url, loginUser, channelId);
        return WebResult.success(signedUrl);
    }
}
