package leigh.conflux;

import leigh.eo.EO;
import leigh.eo.EOLoop;
import leigh.eo.sort.EOSortPolicy;
import leigh.mecha.log.MechaLogger;
import leigh.mecha.log.MechaLoggerFactory;
import leigh.mecha.sort.FieldSort;
import leigh.mecha.sort.SortCardinality;
import org.junit.Test;

import java.util.List;
import java.util.Locale;

import static org.junit.Assert.assertEquals;

/**
 * JUnit test for the {@link DataFrame} class and related functionality.
 *
 * @author Alex Leigh
 */
public class DataFrameTest {
    private static final MechaLogger logger = MechaLoggerFactory.getLogger(DataFrameTest.class);

    @Test
    public void testFilter() throws Exception {
        EOLoop data = new EOLoop();
        data.add(new EO("person").setValue("firstName", "C. Alexander")
                .setValue("lastName", "Leigh"));
        data.add(new EO("person").setValue("firstName", "Nikki")
                .setValue("lastName", "Windon"));

        DataFrame df = new DataFrameImpl(data);
        assertEquals(2, df.size());

        df.setQuickFilter("firstName", "Alex");
        assertEquals(2, df.size());

        df.process();
        assertEquals(1, df.size());
        EO first = df.getView().get(0);
        assertEquals("Leigh", first.getValue("lastName"));
    }

    @Test
    public void testAscendingSort() throws Exception {
        EOLoop data = new EOLoop();
        data.add(new EO("person").setValue("firstName", "Nikki")
                .setValue("lastName", "Windon"));
        data.add(new EO("person").setValue("firstName", "C. Alexander")
                .setValue("lastName", "Leigh"));
        DataFrame df = new DataFrameImpl(data);

        // Select Nikki
        df.getSelection().add(0, 0);

        EOSortPolicy sort = new EOSortPolicy(Locale.US);
        sort.addField(new FieldSort("lastName", SortCardinality.ASCENDING));
        df.setSortPolicy(sort);
        df.process();

        // Check that Leigh is now first
        EO first = df.getView().get(0);
        assertEquals("Leigh", first.get("lastName"));

        // Verify that Nikki is still selected
        List<EO> selected = df.getSelected();
        assertEquals(1, selected.size());
        EO firstSelected = selected.get(0);

        assertEquals("Windon", firstSelected.getValue("lastName"));
    }

    @Test
    public void testDescendingSort() throws Exception {
        EOLoop data = new EOLoop();
        data.add(new EO("person").setValue("firstName", "C. Alexander")
                .setValue("lastName", "Leigh"));
        data.add(new EO("person").setValue("firstName", "Nikki")
                .setValue("lastName", "Windon"));
        DataFrame df = new DataFrameImpl(data);

        EOSortPolicy sort = new EOSortPolicy(Locale.US);
        sort.addField(new FieldSort("lastName", SortCardinality.DESCENDING));
        df.setSortPolicy(sort);
        df.process();

        EO first = df.getView().get(0);
        df.process();
        assertEquals("Windon", first.get("lastName"));
    }
}
