package com.greef.ui.calendar;

import junit.framework.TestCase;

import java.util.GregorianCalendar;

/**
 * Tests <code>{@link CalendarSelectionModel}</code>.
 * @author Adrian BER
 */
public class CalendarSelectionModelTestCase extends TestCase {

    private CalendarSelectionModel x;

    protected void setUp() throws Exception {
        super.setUp();
        x = new CalendarSelectionModel();
    }

    public void testIsSelectionEmpty() {
        assertTrue("New selection model isn't empty", x.isSelectionEmpty());
        x.getDateSelectionModel().addSelectedInterval(
            new GregorianCalendar(2004, 5, 1).getTime(),
            new GregorianCalendar(2004, 5, 10).getTime());
        x.getCalendarModel().setDate(5, 2004);
        assertFalse("After add selection is still empty", x.isSelectionEmpty());
        x.getCalendarModel().setDate(6, 2004);
        assertTrue("Selection in different month isn't considered empty",
            x.isSelectionEmpty());
        x.getCalendarModel().setDate(5, 2004);
        assertFalse(x.isSelectionEmpty());
    }

    public void testClearSelection() {
        assertTrue("New selection model isn't empty", x.isSelectionEmpty());
        x.getDateSelectionModel().addSelectedInterval(
            new GregorianCalendar(2004, 5, 1).getTime(),
            new GregorianCalendar(2004, 5, 10).getTime());
        x.getCalendarModel().setDate(5, 2004);
        assertFalse("After add selection is still empty", x.isSelectionEmpty());
        x.clearSelection();
        assertTrue("clearSelection failed", x.isSelectionEmpty());
    }

    public void testGetMinMaxSelectionIndex() {
        x.getCalendarModel().setDate(6, 2004);
        x.getDateSelectionModel().addSelectedInterval(
            new GregorianCalendar(2004, 6, 8).getTime(),
            new GregorianCalendar(2004, 6, 21).getTime());
        assertEquals(11, x.getMinSelectionIndex());
        assertEquals(24, x.getMaxSelectionIndex());
        x.getCalendarModel().setDate(7, 2004);
        assertEquals(-1, x.getMinSelectionIndex());
        assertEquals(-1, x.getMaxSelectionIndex());
    }

    public void testIsSelectedIndex() {
        assertTrue("New selection model isn't empty", x.isSelectionEmpty());
        x.getDateSelectionModel().addSelectedInterval(
            new GregorianCalendar(2004, 6, 8).getTime(),
            new GregorianCalendar(2004, 6, 21).getTime());
        x.getCalendarModel().setDate(6, 2004);
        assertFalse("After add selection is still empty", x.isSelectionEmpty());
        assertTrue(x.isSelectedIndex(11));
        assertTrue(x.isSelectedIndex(17));
        assertTrue(x.isSelectedIndex(24));
        assertFalse(x.isSelectedIndex(7));
        assertFalse(x.isSelectedIndex(10));
        assertFalse(x.isSelectedIndex(25));
        assertFalse(x.isSelectedIndex(29));

        x.getCalendarModel().setDate(7, 2004);
        assertFalse(x.isSelectedIndex(11));
        assertFalse(x.isSelectedIndex(17));
        assertFalse(x.isSelectedIndex(24));

        x.getCalendarModel().setDate(6, 2004);
        assertTrue(x.isSelectedIndex(11));
        assertTrue(x.isSelectedIndex(17));
        assertTrue(x.isSelectedIndex(24));
        assertFalse(x.isSelectedIndex(7));
        assertFalse(x.isSelectedIndex(10));
        assertFalse(x.isSelectedIndex(25));
        assertFalse(x.isSelectedIndex(29));
    }

    public void testAddSelectionInterval() {
        assertTrue("New selection model isn't empty", x.isSelectionEmpty());
        x.getCalendarModel().setDate(6, 2004);
        x.addSelectionInterval(11, 24);
        assertFalse("After add selection is still empty", x.isSelectionEmpty());
        assertTrue(x.getDateSelectionModel().isDateSelected(
            new GregorianCalendar(2004, 6, 8).getTime()));
        assertTrue(x.getDateSelectionModel().isDateSelected(
            new GregorianCalendar(2004, 6, 17).getTime()));
        assertTrue(x.getDateSelectionModel().isDateSelected(
            new GregorianCalendar(2004, 6, 21).getTime()));
        assertFalse(x.getDateSelectionModel().isDateSelected(
            new GregorianCalendar(2004, 6, 5).getTime()));
        assertFalse(x.getDateSelectionModel().isDateSelected(
            new GregorianCalendar(2004, 6, 7).getTime()));
        assertFalse(x.getDateSelectionModel().isDateSelected(
            new GregorianCalendar(2004, 6, 22).getTime()));
        assertFalse(x.getDateSelectionModel().isDateSelected(
            new GregorianCalendar(2004, 6, 27).getTime()));
        assertTrue(x.isSelectedIndex(11));
        assertTrue(x.isSelectedIndex(15));
        assertTrue(x.isSelectedIndex(24));

        x.getCalendarModel().setDate(7, 2004);
        assertFalse(x.isSelectedIndex(11));
        assertFalse(x.isSelectedIndex(15));
        assertFalse(x.isSelectedIndex(24));
    }

    public void testRemoveSelectionInterval() {
        assertTrue("New selection model isn't empty", x.isSelectionEmpty());
        x.getCalendarModel().setDate(6, 2004);
        x.addSelectionInterval(11, 24);
        x.removeSelectionInterval(17, 20);
        assertFalse("After add selection is still empty", x.isSelectionEmpty());
        assertTrue(x.getDateSelectionModel().isDateSelected(
            new GregorianCalendar(2004, 6, 8).getTime()));
        assertTrue(x.getDateSelectionModel().isDateSelected(
            new GregorianCalendar(2004, 6, 20).getTime()));
        assertTrue(x.getDateSelectionModel().isDateSelected(
            new GregorianCalendar(2004, 6, 21).getTime()));

//        assertFalse(x.getDateSelectionModel().isDateSelected(
//            new GregorianCalendar(2004, 6, 14).getTime()));
        assertFalse(x.getDateSelectionModel().isDateSelected(
            new GregorianCalendar(2004, 6, 15).getTime()));
        assertFalse(x.getDateSelectionModel().isDateSelected(
            new GregorianCalendar(2004, 6, 16).getTime()));
//        assertFalse(x.getDateSelectionModel().isDateSelected(
//            new GregorianCalendar(2004, 6, 17).getTime()));

        assertTrue(x.isSelectedIndex(15));
        assertTrue(x.isSelectedIndex(24));

//        assertFalse(x.isSelectedIndex(17));
        assertFalse(x.isSelectedIndex(18));
        assertFalse(x.isSelectedIndex(19));
//        assertFalse(x.isSelectedIndex(20));

        x.getCalendarModel().setDate(7, 2004);
        assertFalse(x.isSelectedIndex(11));
        assertFalse(x.isSelectedIndex(15));
        assertFalse(x.isSelectedIndex(24));
    }

    public void testSetSelectionInterval() {
        assertTrue("New selection model isn't empty", x.isSelectionEmpty());
        x.getCalendarModel().setDate(6, 2004);
        x.addSelectionInterval(11, 24);
        assertTrue(x.getDateSelectionModel().isDateSelected(
            new GregorianCalendar(2004, 6, 17).getTime()));
        assertTrue(x.isSelectedIndex(15));

        x.getCalendarModel().setDate(7, 2004);
        x.setSelectionInterval(5, 7);
        assertTrue(x.isSelectedIndex(6));
        assertFalse(x.isSelectedIndex(15));

        x.getCalendarModel().setDate(6, 2004);
        assertFalse(x.isSelectedIndex(6));
        assertTrue(x.isSelectedIndex(15));
    }

    public void testPrevNextMonths() {
        x.getCalendarModel().setDate(6, 2004);
        x.addSelectionInterval(1, 3);
        assertFalse("After add selection is still empty", x.isSelectionEmpty());
        assertTrue(x.getDateSelectionModel().isDateSelected(
            new GregorianCalendar(2004, 5, 28).getTime()));
        assertTrue(x.getDateSelectionModel().isDateSelected(
            new GregorianCalendar(2004, 5, 29).getTime()));
        assertTrue(x.getDateSelectionModel().isDateSelected(
            new GregorianCalendar(2004, 5, 30).getTime()));
        assertTrue(x.isSelectedIndex(1));
        assertTrue(x.isSelectedIndex(2));
        assertTrue(x.isSelectedIndex(3));

        x.getCalendarModel().setDate(5, 2004);
        assertTrue(x.isSelectedIndex(29));
        assertTrue(x.isSelectedIndex(30));
        assertTrue(x.isSelectedIndex(31));
    }

}
