package mx.gob.jumapacelaya.views;

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.html.Footer;
import com.vaadin.flow.component.html.H2;
import com.vaadin.flow.component.html.Image;
import com.vaadin.flow.component.html.Span;
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.router.PageTitle;
import com.vaadin.flow.theme.lumo.LumoUtility;
import mx.gob.jumapacelaya.api.SecurityService;
import mx.gob.jumapacelaya.views.crearnuevoticket.CrearnuevoTicketView;
import mx.gob.jumapacelaya.views.tickets.AllTicketsView;
import mx.gob.jumapacelaya.views.tickets.MisTicketsView;
import org.vaadin.lineawesome.LineAwesomeIcon;

/**
 * The main view is a top-level placeholder for other views.
 */
public class MainLayout extends AppLayout {

    private H2 viewTitle;
    private final SecurityService securityService;

    public MainLayout(SecurityService securityService) {
        this.securityService = securityService;
        this.getStyle().set("background-image", "url('images/bckgndNvo.png')");
        setPrimarySection(Section.DRAWER);
        addDrawerContent();
        addHeaderContent();
    }

    private void addHeaderContent() {
        DrawerToggle toggle = new DrawerToggle();
        toggle.setAriaLabel("Menu 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.getStyle().set("margin-right", "50px");
        logoutButton.addThemeVariants(ButtonVariant.LUMO_ERROR);

        HorizontalLayout headerContent = new HorizontalLayout();
        headerContent.setDefaultVerticalComponentAlignment(FlexComponent.Alignment.CENTER);
        headerContent.setWidthFull();
        headerContent.add(viewTitle);
        headerContent.setFlexGrow(1, viewTitle);
        headerContent.add(usrNameLabel, logoutButton);

        addToNavbar(true, toggle, headerContent);
    }

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


        Image imgLogo = new Image("images/imgDrawer.png", "Logo");
        imgLogo.setWidth("260px");
        headerLayout.add(imgLogo);


        Scroller scroller = new Scroller(createNavigation());
        scroller.getStyle().set("background-color", "#A02142");

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

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

        nav.addItem(new SideNavItem("Crear nuevo ticket", CrearnuevoTicketView.class, LineAwesomeIcon.EDIT.create()));
        nav.addItem(new SideNavItem("Mis tickets", MisTicketsView.class, LineAwesomeIcon.TICKET_ALT_SOLID.create()));
        nav.addItem(new SideNavItem("Todos los tickets", AllTicketsView.class, LineAwesomeIcon.LIST_ALT.create()));
        nav.getStyle().set("background-size", "cover"); // Hace que la imagen cubra todo el SideNav
        nav.getStyle().set("background-repeat", "no-repeat"); // Evita que la imagen se repita
        nav.getStyle().set("background-position", "center"); // Centra la imagen
        nav.getStyle().set("background-color", "white");
        nav.getStyle().set("border-radius", "5px");
        nav.getStyle().set("opacity", "0.9");

        return nav;
    }

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

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

    private String getCurrentPageTitle() {
        PageTitle title = getContent().getClass().getAnnotation(PageTitle.class);
        return title == null ? "" : title.value();
    }
}
