package de.assecutor.pdftool.ui;

import com.vaadin.flow.component.applayout.AppLayout;
import com.vaadin.flow.component.applayout.DrawerToggle;
import com.vaadin.flow.component.html.H1;
import com.vaadin.flow.component.html.H2;
import com.vaadin.flow.component.icon.VaadinIcon;
import com.vaadin.flow.component.orderedlayout.Scroller;
import com.vaadin.flow.component.sidenav.SideNav;
import com.vaadin.flow.component.sidenav.SideNavItem;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.theme.lumo.LumoUtility;

/**
 * Rahmenlayout der Anwendung mit linker Sidebar: Navigation zwischen der
 * E-Rechnungs-Erzeugung, dem Rechnungsgenerator (Template-Editor) und dem
 * Adressbuch. Der Titel der aktiven Seite wird oben in der Navbar neben dem
 * Hamburger-Menü angezeigt.
 */
public class MainLayout extends AppLayout {

    /** Titel der aktiven Seite in der Navbar. */
    private final H2 viewTitle = new H2();

    public MainLayout() {
        setPrimarySection(Section.DRAWER);

        H1 appName = new H1("AIT");
        appName.addClassNames(LumoUtility.FontSize.LARGE, LumoUtility.Margin.Vertical.MEDIUM);
        appName.setWidth("100%");
        appName.getStyle().set("text-align", "center");

        SideNav nav = new SideNav();
        nav.addItem(new SideNavItem("E-Rechnung erzeugen", MainView.class, VaadinIcon.INVOICE.create()));
        nav.addItem(new SideNavItem("Templates", TemplateGeneratorView.class,
                VaadinIcon.FILE_PROCESS.create()));
        nav.addItem(new SideNavItem("Adressbuch", AddressBookView.class, VaadinIcon.BOOK.create()));

        Scroller scroller = new Scroller(nav);
        scroller.addClassNames(LumoUtility.Padding.SMALL);

        viewTitle.addClassNames(LumoUtility.FontSize.LARGE, LumoUtility.Margin.NONE);

        addToDrawer(appName, scroller);
        addToNavbar(new DrawerToggle(), viewTitle);
    }

    @Override
    protected void afterNavigation() {
        super.afterNavigation();
        viewTitle.setText(getCurrentPageTitle());
    }

    /** @return der Titel der aktiven View aus deren {@code @PageTitle}-Annotation. */
    private String getCurrentPageTitle() {
        PageTitle title = getContent().getClass().getAnnotation(PageTitle.class);
        return title != null ? title.value() : "";
    }
}
