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

import cn.reghao.jutil.jdk.result.WebResult;
import cn.reghao.oss.api.dto.ObjectInfo;
import cn.reghao.oss.api.dto.ServerInfo;
import cn.reghao.oss.api.iface.ConsoleService;
import cn.reghao.oss.api.iface.StoreServiceWrapper;
import cn.reghao.oss.api.util.AuthContext;
import cn.reghao.oss.console.app.model.dto.GetImageUrls;
import cn.reghao.oss.api.dto.media.*;
import cn.reghao.oss.console.app.model.dto.ObjectsScopeDto;
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;

/**
 * @author reghao
 * @date 2024-02-23 09:05:58
 */
@Api(tags = "视频文件接口")
@RestController
@RequestMapping
public class MediaController {
    private final ConsoleService consoleService;
    private final StoreServiceWrapper storeServiceWrapper;

    public MediaController(ConsoleService consoleService, StoreServiceWrapper storeServiceWrapper) {
        this.consoleService = consoleService;
        this.storeServiceWrapper = storeServiceWrapper;
    }

    @ApiOperation(value = "获取 oss-store 节点")
    @GetMapping(value = "/api/oss/server/info", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getServerInfo(@RequestParam("channelId") int channelId) throws Exception {
        int loginUser = AuthContext.getUserId();
        ServerInfo serverInfo = consoleService.getUploadStore(channelId, loginUser);
        return WebResult.success(serverInfo);
    }

    @ApiOperation(value = "设置对象列表可见范围")
    @PostMapping(value = "/api/oss/object/scope/objects", produces = MediaType.APPLICATION_JSON_VALUE)
    public String setObjectsScope(ObjectsScopeDto objectsScopeDto) throws Exception {
        //storeServiceWrapper.setObjectsScope(objectsScopeDto);
        return WebResult.success();
    }

    @ApiOperation(value = "根据 object_id 删除对象")
    @PostMapping(value = "/api/oss/object/delete/ids", produces = MediaType.APPLICATION_JSON_VALUE)
    public String deleteByObjects(@RequestParam("channelId") Integer channelId,
                                  @RequestParam("objectIds") List<String> objectIds) throws Exception {
        //storeServiceWrapper.deleteByObjectIds(channelId, objectIds);
        return WebResult.success();
    }

    @ApiOperation(value = "根据 object_url 删除对象")
    @PostMapping(value = "/api/oss/object/delete/url", produces = MediaType.APPLICATION_JSON_VALUE)
    public String deleteByObjectUrl(@RequestParam("objectUrl") String objectUrl) throws Exception {
        storeServiceWrapper.deleteByObjectUrl(objectUrl);
        return WebResult.success();
    }

    // TODO 接口作废
    @ApiOperation(value = "获取对象签名 url")
    @GetMapping(value = "/api/oss/object/url", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getObjectUrl(@RequestParam("channelId") Integer channelId,
                               @RequestParam("objectId") String objectId) throws Exception {
        //String signedUrl = storeServiceWrapper.getSignedUrl(channelId, objectId);
        return WebResult.success();
    }

    @ApiOperation(value = "获取对象信息")
    @GetMapping(value = "/api/oss/object/info", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getObjectInfo(@RequestParam("channelId") int channelId,
                                @RequestParam("objectId") String objectId) throws Exception {
        ObjectInfo objectInfo = storeServiceWrapper.getObjectInfo(channelId, objectId);
        return WebResult.success(objectInfo);
    }

    @ApiOperation(value = "获取视频文件信息")
    @GetMapping(value = "/api/oss/media/video/info", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getVideoInfo(@RequestParam("channelId") Integer channelId,
                               @RequestParam("videoFileId") String videoFileId) throws Exception {
        VideoInfo videoInfo = storeServiceWrapper.getVideoInfo(channelId, videoFileId);
        return WebResult.success(videoInfo);
    }

    @ApiOperation(value = "获取图片文件信息")
    @PostMapping(value = "/api/oss/media/image/info", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getImagesInfo(GetImageUrls getImageUrls) throws Exception {
        //ImageInfo imageInfo = storeServiceWrapper.getImageInfo(getImageUrls.getChannelId(), getImageUrls.getImageFileIds());
        return WebResult.success();
    }

    @ApiOperation(value = "获取 webp 图片文件信息")
    @PostMapping(value = "/api/oss/media/image/webp", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getWebpInfo(GetImageUrls getImageUrls) throws Exception {
        //List<ConvertedImageInfo> list = storeServiceWrapper.getWebpInfo(getImageUrls.getChannelId(), getImageUrls.getImageFileIds());
        return WebResult.success();
    }

    @ApiOperation(value = "获取音频文件信息")
    @GetMapping(value = "/api/oss/media/auduio/info", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getAudioInfo(@RequestParam("channelId") Integer channelId,
                               @RequestParam("audioFileId") String audioFileId) throws Exception {
        AudioInfo audioInfo = storeServiceWrapper.getAudioInfo(channelId, audioFileId);
        return WebResult.success(audioInfo);
    }

    @ApiOperation(value = "获取对象签名 url")
    @GetMapping(value = "/api/oss/media/url", produces = MediaType.APPLICATION_JSON_VALUE)
    @Deprecated
    public String getObjectUrl1(@RequestParam("channelId") Integer channelId,
                               @RequestParam("objectId") String objectId) throws Exception {
        String signedUrl = storeServiceWrapper.getSignedUrl(channelId, objectId);
        return WebResult.success(signedUrl);
    }
}
