package net.cserny.videosmover;

import net.cserny.videosmover.helper.InMemoryVideoFileSystemInitializer;
import net.cserny.videosmover.helper.TestHelperConfig;
import net.cserny.videosmover.helper.VideoCreationHelper;
import net.cserny.videosmover.model.Video;
import net.cserny.videosmover.service.PathsProvider;
import net.cserny.videosmover.service.VideoMover;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import static org.junit.Assert.assertTrue;

/**
 * Created by leonardo on 02.09.2017.
 */
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = {ApplicationConfig.class, TestHelperConfig.class})
public class TestVideoMoving extends InMemoryVideoFileSystemInitializer {
    @Autowired
    private VideoCreationHelper videoHelper;
    @Autowired
    private VideoMover videoMover;

    @Test
    public void givenVideoRowTvShowWhenMovingThenMoveToTvShowsOutput() throws Exception {
        Video video = videoHelper.createTvShow(DOWNLOADS_TVSHOW);
        assertTrue(videoMover.move(video));
    }

    @Test
    public void givenMultipleVideoRowsTvShowWhenMovingThenMoveAllToTvShowOutput() throws Exception {
        Video video1 = videoHelper.createTvShow(DOWNLOADS_TVSHOW);
        Video video2 = videoHelper.createTvShow(DOWNLOADS_EXISTING_TVSHOW);
        List<Video> videoList = Arrays.asList(video1, video2);

        assertTrue(videoMover.moveAll(videoList));
    }

    @Test
    public void givenVideoRowMovieWithSubtitlesWhenMovingThenMoveToMoviesOutputWithSubtitles() throws Exception {
        Video video = videoHelper.createMovie(DOWNLOADS_MOVIE_WITH_SUBTITLE);
        video.setSubtitles(Collections.singletonList(PathsProvider.getPath(DOWNLOADS_SUBTITLE)));

        assertTrue(videoMover.move(video));
    }

    @Test
    public void whenSubtitlesAreInSubsFolderMoveThemToSubsFolderInOutputAlso() throws Exception {
        Video video = videoHelper.createMovie(DOWNLOADS_MOVIE_WITH_SUBTITLE_IN_SUBS);
        Path subPath1 = PathsProvider.getPath(DOWNLOADS_SUBTITLE_IN_SUBS);
        Path subPath2 = PathsProvider.getPath(DOWNLOADS_SUBTITLE_IN_SUBS_IDX);
        video.setSubtitles(Arrays.asList(subPath1, subPath2));

        assertTrue(videoMover.move(video));
        assertTrue(Files.exists(video.getOutput().resolve("Subs").resolve(subPath1.getFileName())));
        assertTrue(Files.exists(video.getOutput().resolve("Subs").resolve(subPath2.getFileName())));
    }
}

@@ -252,116 +252,479 @@
 del.
-Video;%0Aimport net.cserny.videosmover.service.PathsProvider;%0Aimport net.cserny.videosmover.service.VideoMover
+SimpleVideoOutput;%0Aimport net.cserny.videosmover.model.Video;%0Aimport net.cserny.videosmover.model.VideoMetadata;%0Aimport net.cserny.videosmover.model.VideoQuery;%0Aimport net.cserny.videosmover.service.OutputResolver;%0Aimport net.cserny.videosmover.service.PathsProvider;%0Aimport net.cserny.videosmover.service.VideoMetadataService;%0Aimport net.cserny.videosmover.service.VideoMover;%0Aimport net.cserny.videosmover.service.helper.SimpleVideoOutputHelper;%0Aimport org.junit.Assert
 ;%0Aim
@@ -1483,16 +1483,139 @@
 eoMover;
+%0A    @Autowired%0A    private VideoMetadataService metadataService;%0A    @Autowired%0A    private OutputResolver outputResolver;
 %0A%0A    @T
@@ -3216,11 +3216,902 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void whenMovieIsChosenFromMetadataServiceOnMovingPlaceMovieInCorrectOutput() throws Exception %7B%0A        Video video = videoHelper.createMovie(DOWNLOADS_MOVIE_WITH_SUBTITLE_IN_SUBS);%0A        SimpleVideoOutput output = SimpleVideoOutputHelper.buildVideoOutput(outputResolver.resolve(video));%0A        List%3CVideoMetadata%3E metadataList = metadataService.searchMovieMetadata(VideoQuery.newInstance()%0A                .withName(output.getName()).withYear(output.getYear()).build());%0A        VideoMetadata videoMetadata = metadataList.get(0);%0A        Assert.assertNotNull(videoMetadata);%0A%0A        String formattedOutput = SimpleVideoOutputHelper.formatOutput(output, videoMetadata);%0A        Path outputPath = PathsProvider.getPath(formattedOutput);%0A        video.setOutput(outputPath);%0A%0A        videoMover.move(video);%0A        Assert.assertTrue(Files.exists(outputPath));%0A    %7D
 %0A%7D%0A

add confirmation test