package cz.senslog.connector.fetch.fieldclimate;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.time.OffsetDateTime;

import static java.time.OffsetDateTime.MIN;
import static org.junit.jupiter.api.Assertions.*;

class FieldClimateSessionTest {

    @BeforeEach
    void setUp() {
    }

    @Test
    void isActive_emptySession_false() {
        FieldClimateSession session = FieldClimateSession.emptySession();
        assertFalse(session.isActive());
    }

    @Test
    void isActive_newSession_true() {
        FieldClimateSession session = new FieldClimateSession(true);
        assertTrue(session.isActive());
    }

    @Test
    void lastUnitTime_addNewUnit() {
        FieldClimateSession session = new FieldClimateSession(true);

        OffsetDateTime actualTime = OffsetDateTime.now();

        session.addLastUnitTime("#1", actualTime);
        session.addLastUnitTime("#1", actualTime.plusMinutes(1));
        session.addLastUnitTime("#1", actualTime.minusMinutes(1));

        session.addLastUnitTime("#2", actualTime);
        session.addLastUnitTime("#2", actualTime.plusMinutes(2));
        session.addLastUnitTime("#2", actualTime.minusMinutes(2));

        assertEquals(actualTime.plusMinutes(1), session.getLastUnitTime("#1", MIN));
        assertEquals(actualTime.plusMinutes(2), session.getLastUnitTime("#2", MIN));
    }

    @Test
    void reduceLastUnits_true() {

        FieldClimateSession session = new FieldClimateSession(true);

        OffsetDateTime actualTime = OffsetDateTime.now();

        session.addLastUnitTime("#1", actualTime);
        session.addLastUnitTime("#2", actualTime);
        session.addLastUnitTime("#3", actualTime);
        session.addLastUnitTime("#4", actualTime);

        String[] removedUnits = session.reduceLastUnits("#1", "#2");
        assertEquals(2, removedUnits.length);

        assertEquals(actualTime, session.getLastUnitTime("#3", MIN));
        assertEquals(actualTime, session.getLastUnitTime("#4", MIN));
        assertEquals(MIN, session.getLastUnitTime("#1", MIN));
        assertEquals(MIN, session.getLastUnitTime("#2", MIN));
    }
}