package extraction;

import java.io.File;
import java.time.LocalTime;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.PropertyConfigurator;
import org.junit.After;
import org.junit.AfterClass;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import constants.TestConstants;
import utils.AudioUtils;

public class TestSingleTrackExtractor {
	
	private final AudioUtils audioUtils = new AudioUtils();
	
	private SingleTrackExtractor extractor = null;
	
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		PropertyConfigurator.configure("log4j.properties");
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}

	@Before
	public void setUp() throws Exception {
		TestConstants.tempDir.mkdirs();
		extractor = new SingleTrackExtractor(TestConstants.tempDir);
	}

	@After
	public void tearDown() throws Exception {
		FileUtils.deleteDirectory(TestConstants.tempDir);
	}

	@Test
	public void should_return_empty_if_data_out_of_range() {
			
		File track = extractor.extract(TestConstants.testAudioFile, LocalTime.of(0, 2, 20), LocalTime.of(0, 0, 20), "track.mp3");
		
		assertEquals(0, this.audioUtils.getMp3DurationInSeconds(track), 1);
	}
	
	@Test
	public void should_not_accept_invalid_parameters() {
	
		assertNull(extractor.extract(null, LocalTime.now(), LocalTime.now(), "track.mp3"));	
		
		assertNull(extractor.extract(TestConstants.testAudioFile, null, null, "track.mp3"));
		
		assertNull(extractor.extract(new File("inexistent"), LocalTime.of(0, 0, 20), LocalTime.of(0, 0, 20), "track.mp3"));
	}
	
	@Test
	public void should_extract_track_if_everything_valid() {
	
		LocalTime start = LocalTime.of(0, 0, 20);
		LocalTime duration = LocalTime.of(0, 0, 10);
		
		File track = extractor.extract(TestConstants.testAudioFile, start, duration, "track.mp3");		
		
		assertEquals(10, this.audioUtils.getMp3DurationInSeconds(track), 1);
	}
} 
