package p.studio.workspaces.assets.details;

import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import p.packer.messages.assets.AssetAction;
import p.packer.messages.assets.AssetFamilyCatalog;
import p.studio.Container;
import p.studio.projects.ProjectReference;
import p.studio.utilities.i18n.I18n;
import p.studio.workspaces.assets.messages.AssetWorkspaceAssetState;
import p.studio.workspaces.assets.messages.AssetWorkspaceBuildParticipation;

import java.nio.file.Path;

public final class AssetDetailsUiSupport {
    private AssetDetailsUiSupport() {
    }

    public static VBox createSection(String title, Node content) {
        final VBox section = new VBox(10);
        section.getStyleClass().add("assets-details-section");
        final Label titleLabel = new Label(title);
        titleLabel.getStyleClass().add("assets-details-section-title");
        section.getChildren().addAll(titleLabel, content);
        return section;
    }

    public static Node createSectionMessage(String text) {
        final Label label = new Label(text);
        label.setWrapText(true);
        label.getStyleClass().add("assets-details-section-message");
        return label;
    }

    public static Node createKeyValueRow(String key, String value) {
        final Label valueLabel = new Label(value);
        valueLabel.getStyleClass().add("assets-details-value");
        valueLabel.setWrapText(true);
        return createKeyValueRow(key, valueLabel);
    }

    public static Node createKeyValueRow(String key, Node valueNode) {
        final HBox row = new HBox(12);
        row.setAlignment(Pos.TOP_LEFT);
        final Label keyLabel = new Label(key);
        keyLabel.getStyleClass().add("assets-details-key");
        HBox.setHgrow(valueNode, Priority.ALWAYS);
        row.getChildren().addAll(keyLabel, valueNode);
        return row;
    }

    public static Label createChip(String toneClass, String text) {
        final Label chip = new Label(text);
        chip.getStyleClass().addAll("assets-details-chip", toneClass);
        return chip;
    }

    public static Button createActionButton(String text) {
        final Button button = new Button(text);
        button.getStyleClass().addAll("studio-button", "studio-button-primary", "assets-details-action-button");
        button.setMaxWidth(Double.MAX_VALUE);
        return button;
    }

    public static String registrationLabel(AssetWorkspaceAssetState state) {
        return switch (state) {
            case REGISTERED -> Container.i18n().text(I18n.ASSETS_VALUE_REGISTERED);
            case UNREGISTERED -> Container.i18n().text(I18n.ASSETS_VALUE_UNREGISTERED);
        };
    }

    public static 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);
        };
    }

    public static String booleanLabel(boolean value) {
        return value
                ? Container.i18n().text(I18n.ASSETS_VALUE_YES)
                : Container.i18n().text(I18n.ASSETS_VALUE_NO);
    }

    public static String typeLabel(AssetFamilyCatalog assetFamily) {
        return switch (assetFamily) {
            case IMAGE_BANK -> Container.i18n().text(I18n.ASSETS_TYPE_IMAGE_BANK);
            case PALETTE_BANK -> Container.i18n().text(I18n.ASSETS_TYPE_PALETTE_BANK);
            case SOUND_BANK -> Container.i18n().text(I18n.ASSETS_TYPE_SOUND_BANK);
            case UNKNOWN -> Container.i18n().text(I18n.ASSETS_TYPE_UNKNOWN);
        };
    }

    public static String typeChipTone(AssetFamilyCatalog assetFamily) {
        return switch (assetFamily) {
            case IMAGE_BANK -> "assets-details-chip-image";
            case PALETTE_BANK -> "assets-details-chip-palette";
            case SOUND_BANK -> "assets-details-chip-audio";
            case UNKNOWN -> "assets-details-chip-generic";
        };
    }

    public static String actionLabel(AssetAction action) {
        return switch (action) {
            case REGISTER -> Container.i18n().text(I18n.ASSETS_ACTION_REGISTER);
        };
    }

    public static String registrationChipTone(AssetWorkspaceAssetState state) {
        return switch (state) {
            case REGISTERED -> "assets-details-chip-registered";
            case UNREGISTERED -> "assets-details-chip-unregistered";
        };
    }

    public static String buildChipTone(AssetWorkspaceBuildParticipation buildParticipation) {
        return switch (buildParticipation) {
            case INCLUDED -> "assets-details-chip-included";
            case EXCLUDED -> "assets-details-chip-excluded";
        };
    }

    public static String projectRelativePath(ProjectReference projectReference, Path path) {
        try {
            return projectReference.rootPath().relativize(path.toAbsolutePath().normalize()).toString();
        } catch (RuntimeException runtimeException) {
            return path.toString();
        }
    }
}
