package dst.ass1.jpa.tests;

import static org.hamcrest.core.IsCollectionContaining.hasItems;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

import java.util.Collection;
import java.util.Date;
import java.util.GregorianCalendar;

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

import dst.ass1.jpa.dao.ICourseDAO;
import dst.ass1.jpa.model.ICourse;

public class Ass1_2_3b_01Test extends Ass1_TestBase {

    private ICourseDAO dao;

    @Before
    public void setUp() throws Exception {
        dao = daoFactory.createCourseDAO();
    }

    @Test
    public void testFindNonCancelledCoursesBetweenStartAndFinish() {
        Collection<ICourse> courses = dao.findNonCancelledCoursesBetweenStartAndEnd(null, null);

        assertThat(map(courses, e -> e.getId()), hasItems(
                testData.course1Id,
                testData.course2Id
        ));
    }

    @Test
    public void testFindEventsForStatusFinishedBetweenStartAndFinish2() {
        Date start = createDate(2017, 1, 1, 1, 1);
        Collection<ICourse> courses = dao.findNonCancelledCoursesBetweenStartAndEnd(start, null);
        Collection<Long> courseIds = map(courses, ICourse::getId);
        assertThat(courseIds, hasItems(testData.course1Id, testData.course2Id));

        assertThat(map(courses, e -> e.getId()), hasItems(
                testData.course1Id,
                testData.course2Id
        ));
    }

    @Test
    public void testFindEventsForStatusFinishedBetweenStartAndFinish3() {
        Date start = dao.findById(testData.course1Id).getStart();
        Collection<ICourse> courses = dao.findNonCancelledCoursesBetweenStartAndEnd(start, null);

        assertThat(map(courses, e -> e.getId()), hasItems(
                testData.course2Id
        ));
    }

    @Test
    public void testFindEventsForStatusFinishedBetweenStartAndFinish4() {
        Date end = dao.findById(testData.course2Id).getEnd();
        Collection<ICourse> courses = dao.findNonCancelledCoursesBetweenStartAndEnd(null, end);

        assertThat(map(courses, e -> e.getId()), hasItems(
                testData.course1Id
        ));
    }

    @Test
    public void testFindEventsForStatusFinishedBetweenStartAndFinish5() {
        Date start = dao.findById(testData.course1Id).getEnd();
        Date end = dao.findById(testData.course2Id).getEnd();
        Collection<ICourse> courses = dao.findNonCancelledCoursesBetweenStartAndEnd(start, end);

        assertNotNull(courses);
        assertTrue(courses.isEmpty());
    }

    private Date createDate(int year, int month, int day, int hours, int minutes) {
        return new GregorianCalendar(year, month, day, hours, minutes).getTime();
    }

}
