package com.sonic.shark.service.impl;

import com.google.common.collect.Lists;
import com.sonic.shark.domain.entity.BlurryImgRecord;
import com.sonic.shark.enums.S3BucketNameEnum;
import com.sonic.shark.lib.input.CopyAndBlurryImgInput;
import com.sonic.shark.lib.output.CopyAndBlurryImgOutput;
import com.sonic.shark.service.BlurryImgRecordService;
import com.sonic.shark.service.CopyAndBlurryImgService;
import com.sonic.shark.service.OssService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * @Author code
 * @Description 拷贝并且模糊图片
 */
@Slf4j
@Service
public class CopyAndBlurryImgServiceImpl implements CopyAndBlurryImgService {

    @Value("${spring.profiles.active}")
    private String runMode;

    @Autowired
    private OssService ossService;

    @Autowired
    private BlurryImgRecordService blurryImgRecordService;

    /**
     * 参考Onlyfans设置的目录路径
     */
    private static final String TARGET_PATH = "files/b/";

    @Override
    public List<CopyAndBlurryImgOutput> copyAndBlurryImg(List<CopyAndBlurryImgInput> inputList) throws Exception {
        S3BucketNameEnum s3BucketNameEnum = S3BucketNameEnum.BLURRY;
        List<CopyAndBlurryImgOutput> outputList = Lists.newArrayList();
        //可以用异步线程池来处理并返回结果优化执行速度
        for (CopyAndBlurryImgInput input : inputList) {
            //根据业务类型获取源图片存储的bucket
            S3BucketNameEnum sourceS3BucketNameEnum = S3BucketNameEnum.SONIC_SHARK;
            //处理图片的相对路径
            if(StringUtils.isNotEmpty(input.getSourceFileUrl())) {
                //根据业务类型解析出相对路径
                input.setSourceFilePath(input.getSourceFileUrl().replace(sourceS3BucketNameEnum.getRootUrl(), ""));
            }
            //处理源文件的前缀多一个/的问题
            input.setSourceFilePath(input.getSourceFilePath().startsWith("/") ? input.getSourceFilePath().substring(1) : input.getSourceFilePath());
            //获取后缀
            String suffix = input.getSourceFilePath().substring(input.getSourceFilePath().lastIndexOf("."));
            //随机生成文件名称，用UUID吧
            String targetFilePath = TARGET_PATH + runMode + "/" + input.getUserId() + "/" + UUID.randomUUID().toString().replace("-", "") + suffix;
            ossService.copyFile(sourceS3BucketNameEnum, input.getSourceFilePath(), s3BucketNameEnum, targetFilePath);
            //保存数据
            BlurryImgRecord record = blurryImgRecordService.saveRecord(s3BucketNameEnum, input.getSourceFilePath(), targetFilePath, input.getBizType().name(), input.getBizId(), input.getUserId());
            CopyAndBlurryImgOutput output = new CopyAndBlurryImgOutput();
            output.setBizId(record.getBizId());
            output.setSourceImgPath(input.getSourceFilePath());
            output.setImgPath(record.getImgPath());
            output.setImg1(record.getImg1());
            output.setImg2(record.getImg2());
            output.setImg3(record.getImg3());
            output.setImgUrl(s3BucketNameEnum.getRootUrl() + record.getImgPath());
            outputList.add(output);
        }
        return outputList;
    }

}
