package ctbrec.recorder.postprocessing;

import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.junit.Test;

import ctbrec.Config;
import ctbrec.Model;
import ctbrec.Recording;

public class MoveDirectoryTest extends AbstractPpTest {

    @Test
    public void testOriginalFileReplacement() throws IOException {
        Config config = mockConfig();
        Recording rec = new Recording();
        rec.setModel(mockModel());
        rec.setAbsoluteFile(originalDir);
        rec.setStartDate(now);
        rec.setSingleFile(false);
        Move pp = new Move();
        pp.getConfig().put(Move.PATH_TEMPLATE, new File(baseDir.toFile(), Move.DEFAULT).getAbsolutePath());
        pp.postprocess(rec, recordingManager, config);

        Matcher m = Pattern.compile(baseDir.toString() + "/Mockita_Boobilicious/\\d{4}-\\d{2}-\\d{2}_\\d{2}-\\d{2}-\\d{2}/original").matcher(rec.getAbsoluteFile().getCanonicalPath());
        assertTrue(m.matches());
        assertEquals(rec.getAbsoluteFile(), rec.getPostProcessedFile());
        assertNotEquals(rec.getAbsoluteFile(), original);
    }

    @Test
    public void absoluteFileShouldKeepBeingOriginalIfFilesDiffer() throws IOException {
        Model model = mockModel();
        Recording rec = mock(Recording.class);
        when(rec.getModel()).thenReturn(model);
        when(rec.getAbsoluteFile()).thenReturn(originalDir);
        when(rec.getPostProcessedFile()).thenReturn(postProcessedDir);
        when(rec.getStartDate()).thenReturn(now);
        doThrow(new RuntimeException("Unexpected call of setAbsoluteFile")).when(rec).setAbsoluteFile(any());

        Files.createDirectories(postProcessedDir.toPath());
        Move pp = new Move();
        Config config = mockConfig();
        pp.getConfig().put(Move.PATH_TEMPLATE, new File(baseDir.toFile(), Move.DEFAULT).getAbsolutePath());
        pp.postprocess(rec, recordingManager, config);
    }
}
