package p.studio.lsp.events;

import org.junit.jupiter.api.Test;
import p.packer.events.PackerEvent;
import p.packer.events.PackerEventKind;
import p.packer.events.PackerProgress;

import java.time.Instant;
import java.util.ArrayList;
import java.util.List;

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

final class StudioEmbeddedPackerEventAdapterTest {
    @Test
    void forwardsScanEventsIntoStudioOperationEvents() {
        final StudioEventBus globalBus = new StudioEventBus();
        final List<StudioPackerOperationEvent> events = new ArrayList<>();
        globalBus.subscribe(StudioPackerOperationEvent.class, events::add);
        final StudioPackerEventAdapter adapter = new StudioPackerEventAdapter(globalBus);

        adapter.publish(new PackerEvent(
                "main",
                "op-1",
                0L,
                PackerEventKind.ASSET_DISCOVERED,
                Instant.now(),
                "Discovered asset.",
                new PackerProgress(0.25d, false),
                List.of("ui_atlas")));

        assertEquals(1, events.size());
        assertEquals(PackerEventKind.ASSET_DISCOVERED, events.getFirst().kind());
        assertEquals(0.25d, events.getFirst().progress());
    }

    @Test
    void forwardsWriteLifecycleEventsIntoStudioOperationEvents() {
        final StudioEventBus globalBus = new StudioEventBus();
        final List<StudioPackerOperationEvent> events = new ArrayList<>();
        globalBus.subscribe(StudioPackerOperationEvent.class, events::add);
        final StudioPackerEventAdapter adapter = new StudioPackerEventAdapter(globalBus);

        adapter.publish(new PackerEvent(
                "main",
                "op-1",
                0L,
                PackerEventKind.ACTION_APPLIED,
                Instant.now(),
                "Asset created.",
                null,
                List.of("ui_atlas")));

        assertEquals(1, events.size());
        assertEquals(PackerEventKind.ACTION_APPLIED, events.getFirst().kind());
        assertEquals("Asset created.", events.getFirst().summary());
    }
}
