package com.thoughtworks.biblioteca;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.ArrayList;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;

public class MovieLibraryTest {

    @Test
    public void shouldHaveAListOfAvailableMovies() {

        ArrayList<Movies> movies1 = new ArrayList<Movies>();
        movies1.add(new Movies("Titanic", 1990, "James Cameron", 5));
        MovieLibrary movieLibrary = new MovieLibrary(movies1);
        assertEquals(movies1,movieLibrary.getMovieLibrary());
    }

    @Test
    public void shouldReturnEqualIfTheMovieListsAreEqual() {
        ArrayList<Movies> movies = new ArrayList<Movies>();
        movies.add(new Movies("Titanic", 1990, "James Cameron", 5));
        movies.add(new Movies("Inception", 2010, "Christopher Nolan", 9));
        MovieLibrary movieLibrary1 = new MovieLibrary(movies);
        MovieLibrary movieLibrary2 = new MovieLibrary(movies);
        assertEquals(movieLibrary1,movieLibrary2);
    }

    @Test
    public void shouldReturnNotEqualIfTheMovieListsAreNotEqual() {
        ArrayList<Movies> movies = new ArrayList<Movies>();
        movies.add(new Movies("Titanic", 1990, "James Cameron", 5));
        movies.add(new Movies("Inception", 2010, "Christopher Nolan", 9));
        MovieLibrary movieLibrary1 = new MovieLibrary(movies);
        ArrayList<Movies> movies1 = new ArrayList<Movies>();
        movies1.add(new Movies("Titanic2", 1990, "James Cameron", 5));
        movies1.add(new Movies("Inception", 2010, "Christopher Nolan", 9));
        MovieLibrary movieLibrary2 = new MovieLibrary(movies1);
        assertNotEquals(movieLibrary1, movieLibrary2);
    }

    @Test
    public void shouldReturnEqualHashCodeIfTwoMoviesListsAreSame() {
        ArrayList<Movies> movies = new ArrayList<Movies>();
        movies.add(new Movies("Titanic", 1990, "James Cameron", 5));
        movies.add(new Movies("Inception", 2010, "Christopher Nolan", 9));
        MovieLibrary movieLibrary1 = new MovieLibrary(movies);
        ArrayList<Movies> movies1 = new ArrayList<Movies>();
        movies1.add(new Movies("Titanic", 1990, "James Cameron", 5));
        movies1.add(new Movies("Inception", 2010, "Christopher Nolan", 9));
        MovieLibrary movieLibrary2 = new MovieLibrary(movies1);
        assertEquals(movieLibrary1.hashCode(), movieLibrary2.hashCode());
    }

    @Test
    public void shouldReturnEqualHashCodeIfTwoMoviesListsAreDifferent() {
        ArrayList<Movies> movies = new ArrayList<Movies>();
        movies.add(new Movies("Titanic", 1990, "James Cameron", 5));
        movies.add(new Movies("Inception", 2010, "Christopher Nolan", 9));
        MovieLibrary movieLibrary1 = new MovieLibrary(movies);
        ArrayList<Movies> movies1 = new ArrayList<Movies>();
        movies1.add(new Movies("Titanicjk", 1990, "James Cameron", 5));
        movies1.add(new Movies("Inceptioon1", 2010, "Christopher Nolan", 9));
        MovieLibrary movieLibrary2 = new MovieLibrary(movies1);
        assertNotEquals(movieLibrary1.hashCode(), movieLibrary2.hashCode());
    }

    @Test
    public void shouldReturnAValidMovieListAfterCheckout() {
        ArrayList<Movies> movies = new ArrayList<Movies>();
        movies.add(new Movies("Titanic", 1990, "James Cameron", 5));
        MovieLibrary movieLibrary1 = new MovieLibrary(movies);
        movies.add(new Movies("Inception", 2010, "Christopher Nolan", 9));
        MovieLibrary movieLibrary2 = new MovieLibrary(movies);
        movieLibrary1.checkout("Inception");
        assertEquals(movieLibrary1, movieLibrary2);
    }

    @Test
    public void shouldValidateASuccessfulReturn()
    {
        ArrayList<Movies> movies = new ArrayList<Movies>();
        movies.add(new Movies("Titanic", 1990, "James Cameron", 5));
        movies.add(new Movies("Inception", 2010, "Christopher Nolan", 9));
        MovieLibrary movieLibrary1 = new MovieLibrary(movies);
        MovieLibrary movieLibrary2 = new MovieLibrary(movies);
        movieLibrary1.checkout("Inception");
        movieLibrary1.returnMovie("Inception");
        assertEquals(movieLibrary1, movieLibrary2);

    }

    private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();

    @Before
    public void setOutputStream() {
        System.setOut(new PrintStream(outContent));
    }

    @Test
    public void shouldReturnValidMovie() {
        ArrayList<Movies> movies = new ArrayList<Movies>();
        movies.add(new Movies("Titanic", 1990, "James Cameron", 5));
        MovieLibrary movieLibrary = new MovieLibrary(movies);
        movieLibrary.displayContents();
        assertEquals(String.format("%-34s %-34d %-34s %-34d\n", "Titanic", 1990, "James Cameron", 5), outContent.toString());
    }
    @After
    public void cleanUpStreams() {
        System.setOut(System.out);
    }

}

@@ -613,16 +613,17 @@
 movies1,
+ 
 movieLib
@@ -1090,16 +1090,17 @@
 ibrary1,
+ 
 movieLib
@@ -3764,20 +3764,16 @@
 Return()
-%0A   
  %7B%0A     
@@ -4856,16 +4856,578 @@
 ;%0A    %7D%0A
+%0A    @Test%0A    public void shouldReturnValidMovies() %7B%0A        ArrayList%3CMovies%3E movies = new ArrayList%3CMovies%3E();%0A        movies.add(new Movies(%22Titanic%22, 1990, %22James Cameron%22, 5));%0A        movies.add(new Movies(%22Inception%22, 2010, %22Christopher Nolan%22, 9));%0A        MovieLibrary movieLibrary = new MovieLibrary(movies);%0A        movieLibrary.displayContents();%0A        assertEquals(String.format(%22%25-34s %25-34d %25-34s %25-34d%5Cn%25-34s %25-34d %25-34s %25-34d%5Cn%22, %22Titanic%22, 1990, %22James Cameron%22, 5, %22Inception%22, 2010, %22Christopher Nolan%22, 9), outContent.toString());%0A    %7D%0A%0A
     @Aft

the movielibrary class should return multiple movie in proper format