package cn.reghao.oss.store.service;

import cn.reghao.oss.storeapi.dto.ObjectChannel;
import cn.reghao.oss.store.db.repository.ObjectRepository;
import cn.reghao.oss.store.model.po.FileMeta;
import cn.reghao.oss.store.model.vo.ObjectProp;
import cn.reghao.oss.store.util.StringUtil;
import org.springframework.stereotype.Service;

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

/**
 * @author reghao
 * @date 2023-06-02 16:22:50
 */
@Service
public class ObjectNameService {
    private final ObjectRepository objectRepository;
    private final StoreLocalService storeLocalService;

    public ObjectNameService(ObjectRepository objectRepository, StoreLocalService storeLocalService) {
        this.objectRepository = objectRepository;
        this.storeLocalService = storeLocalService;
    }

    public String getObjectUrl(String objectName) {
        return String.format("//%s/%s", storeLocalService.getDomain(), objectName);
    }

    public ObjectProp getObjectProp(ObjectChannel channel, String filename) throws Exception {
        int scope = channel.getScope();
        String suffix = StringUtil.getSuffix(filename);
        String objectPrefix = channel.getPrefix();
        String objectName = objectPrefix + UUID.randomUUID().toString().replace("-", "") + suffix;
        FileMeta fileMeta = objectRepository.getByObjectName(objectPrefix);
        if (fileMeta == null) {
            String errMsg = String.format("objectPrefix %s 不合法", objectPrefix);
            throw new Exception(errMsg);
        }

        String pid = fileMeta.getObjectId();
        return new ObjectProp(objectName, scope, pid);
    }

    public ObjectProp getObjectProp(String originalObjectName, String suffix) {
        int idx = originalObjectName.lastIndexOf("/");
        String prefix = originalObjectName.substring(0, idx+1);
        FileMeta fileMeta = objectRepository.getByObjectName(originalObjectName);
        int scope = fileMeta.getScope();
        String pid = fileMeta.getPid();

        String objectName = prefix + UUID.randomUUID().toString().replace("-", "") + suffix;
        return new ObjectProp(objectName, scope, pid);
    }

    public String getObjectNameFromOriginal(String originalObjectName, String suffix) {
        int idx = originalObjectName.lastIndexOf("/");
        String prefix = originalObjectName.substring(0, idx+1);
        String id = UUID.randomUUID().toString().replace("-", "");
        return prefix + id + suffix;
    }

    public void createParentDirs(String objectName, int scope) {
        List<String> list = getSortedParent(objectName);
        for (int i = 0; i < list.size(); i++) {
            String parentName = list.get(i);
            FileMeta fileMeta = objectRepository.getByObjectName(parentName);
            if (fileMeta == null) {
                String pid;
                if (i == 0) {
                    pid = "0";
                } else {
                    pid = objectRepository.getByObjectName(list.get(i-1)).getObjectId();
                }
                String objectId = UUID.randomUUID().toString().replace("-", "");
                String[] names = parentName.split("/");
                String filename = names[names.length-1];
                objectRepository.saveFileMeta(new FileMeta(parentName, objectId, filename, pid, scope));
            }
        }
    }

    private List<String> getSortedParent(String objectName) {
        String[] arr = objectName.split("/");
        List<String> list = new ArrayList<>();
        list.add(arr[0] + "/");
        for (int i = 1; i < arr.length; i++) {
            list.add(list.get(i-1) + arr[i] + "/");
        }
        return list;
    }
}
