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.packer.messages.assets.OutputFormatCatalog;
import p.studio.Container;
import p.studio.controls.forms.StudioFormMode;
import p.studio.controls.forms.StudioFormSection;
import p.studio.controls.forms.StudioSection;
import p.studio.projects.ProjectReference;
import p.studio.utilities.i18n.I18n;
import p.studio.workspaces.assets.metadata.AssetStudioGlyphSpecialization;
import p.studio.workspaces.assets.messages.AssetWorkspaceSceneBankStatus;
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) {
        return StudioSection.createSection(title, content);
    }

    public static VBox createSectionWithoutHeader(Node content) {
        return StudioSection.createSectionWithoutHeader(content);
    }

    public static VBox createSection(String title, Node headerActions, Node content) {
        return StudioFormSection.createEditableSection(title, headerActions, content, StudioFormMode.READ_ONLY);
    }

    public static VBox createEditableSection(String title, Node headerActions, Node content, StudioFormMode mode) {
        return StudioFormSection.createEditableSection(title, headerActions, content, mode);
    }

    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, OutputFormatCatalog outputFormat) {
        final Label valueLabel = new Label(outputFormat.displayName());
        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 Label createChip(String toneClass, String text, String... extraClasses) {
        final Label chip = createChip(toneClass, text);
        if (extraClasses != null && extraClasses.length > 0) {
            chip.getStyleClass().addAll(extraClasses);
        }
        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 void applyActionTone(Button button, String toneClass) {
        button.getStyleClass().removeAll(
                "studio-button-primary",
                "studio-button-warning",
                "studio-button-danger",
                "studio-button-build-include",
                "studio-button-build-exclude");
        button.getStyleClass().add(toneClass);
    }

    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 GLYPH_BANK -> Container.i18n().text(I18n.ASSETS_TYPE_GLYPH_BANK);
            case SCENE_BANK -> Container.i18n().text(I18n.ASSETS_TYPE_SCENE_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 GLYPH_BANK -> "assets-details-chip-image";
            case SCENE_BANK -> "assets-details-chip-scene";
            case SOUND_BANK -> "assets-details-chip-audio";
            case UNKNOWN -> "assets-details-chip-generic";
        };
    }

    public static String specializationLabel(AssetStudioGlyphSpecialization specialization) {
        return switch (specialization) {
            case NONE -> Container.i18n().text(I18n.ASSETS_SPECIALIZATION_NONE);
            case TILESET -> Container.i18n().text(I18n.ASSETS_SPECIALIZATION_TILESET);
            case SPRITES -> Container.i18n().text(I18n.ASSETS_SPECIALIZATION_SPRITES);
            case UI -> Container.i18n().text(I18n.ASSETS_SPECIALIZATION_UI);
        };
    }

    public static String sceneBankStatusLabel(AssetWorkspaceSceneBankStatus status) {
        return switch (status) {
            case NOT_APPLICABLE -> "";
            case PENDING_VALIDATION -> Container.i18n().text(I18n.ASSETS_SCENE_STATUS_PENDING_VALIDATION);
            case VALIDATED_PENDING_ACCEPTANCE -> Container.i18n().text(I18n.ASSETS_SCENE_STATUS_VALIDATED_PENDING_ACCEPTANCE);
            case READY -> Container.i18n().text(I18n.ASSETS_SCENE_STATUS_READY);
            case VALIDATION_FAILED -> Container.i18n().text(I18n.ASSETS_SCENE_STATUS_VALIDATION_FAILED);
        };
    }

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

    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();
        }
    }
}
