package fr.bglands.client.protocol;

import org.junit.jupiter.api.Test;

import java.util.List;

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

final class UiLayoutTest {
    @Test
    void ordersAuthoredWidgetsByZAndPreservesDocumentOrderForTies() {
        UiScene scene = new UiScene(1, new UiSceneCanvas(400, 300, "/assets/frame-full.png"), List.of(
                widget("button", 5), widget("logo", 2), widget("panel-a", 3), widget("panel-b", 3)
        ));
        assertEquals(
                List.of("logo", "panel-a", "panel-b", "button"),
                UiLayout.orderedWidgets(scene).stream().map(UiSceneWidget::id).toList()
        );
    }

    @Test
    void paginatesLongRulesWithoutDroppingTheEleventhLine() {
        assertEquals(2, UiLayout.pageCount(11, 10));
        assertEquals(0, UiLayout.pageStart(0, 11, 10));
        assertEquals(10, UiLayout.pageEnd(0, 11, 10));
        assertEquals(10, UiLayout.pageStart(1, 11, 10));
        assertEquals(11, UiLayout.pageEnd(1, 11, 10));
        assertEquals(1, UiLayout.clampPage(99, 11, 10));
        assertThrows(IllegalArgumentException.class, () -> UiLayout.pageCount(1, 0));
    }

    private static UiSceneWidget widget(String id, int z) {
        return new UiSceneWidget(id, "text", 0, 0, 10, 10, z, "", id, "", "#ffffff", 6, "left", "");
    }
}
