package au.gov.amsa.detection.behaviour;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.util.Date;

import org.junit.Test;

import au.gov.amsa.detection.model.DetectionRule;
import au.gov.amsa.detection.model.DetectionRule.Events.PositionInRegion;

public class DetectionRuleBehaviourTest {

    @Test
    public void testDoesNotCreateDetectionIfTimeOutsideTimeRangeAndIsExclusiveOnUpperLimitOfTimeRange() {
        DetectionRule dr = mock(DetectionRule.class);
        PositionInRegion p = createPositionInRegionEvent();
        when(dr.getMustCross()).thenReturn(Boolean.FALSE);
        when(dr.getStartTime()).thenReturn(new Date(0));
        when(dr.getEndTime()).thenReturn(new Date(10));
        assertFalse(DetectionRuleBehaviour.shouldCreateDetection(dr, p, x -> true, 3));
    }

    @Test
    public void testDoesNotCreateDetectionIfTimeOutsideTimeRange() {
        DetectionRule dr = mock(DetectionRule.class);
        PositionInRegion p = createPositionInRegionEvent();
        when(dr.getMustCross()).thenReturn(Boolean.FALSE);
        when(dr.getStartTime()).thenReturn(new Date(0));
        when(dr.getEndTime()).thenReturn(new Date(9));
        assertFalse(DetectionRuleBehaviour.shouldCreateDetection(dr, p, x -> true, 3));
    }

    @Test
    public void testDoesNotCreateDetectionIfMustCrossAndHasNotBeenOutside() {
        DetectionRule dr = mock(DetectionRule.class);
        PositionInRegion p = createPositionInRegionEvent();
        when(dr.getMustCross()).thenReturn(Boolean.TRUE);
        when(dr.getStartTime()).thenReturn(new Date(0));
        when(dr.getEndTime()).thenReturn(new Date(20));
        assertFalse(DetectionRuleBehaviour.shouldCreateDetection(dr, p, x -> true, 3));
    }

    @Test
    public void testDoesNotCreateDetectionIfDoesNotMatchPattern() {
        DetectionRule dr = mock(DetectionRule.class);
        PositionInRegion p = createPositionInRegionEvent();
        when(dr.getMustCross()).thenReturn(Boolean.FALSE);
        when(dr.getStartTime()).thenReturn(new Date(0));
        when(dr.getEndTime()).thenReturn(new Date(20));
        assertFalse(DetectionRuleBehaviour.shouldCreateDetection(dr, p, x -> false, 3));
    }

    @Test
    public void testCreateDetectionIfNoPreviousDetection() {
        DetectionRule dr = mock(DetectionRule.class);
        PositionInRegion p = createPositionInRegionEvent();
        when(dr.getMustCross()).thenReturn(Boolean.FALSE);
        when(dr.getStartTime()).thenReturn(new Date(0));
        when(dr.getEndTime()).thenReturn(new Date(20));
        assertTrue(DetectionRuleBehaviour.shouldCreateDetection(dr, p, x -> true, 3));
    }

    private PositionInRegion createPositionInRegionEvent() {
        return DetectionRule.Events.PositionInRegion.builder().hasBeenOutsideRegion(false)
                .latitude(-10.0).longitude(135.0).lastExitTimeFromRegion(new Date(0))
                .lastTimeEntered(new Date(0)).time(new Date(10)).craftID("abc").altitudeMetres(0.0)
                .build();
    }

}

@@ -251,16 +251,62 @@
 .Test;%0A%0A
+import au.gov.amsa.detection.model.Detection;%0A
 import a
@@ -347,16 +347,16 @@
 onRule;%0A
-
 import a
@@ -942,33 +942,34 @@
 , p, x -%3E true, 
-3
+15
 ));%0A    %7D%0A%0A    @
@@ -1402,33 +1402,34 @@
 , p, x -%3E true, 
-3
+15
 ));%0A    %7D%0A%0A    @
@@ -1871,33 +1871,34 @@
 , p, x -%3E true, 
-3
+15
 ));%0A    %7D%0A%0A    @
@@ -2340,17 +2340,18 @@
  false, 
-3
+15
 ));%0A    
@@ -2792,17 +2792,664 @@
 %3E true, 
-3
+15));%0A    %7D%0A%0A    @Test%0A    public void testCreateDetectionIfLastDetectionPresent() %7B%0A        DetectionRule dr = mock(DetectionRule.class);%0A%0A        PositionInRegion p = createPositionInRegionEvent();%0A        when(dr.getMustCross()).thenReturn(Boolean.FALSE);%0A        when(dr.getStartTime()).thenReturn(new Date(0));%0A        when(dr.getEndTime()).thenReturn(new Date(20));%0A        Detection d = mock(Detection.class);%0A        when(dr.getDetection_R18()).thenReturn(d);%0A        // after position time for p%0A        when(d.getReportTime()).thenReturn(new Date(16));%0A        assertFalse(DetectionRuleBehaviour.shouldCreateDetection(dr, p, x -%3E true, 15
 ));%0A    

add tests