package com.uwetrottmann.trakt.v2.services;

import com.uwetrottmann.trakt.v2.BaseTestCase;
import com.uwetrottmann.trakt.v2.TestData;
import com.uwetrottmann.trakt.v2.entities.MovieIds;
import com.uwetrottmann.trakt.v2.entities.ShowIds;
import com.uwetrottmann.trakt.v2.entities.SyncEntity;
import com.uwetrottmann.trakt.v2.entities.SyncEpisode;
import com.uwetrottmann.trakt.v2.entities.SyncMovie;
import com.uwetrottmann.trakt.v2.entities.SyncResponse;
import com.uwetrottmann.trakt.v2.entities.SyncSeason;
import com.uwetrottmann.trakt.v2.entities.SyncShow;
import org.junit.Test;

import java.util.LinkedList;

import static org.fest.assertions.api.Assertions.assertThat;

public class SyncTest extends BaseTestCase {

    @Test
    public void test_getCollectionMovies() {
        getTrakt().sync().getCollectionMovies();
    }

    @Test
    public void test_getCollectionShows() {
        getTrakt().sync().getCollectionShows();
    }

    @Test
    public void test_addItemsToCollection() {
        SyncEntity items = new SyncEntity();

        SyncMovie movie = new SyncMovie();
        movie.ids = new MovieIds();
        movie.ids.tmdb = TestData.MOVIE_TMDB_ID;
        items.movies = new LinkedList<>();
        items.movies.add(movie);


        // episode
        SyncEpisode episode = new SyncEpisode();
        episode.number = TestData.EPISODE_NUMBER;
        SyncEpisode episode2 = new SyncEpisode();
        episode2.number = 2;
        // season
        SyncSeason season = new SyncSeason();
        season.number = TestData.EPISODE_SEASON;
        season.episodes = new LinkedList<>();
        season.episodes.add(episode);
        season.episodes.add(episode2);
        // show
        SyncShow show = new SyncShow();
        show.ids = new ShowIds();
        show.ids.tvdb = TestData.SHOW_TVDB_ID;
        show.seasons = new LinkedList<>();
        show.seasons.add(season);
        items.shows = new LinkedList<>();
        items.shows.add(show);

        SyncResponse response = getTrakt().sync().addItemsToCollection(items);
        assertThat(response.added.movies).isNotNull();
        assertThat(response.added.episodes).isNotNull();
        assertThat(response.existing.movies).isNotNull();
        assertThat(response.existing.episodes).isNotNull();
        assertThat(response.not_found).isNotNull();
        assertThat(response.deleted).isNull();
    }

    @Test
    public void test_deleteItemsFromCollection() {
        SyncEntity items = new SyncEntity();

        SyncMovie movie = new SyncMovie();
        movie.ids = new MovieIds();
        movie.ids.tmdb = 118340;
        items.movies = new LinkedList<>();
        items.movies.add(movie);

        // episode
        SyncEpisode episode2 = new SyncEpisode();
        episode2.number = 2;
        // season
        SyncSeason season = new SyncSeason();
        season.number = TestData.EPISODE_SEASON;
        season.episodes = new LinkedList<>();
        season.episodes.add(episode2);
        // show
        SyncShow show = new SyncShow();
        show.ids = new ShowIds();
        show.ids.tvdb = TestData.SHOW_TVDB_ID;
        show.seasons = new LinkedList<>();
        show.seasons.add(season);
        items.shows = new LinkedList<>();
        items.shows.add(show);

        SyncResponse response = getTrakt().sync().deleteItemsFromCollection(items);
        assertThat(response.deleted.movies).isNotNull();
        assertThat(response.deleted.episodes).isNotNull();
        assertThat(response.existing).isNull();
        assertThat(response.not_found).isNotNull();
        assertThat(response.added).isNull();
    }

}

@@ -128,16 +128,250 @@
 stData;%0A
+import com.uwetrottmann.trakt.v2.entities.CollectedEpisode;%0Aimport com.uwetrottmann.trakt.v2.entities.CollectedMovie;%0Aimport com.uwetrottmann.trakt.v2.entities.CollectedSeason;%0Aimport com.uwetrottmann.trakt.v2.entities.CollectedShow;%0A
 import c
@@ -841,16 +841,39 @@
 kedList;
+%0Aimport java.util.List;
 %0A%0Aimport
@@ -1028,32 +1028,62 @@
 vies() %7B%0A       
+ List%3CCollectedMovie%3E movies =
  getTrakt().sync
@@ -1104,24 +1104,136 @@
 onMovies();%0A
+        for (CollectedMovie movie : movies) %7B%0A            assertThat(movie.collected_at).isNotNull();%0A        %7D%0A
     %7D%0A%0A    @
@@ -1288,16 +1288,44 @@
 %0A       
+ List%3CCollectedShow%3E shows =
  getTrak
@@ -1353,24 +1353,355 @@
 ionShows();%0A
+        for (CollectedShow show : shows) %7B%0A            assertThat(show.collected_at).isNotNull();%0A            for (CollectedSeason season : show.seasons) %7B%0A                for (CollectedEpisode episode : season.episodes) %7B%0A                    assertThat(episode.collected_at).isNotNull();%0A                %7D%0A            %7D%0A        %7D%0A
     %7D%0A%0A    @

Add missing collection tests (check for collected_at value).