package p.studio.workspaces.assets.list;

import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Region;
import javafx.scene.layout.VBox;
import p.packer.messages.assets.AssetFamilyCatalog;
import p.studio.Container;
import p.studio.lsp.events.StudioWorkspaceEventBus;
import p.studio.projects.ProjectReference;
import p.studio.utilities.i18n.I18n;
import p.studio.workspaces.assets.messages.AssetWorkspaceAssetState;
import p.studio.workspaces.assets.messages.AssetWorkspaceAssetSummary;
import p.studio.workspaces.assets.messages.AssetWorkspaceBuildParticipation;
import p.studio.workspaces.assets.messages.events.StudioAssetsWorkspaceSelectionRequestedEvent;

import java.nio.file.Path;
import java.util.Objects;

public final class AssetListItemControl extends VBox {
    private final ProjectReference projectReference;
    private final Path projectRoot;
    private final StudioWorkspaceEventBus workspaceBus;

    private AssetWorkspaceAssetSummary summary;

    public AssetListItemControl(
            ProjectReference projectReference,
            StudioWorkspaceEventBus workspaceBus,
            AssetWorkspaceAssetSummary summary) {
        this.projectReference = Objects.requireNonNull(projectReference, "projectReference");
        this.projectRoot = this.projectReference.rootPath();
        this.workspaceBus = Objects.requireNonNull(workspaceBus, "workspaceBus");
        this.summary = Objects.requireNonNull(summary, "summary");
        render();
    }

    private void render() {
        getChildren().clear();
        getStyleClass().setAll("assets-workspace-asset-row", assetRowToneClass(summary.assetFamily()));
        getChildren().setAll(createTopLine(), createPathLabel());
        setOnMouseClicked(event -> workspaceBus.publish(
                new StudioAssetsWorkspaceSelectionRequestedEvent(summary.assetReference(), false)));
    }

    private HBox createTopLine() {
        final var topLine = new HBox(8);
        topLine.setAlignment(Pos.CENTER_LEFT);
        topLine.getChildren().addAll(
                createNameLabel(),
                createSpacer(),
                createBadgesBox());
        return topLine;
    }

    private Label createNameLabel() {
        final var name = new Label(summary.assetName());
        name.getStyleClass().addAll("assets-workspace-asset-name", assetNameToneClass(summary.assetFamily()));
        return name;
    }

    private Region createSpacer() {
        final var spacer = new Region();
        HBox.setHgrow(spacer, Priority.ALWAYS);
        return spacer;
    }

    private HBox createBadgesBox() {
        final var badges = new HBox(6);
        badges.setAlignment(Pos.CENTER_RIGHT);
        badges.getStyleClass().add("assets-workspace-asset-badges");
        populateBadges(badges);
        return badges;
    }

    private Label createPathLabel() {
        final var path = new Label(AssetListProjectionBuilder.relativeRoot(summary, projectRoot));
        path.getStyleClass().add("assets-workspace-asset-path");
        return path;
    }

    private void populateBadges(HBox badges) {
        if (summary.state() == AssetWorkspaceAssetState.UNREGISTERED) {
            badges.getChildren().add(createBadge(Container.i18n().text(I18n.ASSETS_BADGE_UNREGISTERED), "assets-workspace-badge-orphan"));
        } else if (summary.buildParticipation() == AssetWorkspaceBuildParticipation.INCLUDED) {
            badges.getChildren().add(createBadge(buildParticipationLabel(summary.buildParticipation()), "assets-workspace-badge-preload"));
            if (summary.preload()) {
                badges.getChildren().add(createBadge(Container.i18n().text(I18n.ASSETS_BADGE_PRELOAD), "assets-workspace-badge-preload"));
            }
        } else {
            badges.getChildren().add(createBadge(buildParticipationLabel(summary.buildParticipation()), "assets-workspace-badge-diagnostics"));
        }
        if (summary.hasDiagnostics()) {
            badges.getChildren().add(createBadge(Container.i18n().text(I18n.ASSETS_BADGE_DIAGNOSTICS), "assets-workspace-badge-diagnostics"));
        }
    }

    private Label createBadge(String text, String styleClass) {
        final Label badge = new Label(text);
        badge.getStyleClass().addAll("assets-workspace-badge", styleClass);
        return badge;
    }

    private String buildParticipationLabel(AssetWorkspaceBuildParticipation buildParticipation) {
        return switch (buildParticipation) {
            case INCLUDED -> Container.i18n().text(I18n.ASSETS_VALUE_INCLUDED);
            case EXCLUDED -> Container.i18n().text(I18n.ASSETS_VALUE_EXCLUDED);
        };
    }

    private String assetRowToneClass(AssetFamilyCatalog assetFamily) {
        return switch (assetFamily) {
            case GLYPH_BANK -> "assets-workspace-asset-row-tone-image";
            case SCENE_BANK -> "assets-workspace-asset-row-tone-scene";
            case SOUND_BANK -> "assets-workspace-asset-row-tone-audio";
            default -> "assets-workspace-asset-row-tone-generic";
        };
    }

    private String assetNameToneClass(AssetFamilyCatalog assetFamily) {
        return switch (assetFamily) {
            case GLYPH_BANK -> "assets-workspace-asset-name-tone-image";
            case SCENE_BANK -> "assets-workspace-asset-name-tone-scene";
            case SOUND_BANK -> "assets-workspace-asset-name-tone-audio";
            default -> "assets-workspace-asset-name-tone-generic";
        };
    }
}
