package cn.reghao.oss.store.rpc;

import cn.reghao.oss.storeapi.constant.ObjectScope;
import cn.reghao.oss.store.db.repository.ObjectRepository;
import cn.reghao.oss.store.model.po.FileMeta;
import cn.reghao.oss.store.service.ObjectNameService;
import cn.reghao.oss.storeapi.dto.ObjectInfo;
import cn.reghao.oss.storeapi.iface.ObjectService;
import cn.reghao.oss.store.db.mapper.FileMetaMapper;
import cn.reghao.oss.store.service.SignService;
import cn.reghao.oss.store.service.StoreLocalService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author reghao
 * @date 2023-05-28 23:26:03
 */
@DubboService
@Service
public class ObjectServiceImpl implements ObjectService {
    private final FileMetaMapper fileMetaMapper;
    private final ObjectNameService objectNameService;
    private final ObjectRepository objectRepository;
    private final SignService signService;
    private final StoreLocalService storeLocalService;

    public ObjectServiceImpl(FileMetaMapper fileMetaMapper, ObjectNameService objectNameService,
                             ObjectRepository objectRepository, SignService signService,
                             StoreLocalService storeLocalService) {
        this.fileMetaMapper = fileMetaMapper;
        this.objectNameService = objectNameService;
        this.objectRepository = objectRepository;
        this.signService = signService;
        this.storeLocalService = storeLocalService;
    }

    @Override
    public void setObjectsScope(int scope, List<String> objectIds) {
        if (objectIds.isEmpty()) {
            return;
        }

        objectIds.forEach(objectId -> {
            FileMeta fileMeta = objectRepository.getByObjectId(objectId);
            objectRepository.updateObjectScope(scope, fileMeta.getObjectName());
        });
    }

    @Override
    public void deleteByObjectIds(List<String> objectIds) {
        objectIds.forEach(objectId -> {
            FileMeta fileMeta = objectRepository.getByObjectId(objectId);
            objectRepository.deleteObject(fileMeta);
        });
    }

    @Override
    public void deleteByObjectName(String objectName) {
        FileMeta fileMeta = objectRepository.getByObjectName(objectName);
        objectRepository.deleteObject(fileMeta);
    }

    @Override
    public String getSignedUrl(int loginUser, String objectId, int expireSecond) {
        FileMeta fileMeta = objectRepository.getByObjectId(objectId);
        String objectName = fileMeta.getObjectName();
        String url = objectNameService.getObjectUrl(objectName);
        String signedUrl = signService.getSignedUrl(loginUser, url, expireSecond);
        return signedUrl;
    }

    @Override
    public ObjectInfo getObjectInfo(int loginUser, String objectId, int expireSecond) {
        FileMeta fileMeta = fileMetaMapper.findByObjectId(objectId);
        if (fileMeta == null) {
            return null;
        }

        String objectName = fileMeta.getObjectName();
        int fileType = fileMeta.getFileType();
        String filename = fileMeta.getFilename();
        long size = fileMeta.getSize();
        ObjectInfo objectInfo = new ObjectInfo(objectId, objectName, fileType, filename, size);
        String url = String.format("//%s/%s", storeLocalService.getDomain(), objectName);
        int scope = fileMeta.getScope();
        if (scope != ObjectScope.PUBLIC.getCode()) {
            String signedUrl = signService.getSignedUrl(loginUser, url, expireSecond);
            objectInfo.setUrl(signedUrl);
        } else {
            objectInfo.setUrl(url);
        }

        return objectInfo;
    }
}
