package com.friday.executor.common;

import com.friday.base.common.response.DubboResult;
import com.friday.base.file.dto.FileDownloadDto;
import com.friday.base.file.dto.FileUploadResponseDto;
import com.friday.base.file.service.COSPlatformFileService;
import com.friday.base.web.service.WebFileService;
import static com.friday.operation.api.distributor.constant.FileTransferType.*;
import com.friday.executor.util.FileUtils;
import com.friday.operation.api.distributor.dto.FileInfoDto;
import com.friday.operation.api.executor.service.IFileService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Objects;

/**
 * @Author YaoYao
 * @Date Created In 13:57 2023/4/4
 * @Description 资讯接口服务
 * @Modified By
 */
@Service
@Slf4j
public class FileHelper implements IFileService {
    @Autowired
    private WebFileService webFileService;

    @Autowired
    private COSPlatformFileService cosPlatformFileService;

    @Override
    public DubboResult<String> upload(FileInfoDto localFile) {
        if(StringUtils.isAnyEmpty(localFile.getTransferType(), localFile.getFilePath(), localFile.getFileName())){
            return DubboResult.newFailure(DubboResult.CLIENT_EXCEPTION, "上传文件入参无效");
        }
        String fileKey = "";
        try {
            if(GATEWAY.getType().equals(localFile.getTransferType())){
                fileKey = this.uploadFileByGateway(localFile.getFilePath(), localFile.getFileName());
                return DubboResult.newSuccess(fileKey);
            }
            return DubboResult.newFailure(DubboResult.CLIENT_EXCEPTION, "不支持的文件传输类型");
        } catch (Exception e){
            log.error("执行器Dubbo上传文件异常", e);
            return DubboResult.newFailure(DubboResult.SERVER_EXCEPTION, "上传文件异常");
        }
    }

    //上传文件至存储网关
    private String uploadFileByGateway(String filePath, String fileName) throws Exception {
        File file = new File(filePath);
        HashMap<String, byte[]> fileDataMap = new HashMap<>();
        fileDataMap.put(fileName, FileUtils.fileToBinArray(file));
        final DubboResult<ArrayList<FileUploadResponseDto>> uploadResult = cosPlatformFileService.uploadFileByCOS(fileDataMap);
        if(uploadResult.success()){
            return uploadResult.getObject().get(0).getFileKey();
        }
        throw new Exception("上传文件至COS失败！");
    }

    @Override
    public DubboResult<String> download(FileInfoDto fileInfo) {
        if(StringUtils.isAnyEmpty(fileInfo.getTransferType(), fileInfo.getFileKey(), fileInfo.getFilePath())){
            return DubboResult.newFailure(DubboResult.CLIENT_EXCEPTION, "下载文件入参无效");
        }
        try {
            if(GATEWAY.getType().equals(fileInfo.getTransferType())){
                String fileDir = fileInfo.getFilePath();
                String fileName = this.downloadFileByGateWay(fileInfo.getFileKey(), fileDir);
                return DubboResult.newSuccess(fileDir + fileName);
            }
            return DubboResult.newFailure(DubboResult.CLIENT_EXCEPTION, "不支持的文件传输类型");
        } catch (Exception e){
            log.error("执行器Dubbo下载文件异常", e);
            return DubboResult.newFailure(DubboResult.SERVER_EXCEPTION, "下载文件异常");
        }
    }

    //下载存储网关文件
    private String downloadFileByGateWay(String fileKey, String fileDir) throws Exception {

        final DubboResult<FileDownloadDto> downloadFileByCOS = cosPlatformFileService.downloadFileByCOS(fileKey);
        if(!downloadFileByCOS.success()){
            throw new Exception("获取存储网关文件失败");
        }
        try(FileOutputStream outputStream = new FileOutputStream(fileDir)){
            outputStream.write(downloadFileByCOS.getObject().getBytes());
        }
        return downloadFileByCOS.getObject().getFileName();
    }

}
