package org.springblade.etl.util;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import cn.hutool.core.lang.UUID;

@Component
public class SystemKit implements InitializingBean, DisposableBean {

    private static String workspace;

    @Value("${platomix.etl.tempWorkspace}")
    public void setWorkspace(String workspace) {
        SystemKit.workspace = workspace;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        FileKit.deleteDir(Paths.get(workspace));
    }

    @Override
    public void destroy() throws Exception {
//        FileKit.deleteDir(Paths.get(workspace));
    }

    public static Path getWorkspace() throws IOException {
        return FileKit.createDir(Paths.get(workspace));
    }

    public static Path getRandomWorkspace() throws IOException {
        return FileKit.createDir(getWorkspace().resolve(UUID.fastUUID().toString(true)));
    }

    public static Path getSavepointDir(String path) throws IOException{
        return FileKit.createDir(getWorkspace().resolve(path));
    }
}
