package com.knecon.fforesight.service.layoutparser.server.graph;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.Collections;
import java.util.List;

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

import com.knecon.fforesight.service.layoutparser.processor.model.graph.TextRange;

class TextRangeTest {

    TextRange startTextRange;


    @BeforeEach
    void setUp() {

        startTextRange = new TextRange(10, 100);
    }


    @Test
    void testContains() {

        assertTrue(startTextRange.contains(11));
        assertTrue(startTextRange.contains(50));
        assertFalse(startTextRange.contains(9));
        assertFalse(startTextRange.contains(100));
        assertFalse(startTextRange.contains(150));
        assertFalse(startTextRange.contains(-123));
        assertTrue(startTextRange.contains(new TextRange(11, 99)));
        assertTrue(startTextRange.contains(new TextRange(10, 100)));
        assertTrue(startTextRange.contains(new TextRange(11, 11)));
        assertFalse(startTextRange.contains(9, 100));
        assertTrue(startTextRange.contains(100, 100));
        assertFalse(startTextRange.contains(100, 101));
        assertFalse(startTextRange.contains(150, 151));
    }


    @Test
    void testIntersects() {

        assertTrue(startTextRange.intersects(new TextRange(1, 11)));
        assertTrue(startTextRange.intersects(new TextRange(11, 12)));
        assertTrue(startTextRange.intersects(new TextRange(11, 100)));
        assertFalse(startTextRange.intersects(new TextRange(100, 101)));
        assertTrue(startTextRange.intersects(new TextRange(99, 101)));
    }


    @Test
    void testSplit() {

        assertEquals(4, startTextRange.split(List.of(12, 40, 90)).size());
        assertEquals(List.of(new TextRange(10, 12), new TextRange(12, 40), new TextRange(40, 90), new TextRange(90, 100)), startTextRange.split(List.of(12, 40, 90)));
        assertEquals(List.of(new TextRange(10, 40), new TextRange(40, 100)), startTextRange.split(List.of(40)));
        assertEquals(1, startTextRange.split(Collections.emptyList()).size());
        assertEquals(1, startTextRange.split(List.of(startTextRange.start())).size());
        assertThrows(IndexOutOfBoundsException.class, () -> startTextRange.split(Collections.singletonList(0)));
        assertThrows(IndexOutOfBoundsException.class, () -> startTextRange.split(Collections.singletonList(100)));
        assertThrows(IndexOutOfBoundsException.class, () -> startTextRange.split(List.of(12, 40, 100)));
    }

}