package de.assecutor.votianlt.pages.base.ui.component;

import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.button.ButtonVariant;
import com.vaadin.flow.component.dialog.Dialog;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.html.Span;
import com.vaadin.flow.component.orderedlayout.FlexComponent;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;

/**
 * Shared helper for dialogs that should use the station-dialog shell.
 */
public final class DialogStylingHelper {

    private DialogStylingHelper() {
    }

    public static Dialog createStyledDialog(String title, String width) {
        Dialog dialog = new Dialog();
        apply(dialog, title, width);
        return dialog;
    }

    public static Dialog createConfirmationDialog(String title, String message, String width, String cancelText,
            String confirmText, Runnable onConfirm, ButtonVariant... confirmVariants) {
        Dialog dialog = createStyledDialog(title, width);
        dialog.setCloseOnEsc(true);
        dialog.setCloseOnOutsideClick(true);

        VerticalLayout dialogContent = createContentLayout("320px");
        if (message != null && !message.isBlank()) {
            dialogContent.add(new Span(message));
        }

        Button cancelButton = new Button(cancelText, event -> dialog.close());
        cancelButton.addThemeVariants(ButtonVariant.LUMO_TERTIARY);

        Button confirmButton = new Button(confirmText, event -> {
            dialog.close();
            onConfirm.run();
        });
        if (confirmVariants != null && confirmVariants.length > 0) {
            confirmButton.addThemeVariants(confirmVariants);
        }

        dialog.add(wrapContent(dialogContent));
        dialog.getFooter().add(cancelButton, confirmButton);
        return dialog;
    }

    public static void apply(Dialog dialog, String title, String width) {
        if (title != null && !title.isBlank()) {
            dialog.setHeaderTitle(title);
        }
        if (width != null && !width.isBlank()) {
            dialog.setWidth(width);
        }
        dialog.setMaxWidth("95vw");
        dialog.getElement().setAttribute("theme", "no-inner-card");
    }

    public static Component wrapContent(Component content) {
        return wrapContent(content, false);
    }

    public static Component wrapContent(Component content, boolean fillHeight) {
        Div frame = new Div();
        frame.getStyle().set("border", "10px solid transparent");
        frame.getStyle().set("border-radius", "0");
        frame.getStyle().set("box-sizing", "border-box");
        if (fillHeight) {
            frame.getStyle().set("display", "flex");
            frame.getStyle().set("flex-direction", "column");
            frame.getStyle().set("height", "100%");
            frame.getStyle().set("min-height", "0");
            frame.getStyle().set("flex", "1");
        }
        frame.setWidthFull();

        Div whiteCard = new Div();
        whiteCard.getStyle().set("background", "white");
        whiteCard.getStyle().set("border-radius", "24px");
        if (fillHeight) {
            whiteCard.getStyle().set("display", "flex");
            whiteCard.getStyle().set("flex-direction", "column");
            whiteCard.getStyle().set("height", "100%");
            whiteCard.getStyle().set("min-height", "0");
            whiteCard.getStyle().set("flex", "1");
            whiteCard.getStyle().set("overflow", "hidden");
            content.getElement().getStyle().set("width", "100%");
            content.getElement().getStyle().set("height", "100%");
            content.getElement().getStyle().set("min-height", "0");
            content.getElement().getStyle().set("flex", "1");
        } else {
            whiteCard.getStyle().set("overflow", "auto");
        }
        whiteCard.setWidthFull();
        whiteCard.add(content);

        frame.add(whiteCard);
        return frame;
    }

    public static VerticalLayout createContentLayout(String maxWidth) {
        return createContentLayout(maxWidth, FlexComponent.Alignment.STRETCH);
    }

    public static VerticalLayout createContentLayout(String maxWidth, FlexComponent.Alignment alignment) {
        VerticalLayout content = new VerticalLayout();
        content.setPadding(true);
        content.setSpacing(true);
        content.setWidthFull();
        if (maxWidth != null && !maxWidth.isBlank()) {
            content.setMaxWidth(maxWidth);
        }
        content.setDefaultHorizontalComponentAlignment(alignment);
        content.getStyle().set("margin", "0 auto");
        return content;
    }
}
