package p.studio.workspaces.assets.details.summary;

import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import p.studio.Container;
import p.studio.controls.lifecycle.StudioControlLifecycle;
import p.studio.controls.lifecycle.StudioControlLifecycleSupport;
import p.studio.lsp.events.StudioWorkspaceEventBus;
import p.studio.projects.ProjectReference;
import p.studio.utilities.i18n.I18n;
import p.studio.workspaces.assets.details.AssetDetailsUiSupport;
import p.studio.workspaces.assets.metadata.AssetStudioGlyphSpecialization;
import p.studio.workspaces.assets.metadata.AssetStudioSceneBankMetadata;
import p.studio.workspaces.assets.messages.AssetWorkspaceAssetSummary;
import p.studio.workspaces.assets.messages.AssetWorkspaceDetailsViewState;
import p.studio.workspaces.assets.messages.events.StudioAssetsDetailsViewStateChangedEvent;
import p.studio.workspaces.framework.StudioSubscriptionBag;

import java.util.Objects;

public final class AssetDetailsSummaryControl extends VBox implements StudioControlLifecycle {
    private final ProjectReference projectReference;
    private final StudioWorkspaceEventBus workspaceBus;
    private final StudioSubscriptionBag subscriptions = new StudioSubscriptionBag();

    private AssetWorkspaceDetailsViewState viewState;

    public AssetDetailsSummaryControl(ProjectReference projectReference, StudioWorkspaceEventBus workspaceBus) {
        StudioControlLifecycleSupport.install(this, this);
        this.projectReference = Objects.requireNonNull(projectReference, "projectReference");
        this.workspaceBus = Objects.requireNonNull(workspaceBus, "workspaceBus");
    }

    @Override
    public void subscribe() {
        subscriptions.add(workspaceBus.subscribe(StudioAssetsDetailsViewStateChangedEvent.class, event -> {
            viewState = event.viewState();
            render();
        }));
    }

    @Override
    public void unsubscribe() {
        subscriptions.clear();
    }

    private void render() {
        if (viewState == null || viewState.selectedAssetDetails() == null) {
            getChildren().clear();
            return;
        }
        final AssetWorkspaceAssetSummary summary = viewState.selectedAssetDetails().summary();
        if (summary == null) {
            getChildren().clear();
            return;
        }

        final VBox content = new VBox(8);
        content.getChildren().setAll(
                AssetDetailsUiSupport.createKeyValueRow(Container.i18n().text(I18n.ASSETS_LABEL_ASSET_ID), summary.assetId() == null ? "—" : String.valueOf(summary.assetId())),
                AssetDetailsUiSupport.createKeyValueRow(Container.i18n().text(I18n.ASSETS_LABEL_LOCATION), AssetDetailsUiSupport.projectRelativePath(projectReference, summary.assetRoot())));
        final HBox typeBox = new HBox(8);
        typeBox.getStyleClass().add("assets-details-chips");
        typeBox.getChildren().add(AssetDetailsUiSupport.createChip(
                AssetDetailsUiSupport.typeChipTone(summary.assetFamily()),
                AssetDetailsUiSupport.typeLabel(summary.assetFamily())));
        if (summary.glyphSpecialization() != AssetStudioGlyphSpecialization.NONE) {
            typeBox.getChildren().add(AssetDetailsUiSupport.createChip(
                    AssetDetailsUiSupport.typeChipTone(summary.assetFamily()),
                    AssetDetailsUiSupport.specializationLabel(summary.glyphSpecialization()),
                    "assets-details-chip-fixed"));
        }
        content.getChildren().add(AssetDetailsUiSupport.createKeyValueRow(
                Container.i18n().text(I18n.ASSETS_LABEL_TYPE),
                typeBox));
        content.getChildren().add(AssetDetailsUiSupport.createKeyValueRow(
                Container.i18n().text(I18n.ASSETS_LABEL_REGISTRATION),
                AssetDetailsUiSupport.createChip(
                        AssetDetailsUiSupport.registrationChipTone(summary.state()),
                        AssetDetailsUiSupport.registrationLabel(summary.state()))));
        content.getChildren().add(AssetDetailsUiSupport.createKeyValueRow(
                Container.i18n().text(I18n.ASSETS_LABEL_BUILD_PARTICIPATION),
                AssetDetailsUiSupport.createChip(
                        AssetDetailsUiSupport.buildChipTone(summary.buildParticipation()),
                        AssetDetailsUiSupport.buildParticipationLabel(summary.buildParticipation()))));
        getChildren().setAll(AssetDetailsUiSupport.createSection(
                Container.i18n().text(I18n.ASSETS_SECTION_SUMMARY),
                content));
    }
}
