package ctbrec.recorder.postprocessing;

import java.io.IOException;
import java.time.Duration;

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

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

public class DeleteTooShort extends AbstractPostProcessor {

    private static final transient Logger LOG = LoggerFactory.getLogger(DeleteTooShort.class);
    public static final String MIN_LEN_IN_SECS = "minimumLengthInSeconds";

    @Override
    public String getName() {
        return "delete too short";
    }

    @Override
    public boolean postprocess(Recording rec, RecordingManager recordingManager, Config config) throws IOException {
        Duration minimumLengthInSeconds = Duration.ofSeconds(Integer.parseInt(getConfig().getOrDefault(MIN_LEN_IN_SECS, "0")));
        if (minimumLengthInSeconds.getSeconds() > 0) {
            Duration recordingLength = rec.getLength();
            if (recordingLength.isNegative()) {
                LOG.info("Video length couldn't be determined. Keeping the file!");
            } else {
                if (!recordingLength.isZero() && recordingLength.compareTo(minimumLengthInSeconds) < 0) {
                    LOG.info("Deleting too short recording {} [{} < {}]", rec, recordingLength, minimumLengthInSeconds);
                    recordingManager.delete(rec);
                    return false;
                }
            }
        }
        return true;
    }
}
