package p.studio.workspaces.assets;

import p.studio.Container;
import p.studio.lsp.events.StudioPackerOperationEvent;
import p.studio.lsp.events.StudioWorkspaceEventBus;
import p.studio.utilities.i18n.I18n;
import p.studio.utilities.logspane.LogsPane;
import p.studio.workspaces.assets.messages.events.StudioAssetLogEvent;
import p.studio.workspaces.assets.messages.events.StudioAssetsWorkspaceRefreshFailedEvent;
import p.studio.workspaces.assets.messages.events.StudioAssetsWorkspaceRefreshStartedEvent;
import p.studio.workspaces.assets.messages.events.StudioAssetsWorkspaceRefreshedEvent;
import p.studio.workspaces.framework.StudioEventAware;
import p.studio.workspaces.framework.StudioEventBindings;

import java.util.Objects;

public final class AssetLogsPane extends LogsPane implements StudioEventAware {
    private final StudioWorkspaceEventBus workspaceBus;
    private final StudioEventBindings eventBindings = new StudioEventBindings();

    public AssetLogsPane(StudioWorkspaceEventBus workspaceBus) {
        super(Container.i18n().bind(I18n.ASSETS_LOGS_TITLE), "assets", false);
        this.workspaceBus = Objects.requireNonNull(workspaceBus, "workspaceBus");
    }

    @Override
    public StudioEventBindings eventBindings() {
        return eventBindings;
    }

    @Override
    public void registerEventSubscriptions() {
        eventBindings.listen(workspaceBus, StudioAssetLogEvent.class).handle(event -> {
            appendLine("[" + event.source() + "] " + event.message());
        });
        eventBindings.listen(workspaceBus, StudioAssetsWorkspaceRefreshStartedEvent.class).handle(event -> {
            appendLine("[refresh] Asset scan started.");
        });
        eventBindings.listen(workspaceBus, StudioAssetsWorkspaceRefreshedEvent.class).handle(event -> {
            appendLine("[refresh] " + event.assetCount() + " assets loaded.");
        });
        eventBindings.listen(workspaceBus, StudioAssetsWorkspaceRefreshFailedEvent.class).handle(event -> {
            appendLine("[error] " + event.message());
        });
        eventBindings.listen(Container.eventBus(), StudioPackerOperationEvent.class).handle(event -> {
            appendLine("[" + event.kind().name().toLowerCase() + "] " + event.summary());
        });
    }
}
