package p.studio.window;

import javafx.scene.layout.BorderPane;
import p.studio.Container;
import p.studio.controls.shell.*;
import p.studio.events.StudioWorkspaceSelectedEvent;
import p.studio.projects.ProjectReference;
import p.studio.utilities.i18n.I18n;
import p.studio.workspaces.WorkspaceHost;
import p.studio.workspaces.WorkspaceId;
import p.studio.workspaces.assets.AssetWorkspace;
import p.studio.workspaces.builder.ShipperWorkspace;
import p.studio.workspaces.editor.EditorWorkspace;

import java.util.List;

public final class MainView extends BorderPane {
    private final WorkspaceHost host = new WorkspaceHost();
    private final ProjectReference projectReference;

    public MainView(final ProjectReference projectReference) {
        this.projectReference = projectReference;
        final var menuBar = new StudioShellMenuBarControl();
        final var runSurface = new StudioRunSurfaceControl();
        setTop(new StudioShellTopBarControl(menuBar));

        host.register(new AssetWorkspace(projectReference));
        host.register(new EditorWorkspace(projectReference));
//        host.register(new PlaceholderWorkspace(WorkspaceId.DEBUG, I18n.WORKSPACE_DEBUG, "Debug"));
        host.register(new ShipperWorkspace(projectReference));

        final var workspaceRail = new StudioWorkspaceRailControl<>(
                List.of(
                        new StudioWorkspaceRailItem<>(WorkspaceId.ASSETS, "📦", Container.i18n().bind(I18n.WORKSPACE_ASSETS)),
                        new StudioWorkspaceRailItem<>(WorkspaceId.EDITOR, "📝", Container.i18n().bind(I18n.WORKSPACE_CODE)),
                        new StudioWorkspaceRailItem<>(WorkspaceId.DEBUG, "🎮", Container.i18n().bind(I18n.WORKSPACE_DEBUG)),
                        new StudioWorkspaceRailItem<>(WorkspaceId.SHIPPER, "⚙️", Container.i18n().bind(I18n.WORKSPACE_SHIPPER))
                ),
                this::loadWorkspace);
        setLeft(workspaceRail);
        setCenter(host);
        setRight(new StudioRightUtilityPanelControl(
                runSurface,
                Container.i18n().bind(I18n.SHELL_ACTIVITY),
                new StudioActivityFeedControl(projectReference)));

        // default
        workspaceRail.select(WorkspaceId.ASSETS);
        loadWorkspace(WorkspaceId.ASSETS);
    }

    public ProjectReference projectReference() {
        return projectReference;
    }

    private void loadWorkspace(WorkspaceId workspaceId) {
        host.change(workspaceId);
        Container.eventBus().publish(new StudioWorkspaceSelectedEvent(workspaceId));
    }
}
