package cn.reghao.oss.store.service;

import cn.reghao.jutil.jdk.security.DigestUtil;
import cn.reghao.oss.store.config.props.OssProperties;
import cn.reghao.oss.store.db.repository.ObjectRepository;
import cn.reghao.oss.store.model.po.DataBlock;
import cn.reghao.oss.store.model.po.FileMeta;
import cn.reghao.oss.store.model.vo.ObjectProp;
import cn.reghao.oss.store.model.vo.ObjectResult;
import cn.reghao.oss.store.util.FileType;
import cn.reghao.oss.store.util.StringUtil;
import cn.reghao.oss.storeapi.dto.ObjectMeta;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.List;
import java.util.UUID;

/**
 * @author reghao
 * @date 2023-05-10 13:28:18
 */
@Slf4j
@Service
public class PutObjectService {
    private final ObjectRepository objectRepository;
    private final ObjectNameService objectNameService;
    private final String nodeAddress;

    public PutObjectService(ObjectRepository objectRepository, ObjectNameService objectNameService,
                            OssProperties ossProperties) {
        this.objectRepository = objectRepository;
        this.objectNameService = objectNameService;
        this.nodeAddress = ossProperties.getHost();
    }

    public ObjectResult putObject(ObjectProp objectProp, String contentId, File savedFile, String originalFilename, String sha256sum) {
        String pid = objectProp.getPid();
        String objectName = objectProp.getObjectName();
        FileMeta fileMeta = objectRepository.getBySha256sum(sha256sum);
        if (fileMeta != null) {
            FileUtils.deleteQuietly(savedFile);
            return copyObject(objectProp, originalFilename, fileMeta);
        } else {
            String savedPath = savedFile.getAbsolutePath();
            long size = savedFile.length();
            String objectId = UUID.randomUUID().toString().replace("-", "");
            String contentType = FileType.getMediaType(savedPath);
            int fileType = FileType.getFileType(contentType);
            int scope = objectProp.getScope();
            fileMeta = new FileMeta(objectName, objectId, contentId, originalFilename, size,
                    fileType, contentType, sha256sum, pid, scope);
            String blockId = UUID.randomUUID().toString();
            List<DataBlock> list = List.of(new DataBlock(contentId, blockId, nodeAddress, savedPath, size));
            objectRepository.saveObject(fileMeta, list);
            return new ObjectResult(objectName, objectId, fileType, savedPath);
        }
    }

    public ObjectResult putObject(String originalObjectName, String contentId, String suffix, File savedFile) throws Exception {
        ObjectProp objectProp = objectNameService.getObjectProp(originalObjectName, suffix);
        String sha256sum = DigestUtil.sha256sum(savedFile.getAbsolutePath());
        return putObject(objectProp, contentId, savedFile, "", sha256sum);
    }

    public void putObject(String objectName, byte[] bytes) {
    }

    public ObjectResult copyObject(ObjectProp objectProp, String filename, FileMeta fileMeta) {
        String dupObjectId = fileMeta.getObjectId();
        ObjectMeta objectMeta = objectRepository.getObjectMetaByName(fileMeta.getObjectName());

        int fileType = fileMeta.getFileType();
        String savedPath = objectMeta.getAbsolutePath();
        String objectName = objectProp.getObjectName();
        String objectId = UUID.randomUUID().toString().replace("-", "");
        int scope = objectProp.getScope();
        FileMeta fileMeta1 = new FileMeta(objectName, objectId, filename, fileMeta, scope);
        objectRepository.saveFileMeta(fileMeta1);
        return new ObjectResult(objectName, objectId, fileType, savedPath, dupObjectId);
    }

    public ObjectResult copyFromObjectName(String fromObjectName) {
        FileMeta fileMeta = objectRepository.getByObjectName(fromObjectName);
        int fileType = fileMeta.getFileType();

        String suffix = StringUtil.getSuffix(fromObjectName);
        String filename = fileMeta.getFilename();
        String savedPath = "";
        int scope = fileMeta.getScope();
        String toObjectName = objectNameService.getObjectNameFromOriginal(fromObjectName, suffix);
        String toObjectId = UUID.randomUUID().toString().replace("-", "");
        FileMeta fileMeta1 = new FileMeta(toObjectName, toObjectId, filename, fileMeta, scope);
        objectRepository.saveFileMeta(fileMeta1);
        return new ObjectResult(toObjectName, toObjectId, fileType, savedPath);
    }

    public ObjectResult copyFromObjectId(String fromObjectId) {
        FileMeta fileMeta = objectRepository.getByObjectId(fromObjectId);
        int fileType = fileMeta.getFileType();

        String fromObjectName = fileMeta.getObjectName();
        String suffix = StringUtil.getSuffix(fromObjectName);
        String filename = fileMeta.getFilename();
        String savedPath = "";
        int scope = fileMeta.getScope();
        String toObjectName = objectNameService.getObjectNameFromOriginal(fromObjectName, suffix);
        String toObjectId = UUID.randomUUID().toString().replace("-", "");
        FileMeta fileMeta1 = new FileMeta(toObjectName, toObjectId, filename, fileMeta, scope);
        objectRepository.saveFileMeta(fileMeta1);
        return new ObjectResult(toObjectName, toObjectId, fileType, savedPath);
    }

    public void deleteObject(String objectId) {
        FileMeta fileMeta = objectRepository.getByObjectId(objectId);
        objectRepository.deleteObject(fileMeta);
    }
}
