package de.thpeetz.kontor.common.views;

import com.vaadin.flow.component.applayout.AppLayout;
import com.vaadin.flow.component.applayout.AppLayout.Section;
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.orderedlayout.FlexComponent;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.orderedlayout.Scroller;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.sidenav.SideNav;
import com.vaadin.flow.router.RouterLink;
import com.vaadin.flow.theme.lumo.LumoUtility;

import de.thpeetz.kontor.admin.AdminConstants;
import de.thpeetz.kontor.admin.services.AdminService;
import de.thpeetz.kontor.bookshelf.BookshelfConstants;
import de.thpeetz.kontor.comics.ComicConstants;
import de.thpeetz.kontor.security.SecurityService;
import de.thpeetz.kontor.tysc.TyscConstants;
import lombok.*;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class KontorLayoutUtil {

    private final AppLayout appLayout;
    @Setter
    private HorizontalLayout secondaryNavigation;

    private final AdminService adminService;

    private final SecurityService securityService;

    public KontorLayoutUtil(AppLayout layout, AdminService adminService, SecurityService securityService) {
        this.adminService = adminService;
        this.securityService = securityService;
        this.appLayout = layout;
    }

    public void createHeader(String titleName) {
        appLayout.addToDrawer(createTitle(), getScroller());
        appLayout.addToNavbar(getHeader(titleName));
        appLayout.setPrimarySection(Section.DRAWER);
    }

    private H1 createTitle() {
        H1 appTitle = new H1(new RouterLink("Kontor", MainView.class));
        appTitle.getStyle().set("font-size", "var(--lumo-font-size-l)")
                .set("line-height", "var(--lumo-size-l)")
                .set("margin", "0 var(--lumo-space-m)");
        return appTitle;
    }

    private Scroller getScroller() {
        Scroller scroller = new Scroller(getPrimaryNavigation());
        scroller.setClassName(LumoUtility.Padding.SMALL);
        return scroller;
    }

    private VerticalLayout getHeader(String header) {
        DrawerToggle toggle = new DrawerToggle();
        H2 viewTitle = new H2(header);
        viewTitle.addClassNames(LumoUtility.FontSize.LARGE, LumoUtility.Margin.NONE);
        HorizontalLayout subViews = this.secondaryNavigation;

        AvatarMenuBar avatar = new AvatarMenuBar(securityService, adminService);
        HorizontalLayout wrapper = new HorizontalLayout(toggle, viewTitle, avatar);
        wrapper.setDefaultVerticalComponentAlignment(FlexComponent.Alignment.CENTER);
        wrapper.expand(viewTitle);
        wrapper.setWidthFull();
        wrapper.setSpacing(false);

        VerticalLayout viewHeader = new VerticalLayout(wrapper, subViews);
        viewHeader.setPadding(false);
        viewHeader.setSpacing(false);
        return viewHeader;
    }

    private SideNav getPrimaryNavigation() {
        SideNav sideNav = new SideNav();
        sideNav.addItem(ComicConstants.getComicsNavigation());
        sideNav.addItem(TyscConstants.getTyscNavigation());
        sideNav.addItem(BookshelfConstants.getBookshelfNavigation());
        securityService.getAuthenticatedUser().ifPresent(user -> {
            log.info("User {} found", user.getUsername());
            boolean isAdmin = user.getAuthorities().stream()
                    .anyMatch(grantedAuthority -> "ROLE_ADMIN".equals(grantedAuthority.getAuthority()));
            log.info("User {} hat Admin-Rechte: {}", user, isAdmin);
            if (isAdmin) {
                sideNav.addItem(AdminConstants.getAdminNavigation());
            }
        });
        return sideNav;
    }
}
