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

import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.Composite;
import com.vaadin.flow.component.applayout.DrawerToggle;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.html.H1;
import com.vaadin.flow.component.html.Header;
import com.vaadin.flow.theme.lumo.LumoUtility.*;

public final class ViewToolbar extends Composite<Header> {

    public ViewToolbar(String viewTitle, Component... components) {
        this(viewTitle, true, components);
    }

    public ViewToolbar(String viewTitle, boolean showDrawerToggle, Component... components) {
        addClassName("view-toolbar-host");
        getContent().addClassName("view-toolbar");
        addClassNames(Display.FLEX, FlexDirection.COLUMN, JustifyContent.BETWEEN, AlignItems.STRETCH, Gap.MEDIUM,
                FlexDirection.Breakpoint.Medium.ROW, AlignItems.Breakpoint.Medium.CENTER);

        Div toggleAndTitle;
        if (showDrawerToggle) {
            var drawerToggle = new DrawerToggle();
            drawerToggle.addClassNames(Margin.NONE);
            drawerToggle.addClassName("view-toolbar-toggle");
            var title = new H1(viewTitle);
            title.addClassNames(FontSize.XLARGE, Margin.NONE, FontWeight.LIGHT);
            title.addClassName("view-toolbar-title");
            title.getStyle().set("white-space", "nowrap");
            toggleAndTitle = new Div(drawerToggle, title);
        } else {
            var title = new H1(viewTitle);
            title.addClassNames(FontSize.XLARGE, Margin.NONE, FontWeight.LIGHT);
            title.addClassName("view-toolbar-title");
            title.getStyle().set("white-space", "nowrap");
            toggleAndTitle = new Div(title);
        }
        toggleAndTitle.addClassNames(Display.FLEX, AlignItems.CENTER);
        toggleAndTitle.addClassName("view-toolbar-title-row");
        getContent().add(toggleAndTitle);

        if (components.length > 0) {
            var actions = new Div(components);
            actions.addClassNames(Display.FLEX, FlexDirection.COLUMN, JustifyContent.END, Flex.GROW, Gap.SMALL,
                    FlexDirection.Breakpoint.Medium.ROW);
            actions.addClassName("view-toolbar-actions");
            getContent().add(actions);
        }
    }

    public static Component group(Component... components) {
        var group = new Div(components);
        group.addClassNames(Display.FLEX, FlexDirection.COLUMN, AlignItems.STRETCH, Gap.SMALL,
                FlexDirection.Breakpoint.Medium.ROW, AlignItems.Breakpoint.Medium.CENTER);
        group.addClassName("view-toolbar-group");
        return group;
    }
}
