package ctbrec.recorder.postprocessing;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ctbrec.Config;
import ctbrec.Recording;
import ctbrec.recorder.RecordingManager;

public class Rename extends AbstractPlaceholderAwarePostProcessor {

    private static final Logger LOG = LoggerFactory.getLogger(Rename.class);
    public static final String FILE_NAME_TEMPLATE = "filename.template";
    public static final String DEFAULT = "${modelSanitizedName}_${localDateTime}.${fileSuffix}";
    public static final String DEFAULT_DIR = "${modelSanitizedName}_${localDateTime}";

    @Override
    public String getName() {
        return "rename";
    }

    @Override
    public void postprocess(Recording rec, RecordingManager recordingManager, Config config) throws IOException {
        String defaultTemplate = rec.isSingleFile() ? DEFAULT : DEFAULT_DIR;
        String filenameTemplate = getConfig().getOrDefault(FILE_NAME_TEMPLATE, defaultTemplate);
        String filename = fillInPlaceHolders(filenameTemplate, rec, config);
        File src = rec.getPostProcessedFile();
        File target = new File(src.getParentFile(), filename);
        if (Objects.equals(src, target)) {
            return;
        }
        LOG.info("Renaming {} to {}", src.getName(), target.getName());
        Files.move(rec.getPostProcessedFile().toPath(), target.toPath());
        rec.setPostProcessedFile(target);
        if (Objects.equals(src, rec.getAbsoluteFile())) {
            rec.setAbsoluteFile(target);
        }
        rec.getAssociatedFiles().remove(src.getCanonicalPath());
        rec.getAssociatedFiles().add(target.getCanonicalPath());
    }

    @Override
    public String toString() {
        String s = getName();
        if (getConfig().containsKey(FILE_NAME_TEMPLATE)) {
            s += " [" + getConfig().get(FILE_NAME_TEMPLATE) + ']';
        }
        return s;
    }
}
