package mx.gob.jumapacelaya.ui;

import com.vaadin.flow.component.applayout.AppLayout;
import com.vaadin.flow.component.applayout.DrawerToggle;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.button.ButtonVariant;
import com.vaadin.flow.component.dependency.CssImport;
import com.vaadin.flow.component.html.*;
import com.vaadin.flow.component.icon.VaadinIcon;
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.component.sidenav.SideNavItem;
import com.vaadin.flow.theme.lumo.LumoUtility;
import mx.gob.jumapacelaya.services.SecurityService;


@CssImport("./themes/sistema-mantenimiento/styles.css")
public class MainLayout extends AppLayout {

    private H2 viewTitle;
    private  final SecurityService securityService;

    public MainLayout(SecurityService securityService) {
        this.securityService = securityService;
        this.addClassName("app-layout");
        setPrimarySection(Section.DRAWER);
        addDrawerContent();
        addHeaderContent();
    }

    private void addHeaderContent() {
        DrawerToggle toggle = new DrawerToggle();
        toggle.setAriaLabel("Menu toggle");
        toggle.addClassName("drawer-toggle");

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


        String u = securityService.getAuthenticatedUser();
        Span usrNameLabel = new Span("Hola " + u);

        Button logoutButton = new Button("Cerrar sesión", event -> {
            securityService.logout();
        });
        logoutButton.addClassName("logout-button");
        logoutButton.addThemeVariants(ButtonVariant.LUMO_PRIMARY ,ButtonVariant.LUMO_ERROR);

        HorizontalLayout headerContent = new HorizontalLayout();
        headerContent.setDefaultVerticalComponentAlignment(FlexComponent.Alignment.CENTER);
        headerContent.addClassName("header-content");
        headerContent.add(viewTitle);
        headerContent.setFlexGrow(1, viewTitle);
        headerContent.add(usrNameLabel, logoutButton);

        addToNavbar(false, toggle, headerContent);
    }

    private void addDrawerContent() {
        VerticalLayout headerLayout = new VerticalLayout();
        headerLayout.setPadding(false);
        headerLayout.setSpacing(false);
        headerLayout.setAlignItems(FlexComponent.Alignment.CENTER);

        Image imgLogo = new Image("images/1027x160.png", "Logo");
        imgLogo.addClassName("drawer-logo");

        headerLayout.add(imgLogo);

        Scroller scroller = new Scroller(createNavigation());

        addToDrawer(headerLayout, scroller, createFooter());
    }

    private SideNav createNavigation() {
        SideNav nav = new SideNav();

        nav.addItem(new SideNavItem("Plan Anual", PlanAnualView.class, VaadinIcon.CALENDAR.create()));
        nav.addItem(new SideNavItem("Listado de Actividades", ActDiariaView.class, VaadinIcon.EDIT.create()));
        nav.addItem(new SideNavItem("Mantenimiento", MantenimientoView.class, VaadinIcon.WRENCH.create()));
        return nav;
    }

    private Footer createFooter() {
        Footer layout = new Footer();

        return layout;
    }

    @Override
    protected void afterNavigation() {
        super.afterNavigation();
        viewTitle.setText("Sistema de Mantenimiento de Hardware");
    }
}
