package com.bonus.oss.service.impl;

import com.alibaba.nacos.common.utils.UuidUtils;
import com.aliyun.oss.model.OSSObject;
import com.bonus.common.core.domain.R;
import com.bonus.common.core.utils.StringUtils;
import com.bonus.common.core.utils.file.FileUtils;
import com.bonus.oss.domain.OssInfo;
import com.bonus.oss.service.OssService;
import com.bonus.oss.utils.OssUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Service
public class OssServiceImpl implements OssService {

    @Resource
    private OssUtils ossUtils;


    /**
     * 文件上传
     *
     * @param file 文件流
     * @return 文件信息
     */
    @Override
    public R<OssInfo> uploadFile(MultipartFile file) {
        if (ObjectUtils.isEmpty(file)) {
            return R.fail("File is null.");
        }
        try {
            String originalFilename = Objects.requireNonNull(file.getOriginalFilename(), "文件名不能为空");
            String extension = originalFilename.substring(originalFilename.lastIndexOf('.'));
            String objectKey = UuidUtils.generateUuid() + extension;
            objectKey = FileUtils.generateObjectName(objectKey);
            return ossUtils.upload(objectKey, FileUtils.multipartFileToFile(file));
        } catch (Exception e) {
            return R.fail("File upload failed.");
        }
    }

    /**
     * 文件下载
     *
     * @param objectKey oss文件存储地址
     */
    @Override
    public R<OSSObject> downloadFile(String objectKey) {
        if (ObjectUtils.isEmpty(objectKey)) {
            return R.fail("objectKey is null.");
        }
        return ossUtils.download(objectKey);
    }

    /**
     * 文件删除
     *
     * @param objectKey oss文件存储地址
     */
    @Override
    public R<T> deleteFile(String objectKey) {
        try {
            if (ObjectUtils.isNotEmpty(objectKey)) {
                return ossUtils.delete(objectKey);
            } else {
                return R.fail("objectKey is null.");
            }
        } catch (Exception e) {
            return R.fail("File delete failed.");
        }
    }

    /**
     * 多文件上传
     *
     * @param files 文件流
     * @return 文件信息
     */
    @Override
    public R<List<OssInfo>> uploadFiles(MultipartFile[] files) {
        try {
            List<OssInfo> ossInfos = new ArrayList<>();
            for (MultipartFile multipartFile : files) {
                String originalFilename = Objects.requireNonNull(multipartFile.getOriginalFilename(), "文件名不能为空");
                String extension = originalFilename.substring(originalFilename.lastIndexOf('.'));
                String objectKey = UuidUtils.generateUuid() + extension;
                File file = FileUtils.multipartFileToFile(multipartFile);
                objectKey = FileUtils.generateObjectName(objectKey);
                ossInfos.add(ossUtils.upload(objectKey, file).getData());
            }
            return R.ok(ossInfos, "文件上传成功");
        } catch (Exception e) {
            return R.fail("File upload failed.");
        }
    }
}
