package cn.reghao.tnb.file.app.zoss.controller;

import cn.reghao.jutil.jdk.web.result.Result;
import cn.reghao.tnb.common.web.WebResult;
import cn.reghao.tnb.oss.api.iface.ConsoleService;
import cn.reghao.tnb.oss.api.dto.ObjectInfo;
import cn.reghao.tnb.oss.api.dto.ServerInfo;
import cn.reghao.tnb.oss.api.dto.media.AudioInfo;
import cn.reghao.tnb.oss.api.dto.media.ConvertedImageInfo;
import cn.reghao.tnb.oss.api.dto.media.ImageInfo;
import cn.reghao.tnb.oss.api.dto.media.VideoInfo;
import cn.reghao.tnb.common.auth.UserContext;
import cn.reghao.tnb.file.app.zoss.model.dto.KeyAuthDto;
import cn.reghao.tnb.file.app.zoss.rpc.StoreServiceWrapper;
import cn.reghao.tnb.file.app.zoss.service.UserKeyService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author reghao
 * @date 2024-02-23 09:05:58
 */
@Tag(name = "oss-sdk 接口")
@RestController
@RequestMapping("/api/oss/sdk")
public class OssSdkController {
    private final UserKeyService userKeyService;
    private final ConsoleService consoleService;
    private final StoreServiceWrapper storeServiceWrapper;

    public OssSdkController(UserKeyService userKeyService, ConsoleService consoleService, 
                            StoreServiceWrapper storeServiceWrapper) {
        this.userKeyService = userKeyService;
        this.consoleService = consoleService;
        this.storeServiceWrapper = storeServiceWrapper;
    }

    @Operation(summary = "对 oss key 进行认证", description = "N")
    @PostMapping(value = "/key/auth", produces = MediaType.APPLICATION_JSON_VALUE)
    public String authKey(@Validated KeyAuthDto keyAuthDto) {
        Result result = userKeyService.auth(keyAuthDto);
        if (result.getCode() == 0) {
            return WebResult.success(result.getData());
        }

        return WebResult.result(result);
    }

    @Operation(summary = "获取上传文件存储的 oss-store 节点", description = "N")
    @GetMapping(value = "/upload/store", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getUploadStore(@RequestParam("channelCode") int channelCode) {
        ServerInfo serverInfo = consoleService.getUploadStore(channelCode);
        return WebResult.success(serverInfo);
    }

    @Operation(summary = "设置对象可见范围", description = "N")
    @PostMapping(value = "/object/scope", produces = MediaType.APPLICATION_JSON_VALUE)
    public String setObjectScope(int channelCode, String objectId, int scope) throws Exception {
        int loginUser = (int) UserContext.getUserId();
        storeServiceWrapper.setObjectScope(channelCode, objectId, scope, loginUser);
        return WebResult.success();
    }

    @Operation(summary = "根据 objectId 删除对象", description = "N")
    @PostMapping(value = "/object/delete/id", produces = MediaType.APPLICATION_JSON_VALUE)
    public String deleteByObjectId(@RequestParam("channelCode") int channelCode,
                                   @RequestParam("objectId") String objectId) throws Exception {
        int loginUser = (int) UserContext.getUserId();
        storeServiceWrapper.deleteByObjectId(channelCode, objectId, loginUser);
        return WebResult.success();
    }

    @Operation(summary = "根据 objectUrl 删除对象", description = "N")
    @PostMapping(value = "/object/delete/url", produces = MediaType.APPLICATION_JSON_VALUE)
    public String deleteByObjectUrl(@RequestParam("objectUrl") String objectUrl) {
        int loginUser = (int) UserContext.getUserId();
        storeServiceWrapper.deleteByObjectUrl(objectUrl, loginUser);
        return WebResult.success();
    }

    @Operation(summary = "获取对象信息", description = "N")
    @GetMapping(value = "/object/info", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getObjectInfo(@RequestParam("channelCode") int channelCode,
                                @RequestParam("objectId") String objectId) throws Exception {
        int loginUser = (int) UserContext.getUserId();
        ObjectInfo objectInfo = storeServiceWrapper.getObjectInfo(channelCode, objectId, loginUser);
        return WebResult.success(objectInfo);
    }

    @Operation(summary = "获取对象签名 url", description = "N")
    @GetMapping(value = "/object/url", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getSignedUrl(@RequestParam("channelCode") Integer channelCode,
                               @RequestParam("objectId") String objectId) throws Exception {
        int loginUser = (int) UserContext.getUserId();
        String signedUrl  = storeServiceWrapper.getSignedUrl(channelCode, objectId, loginUser);
        return WebResult.success(signedUrl);
    }

    @Operation(summary = "根据对象 url 获取签名 url", description = "N")
    @GetMapping(value = "/object/signed_url", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getSignedUrlByUrl(@RequestParam("channelCode") Integer channelCode,
                                    @RequestParam("objectUrl") String objectUrl) throws Exception {
        int loginUser = (int) UserContext.getUserId();
        String signedUrl  = storeServiceWrapper.getSignedUrlByUrl(channelCode, objectUrl, loginUser);
        return WebResult.success(signedUrl);
    }

    @Operation(summary = "获取视频文件信息", description = "N")
    @GetMapping(value = "/object/video/info", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getVideoInfo(@RequestParam("channelCode") Integer channelCode,
                               @RequestParam("objectId") String objectId) throws Exception {
        int loginUser = (int) UserContext.getUserId();
        VideoInfo videoInfo = storeServiceWrapper.getVideoInfo(channelCode, objectId, loginUser);
        return WebResult.success(videoInfo);
    }

    @Operation(summary = "获取图片文件信息", description = "N")
    @GetMapping(value = "/object/image/info", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getImagesInfo(@RequestParam("channelCode") Integer channelCode,
                                @RequestParam("objectId") String objectId) throws Exception {
        int loginUser = (int) UserContext.getUserId();
        ImageInfo imageInfo = storeServiceWrapper.getImageInfo(channelCode, objectId, loginUser);
        return WebResult.success(imageInfo);
    }

    @Operation(summary = "获取 webp 图片文件信息", description = "N")
    @PostMapping(value = "/object/image/webp", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getWebpInfo(@RequestParam("channelCode") Integer channelCode,
                              @RequestParam("objectId") String objectId) throws Exception {
        int loginUser = (int) UserContext.getUserId();
        ConvertedImageInfo convertedImageInfo = storeServiceWrapper.getWebpInfo(channelCode, objectId, loginUser);
        return WebResult.success(convertedImageInfo);
    }

    @Operation(summary = "获取音频文件信息", description = "N")
    @GetMapping(value = "/media/audio/info", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getAudioInfo(@RequestParam("channelCode") Integer channelCode,
                               @RequestParam("objectId") String objectId) throws Exception {
        int loginUser = (int) UserContext.getUserId();
        AudioInfo audioInfo = storeServiceWrapper.getAudioInfo(channelCode, objectId, loginUser);
        return WebResult.success(audioInfo);
    }
}
