package com.yfd.platform.modules.storage.controller.file;

import cn.hutool.core.bean.BeanUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.yfd.platform.config.ResponseResult;
import com.yfd.platform.exception.file.InvalidStorageSourceException;
import com.yfd.platform.modules.config.model.request.FileListRequest;
import com.yfd.platform.modules.storage.chain.FileChain;
import com.yfd.platform.modules.storage.chain.FileContext;
import com.yfd.platform.modules.storage.context.StorageSourceContext;
import com.yfd.platform.modules.storage.convert.StorageSourceConvert;
import com.yfd.platform.modules.storage.model.entity.StorageSource;
import com.yfd.platform.modules.storage.model.result.FileInfoResult;
import com.yfd.platform.modules.storage.model.result.FileItemResult;
import com.yfd.platform.modules.storage.model.result.StorageSourceResult;
import com.yfd.platform.modules.storage.service.StorageSourceService;
import com.yfd.platform.modules.storage.service.base.AbstractBaseFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 文件列表相关接口, 如展示存储源列表, 展示文件列表, 搜索文件列表等.
 *
 * @author zhengsl
 */
@Api(tags = "文件列表模块")
@ApiSort(2)
@Slf4j
@RequestMapping("/api/storage")
@RestController
public class FileController {

    @Resource
    private StorageSourceContext storageSourceContext;

    @Resource
    private StorageSourceService storageSourceService;

    @Resource
    private FileChain fileChain;

    @Resource
    private StorageSourceConvert storageSourceConvert;

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "获取存储源列表", notes = "获取所有已启用的存储源, 并且按照后台顺序排序")
    @GetMapping("/list")
    public ResponseResult storageList() {
        List<StorageSource> storageList = storageSourceService.findAllEnableOrderByOrderNum();
        List<StorageSourceResult> storageSourceResultList = storageSourceConvert.entityToResultList
                (storageList);
        return ResponseResult.successData(storageSourceResultList);
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "获取文件列表", notes = "获取某个存储源下, 指定路径的文件&文件夹列表")
    @PostMapping("/files")
    public ResponseResult list(@Valid @RequestBody FileListRequest fileListRequest) throws Exception {
        String storageKey = fileListRequest.getStorageKey();
        Integer storageId = storageSourceService.findIdByKey(storageKey);
        if (storageId == null) {
            throw new InvalidStorageSourceException("通过存储源 key 未找到存储源, key: " + storageKey);
        }

        // 处理请求参数默认值
        fileListRequest.handleDefaultValue();

        // 获取文件列表
        AbstractBaseFileService<?> fileService = storageSourceContext.getByStorageId(storageId);
        List<FileItemResult> fileItemList = fileService.fileList(fileListRequest.getPath());
        List<FileItemResult> fileItemListss = fileService.fileListData(fileListRequest.getPath(),  "4a547ee5b2b49ce2d9c2b95ee92766c2");

        // 执行责任链
        FileContext fileContext = FileContext.builder()
                .storageId(storageId)
                .fileListRequest(fileListRequest)
                .fileItemList(fileItemList).build();
        fileChain.execute(fileContext);

        return ResponseResult.successData(new FileInfoResult(fileContext.getFileItemList(),
                fileContext.getPasswordPattern()));
    }

}
