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

import cn.reghao.jutil.jdk.web.db.PageList;
import cn.reghao.jutil.jdk.web.result.Result;
import cn.reghao.tnb.common.web.WebResult;
import cn.reghao.tnb.common.auth.AuthUser;
import cn.reghao.tnb.common.auth.UserContext;
import cn.reghao.tnb.file.app.zdisk.model.dto.*;
import cn.reghao.tnb.file.app.zdisk.model.po.DiskFile;
import cn.reghao.tnb.file.app.zdisk.model.query.DiskQuery;
import cn.reghao.tnb.file.app.zdisk.model.vo.DiskFileDetail;
import cn.reghao.tnb.file.app.zdisk.model.vo.DiskInfo;
import cn.reghao.tnb.file.app.zdisk.model.vo.FileInfo;
import cn.reghao.tnb.file.app.zdisk.model.vo.NamePath;
import cn.reghao.tnb.file.app.zdisk.service.DiskFileService;
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.*;

import java.io.IOException;
import java.util.List;

/**
 * @author reghao
 * @date 2024-08-29 22:17:06
 */
@Tag(name = "网盘文件接口")
@RestController
@RequestMapping("/api/disk/file")
@AuthUser
public class DiskFileController {
    private int pageSize = 10;
    private final DiskFileService diskFileService;

    public DiskFileController(DiskFileService diskFileService) {
        this.diskFileService = diskFileService;
    }

    @Operation(summary = "添加网盘文件", description = "N")
    @PostMapping(value = "/add", produces = MediaType.APPLICATION_JSON_VALUE)
    public String addFile(@RequestBody @Validated UploadedFile uploadedFile) {
        Result result = diskFileService.addDiskFile(uploadedFile);
        return WebResult.result(result);
    }

    @Operation(summary = "删除文件/文件夹", description = "N")
    @PostMapping(value = "/delete", produces = MediaType.APPLICATION_JSON_VALUE)
    public String deleteFiles(@RequestBody @Validated DeleteFile deleteFile) {
        Result result = diskFileService.deleteDiskFile(deleteFile);
        return WebResult.result(result);
    }

    @Operation(summary = "网盘文件列表", description = "N")
    @GetMapping("/list")
    public String diskPage(@Validated SearchForm searchForm) {
        Integer pageNumber = searchForm.getPn();
        String path = searchForm.getPath();
        Integer fileType = searchForm.getFileType();
        String pid = "0";
        if (!"/".equals(path)) {
            DiskFile diskFile = diskFileService.findByPath(path);
            if (diskFile == null) {
                return WebResult.failWithMsg("not found diskFile");
            }
            pid = diskFile.getFileId();
        }


        long loginUser = UserContext.getUserId();
        DiskQuery.Builder builder  = new DiskQuery.Builder()
                .pageNumber(pageNumber)
                .pageSize(pageSize)
                .pid(pid)
                .owner(loginUser);
        if (fileType != null) {
            builder.fileType(fileType);
        }

        DiskQuery diskQuery = builder.build();
        PageList<FileInfo> pageList = diskFileService.findByDiskQuery(diskQuery);
        List<NamePath> namePathList = diskFileService.getPathList(path);
        DiskInfo diskInfo = new DiskInfo(pid, pageList, namePathList);
        return WebResult.success(diskInfo);
    }

    @Operation(summary = "预览文件", description = "N")
    @GetMapping(value = "/preview", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getPreviewFile(@RequestParam("fileId") String fileId) {
        DiskFileDetail diskFileDetail = diskFileService.getDiskFileDetail(fileId);
        if (diskFileDetail != null) {
            return WebResult.success(diskFileDetail);
        }

        return WebResult.failWithMsg("获取文件详情失败");
    }

    @Operation(summary = "重命名文件", description = "N")
    @PostMapping(value = "/rename", produces = MediaType.APPLICATION_JSON_VALUE)
    public String rename(@Validated RenameFile renameFile) throws IOException {
        /*long owner = UserContext.getUserId();
        String fileId = renameFile.getFileId();
        String newFilename = renameFile.getNewFilename();
        Result result = diskFileService.rename(fileId, newFilename, owner);
        return WebResult.result(result);*/
        return WebResult.successWithMsg("尚未实现");
    }

    @Operation(summary = "复制文件", description = "N")
    @PostMapping(value = "/copy", produces = MediaType.APPLICATION_JSON_VALUE)
    public String copyFiles(@RequestParam("fileId") List<String> fileIds, String pid) throws IOException {
        /*long owner = UserContext.getUserId();
        diskFileService.move(pid, fileIds, true, owner);*/
        return WebResult.successWithMsg("尚未实现");
    }

    @Operation(summary = "移动(多个)文件到指定文件夹", description = "N")
    @PostMapping(value = "/move", produces = MediaType.APPLICATION_JSON_VALUE)
    public String moveFiles(@RequestBody @Validated MoveFile moveFile) {
        Result result = diskFileService.move(moveFile);
        return WebResult.result(result);
    }

    @Operation(summary = "获取回收站中的文件列表", description = "N")
    @GetMapping("/trash/list")
    public String getTrashList() {
        return WebResult.successWithMsg("尚未实现");
    }

    @Operation(summary = "还原删除的文件", description = "N")
    @PostMapping(value = "/restore", produces = MediaType.APPLICATION_JSON_VALUE)
    public String restoreFiles(@RequestParam("fileId") List<String> fileIds) throws IOException {
        return WebResult.successWithMsg("尚未实现");
    }

    @Operation(summary = "清空回收站", description = "N")
    @PostMapping(value = "/erase", produces = MediaType.APPLICATION_JSON_VALUE)
    public String eraseFiles(@RequestParam("fileId") List<String> fileIds) throws IOException {
        return WebResult.successWithMsg("尚未实现");
    }
}
