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

import cn.reghao.file.api.iface.FileService;
import cn.reghao.jutil.jdk.web.db.PageList;
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.common.auth.UserContext;
import cn.reghao.tnb.file.app.zoss.model.po.UserNode;
import cn.reghao.tnb.file.app.zoss.model.vo.FileInfo;
import cn.reghao.tnb.file.app.service.OssFileService;
import cn.reghao.tnb.file.app.zoss.service.UploadChannelService;
import cn.reghao.tnb.file.app.zoss.service.UserNodeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author reghao
 * @date 2024-02-27 09:12:14
 */
@Tag(name = "存储对象接口")
@RestController
@RequestMapping("/api/oss/object")
public class StoreObjectController {
    private final ConsoleService consoleService;
    private final OssFileService ossFileService;
    private final UserNodeService userNodeService;
    private final UploadChannelService uploadChannelService;
    private final FileService fileService;

    public StoreObjectController(ConsoleService consoleService, OssFileService ossFileService,
                                 UserNodeService userNodeService, UploadChannelService uploadChannelService,
                                 FileService fileService) {
        this.consoleService = consoleService;
        this.ossFileService = ossFileService;
        this.userNodeService = userNodeService;
        this.uploadChannelService = uploadChannelService;
        this.fileService = fileService;
    }

    @Operation(summary = "对象列表页面", description = "N")
    @GetMapping(value = "/list", produces = MediaType.APPLICATION_JSON_VALUE)
    public String index(@RequestParam(value = "userNodeId", required = false) Integer userNodeId,
                        @RequestParam(value = "path", required = false) String path) {
        if (path == null || path.isBlank()) {
            path = "/";
        }

        int loginUser = (int) UserContext.getUserId();
        List<UserNode> userNodes = userNodeService.getUserNodes(loginUser);
        if (userNodes.isEmpty()) {
            String errMsg = "没有可用节点";
            WebResult.failWithMsg(errMsg);
        }

        UserNode userNode;
        if (userNodeId != null) {
            userNode = userNodeService.getUserNode(userNodeId);
        } else {
            userNode = userNodes.get(0);
            userNodeId = userNode.getId();
        }
        String nodeDomain = userNode.getDomain();

        int pn = 1;
        int ps = 10;
        String objectName = "fileService.getObjectName(path)";
        PageList<FileInfo> pageList = ossFileService.getFileList(nodeDomain, objectName, pn);
        /*model.addAttribute("parentDirs", Collections.emptyList());
        model.addAttribute("page", pageList);
        model.addAttribute("userNodeId", userNodeId);
        model.addAttribute("storeNodes", userNodes);
        model.addAttribute("uploadPage", "/admin/oss/object/upload");
        model.addAttribute("showTitle", 1);*/
        return WebResult.success(pageList);
    }

    @Operation(summary = "对象详情页面", description = "N")
    @GetMapping(value = "/detail", produces = MediaType.APPLICATION_JSON_VALUE)
    public String objectInfoPage(@RequestParam(value = "userNodeId") Integer userNodeId,
                                 @RequestParam(value = "objectId") String objectId) throws Exception {
        ObjectInfo objectInfo = ossFileService.getObjectInfo(userNodeId, objectId);
        return WebResult.success(objectInfo);
    }

    @Operation(summary = "获取对象上传需要的 token", description = "N")
    @PostMapping(value = "/serverinfo", produces = MediaType.APPLICATION_JSON_VALUE)
    public String uploadFile(Integer channelCode) {
        ServerInfo serverInfo = consoleService.getUploadStore(channelCode);
        return WebResult.success(serverInfo);
    }
}
