package ctbrec.recorder.postprocessing;

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

import java.io.IOException;
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 RenameSingleFileTest extends AbstractPpTest {

    @Test
    public void testOriginalFileReplacement() throws IOException {
        Config config = mockConfig();
        Recording rec = new Recording();
        rec.setModel(mockModel());
        rec.setAbsoluteFile(original);
        rec.setStartDate(now);
        rec.setSingleFile(true);
        Rename pp = new Rename();
        pp.postprocess(rec, recordingManager, config);

        Matcher m = Pattern.compile("Mockita_Boobilicious_\\d{4}-\\d{2}-\\d{2}_\\d{2}-\\d{2}-\\d{2}\\.ts").matcher(rec.getAbsoluteFile().getName());
        assertTrue(m.matches());
        assertEquals(rec.getAbsoluteFile(), rec.getPostProcessedFile());
        assertNotEquals(rec.getAbsoluteFile(), original);
    }

    @Test
    public void testEarlyExit() throws IOException {
        Config config = mockConfig();
        Model model = mockModel();
        Recording rec = mock(Recording.class);
        when(rec.getModel()).thenReturn(model);
        when(rec.getAbsoluteFile()).thenReturn(original);
        when(rec.getPostProcessedFile()).thenReturn(original);
        when(rec.getStartDate()).thenReturn(now);
        doThrow(new RuntimeException("Unexpected call of setAbsoluteFile")).when(rec).setAbsoluteFile(any());
        Rename pp = new Rename();
        pp.getConfig().put(Rename.FILE_NAME_TEMPLATE, original.getName());
        pp.postprocess(rec, recordingManager, config);
    }

    @Test
    public void absoluteFileShouldKeepBeingOriginalIfFilesDiffer() throws IOException {
        Config config = mockConfig();
        Model model = mockModel();
        Recording rec = mock(Recording.class);
        when(rec.getModel()).thenReturn(model);
        when(rec.getAbsoluteFile()).thenReturn(original);
        when(rec.getPostProcessedFile()).thenReturn(postProcessed);
        when(rec.getStartDate()).thenReturn(now);
        doThrow(new RuntimeException("Unexpected call of setAbsoluteFile")).when(rec).setAbsoluteFile(any());
        Rename pp = new Rename();
        pp.postprocess(rec, recordingManager, config);
    }

    @Test
    public void testToString() {
        Rename pp = new Rename();
        assertEquals("rename", pp.toString());

        pp.getConfig().put(Rename.FILE_NAME_TEMPLATE, Rename.DEFAULT);
        assertEquals("rename [${modelSanitizedName}_${localDateTime}.${fileSuffix}]", pp.toString());
    }
}
