package p.studio.controls.shell;

import p.studio.lsp.events.*;
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 java.util.Optional;

public final class StudioActivityEventMapper {
    private StudioActivityEventMapper() {
    }

    public static Optional<StudioActivityEntry> map(StudioEvent event) {
        return switch (event) {
            case StudioProjectOpenedEvent ignored ->
                    Optional.of(new StudioActivityEntry("Studio", "Project opened", StudioActivityEntrySeverity.SUCCESS, false));
            case StudioProjectLoadingStartedEvent ignored ->
                    Optional.of(new StudioActivityEntry("Studio", "Project loading started", StudioActivityEntrySeverity.INFO, false));
            case StudioProjectLoadingCompletedEvent ignored ->
                    Optional.of(new StudioActivityEntry("Studio", "Project ready", StudioActivityEntrySeverity.SUCCESS, false));
            case StudioProjectLoadingFailedEvent failed ->
                    Optional.of(new StudioActivityEntry("Studio", failed.message(), StudioActivityEntrySeverity.ERROR, true));
            case StudioAssetsWorkspaceRefreshStartedEvent ignored ->
                    Optional.of(new StudioActivityEntry("Assets", "Asset scan started", StudioActivityEntrySeverity.INFO, false));
            case StudioAssetsWorkspaceRefreshedEvent refreshed ->
                    Optional.of(new StudioActivityEntry("Assets", refreshed.assetCount() + " assets loaded", StudioActivityEntrySeverity.SUCCESS, false));
            case StudioAssetsWorkspaceRefreshFailedEvent failed ->
                    Optional.of(new StudioActivityEntry("Assets", failed.message(), StudioActivityEntrySeverity.ERROR, true));
            case StudioPackerOperationEvent packerEvent ->
                    Optional.of(new StudioActivityEntry("Assets", packerEvent.summary(), severity(packerEvent), packerEvent.kind() == p.packer.events.PackerEventKind.ACTION_FAILED));
            case StudioExecutionLifecycleEvent executionEvent ->
                    Optional.of(new StudioActivityEntry(executionEvent.source(), executionEvent.message(), executionEvent.severity(), executionEvent.sticky()));
            default -> Optional.empty();
        };
    }

    private static StudioActivityEntrySeverity severity(StudioPackerOperationEvent event) {
        return switch (event.kind()) {
            case ACTION_APPLIED -> StudioActivityEntrySeverity.SUCCESS;
            case ACTION_FAILED -> StudioActivityEntrySeverity.ERROR;
            default -> StudioActivityEntrySeverity.INFO;
        };
    }
}
