package de.assecutor.votianlt.pages.view;

import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.button.ButtonVariant;
import com.vaadin.flow.component.html.H1;
import com.vaadin.flow.component.notification.Notification;
import com.vaadin.flow.component.orderedlayout.FlexComponent;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.textfield.EmailField;
import com.vaadin.flow.router.HasDynamicTitle;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.server.VaadinService;
import com.vaadin.flow.server.auth.AnonymousAllowed;
import de.assecutor.votianlt.pages.service.PasswordResetService;

@Route("forgot-password-request")
@AnonymousAllowed
public class ForgotPasswordRequestView extends VerticalLayout implements HasDynamicTitle {

    public ForgotPasswordRequestView(PasswordResetService passwordResetService) {

        setSizeFull();
        setJustifyContentMode(FlexComponent.JustifyContentMode.CENTER);
        setDefaultHorizontalComponentAlignment(FlexComponent.Alignment.CENTER);
        setPadding(true);
        addClassName("form-page");

        VerticalLayout container = new VerticalLayout();
        container.setWidth("400px");
        container.setMaxWidth("100%");
        container.setPadding(true);
        container.setSpacing(true);
        container.setDefaultHorizontalComponentAlignment(FlexComponent.Alignment.STRETCH);
        container.addClassNames("form-shell", "narrow", "form-card");

        H1 title = new H1("Passwort zurücksetzen");
        title.addClassName("form-title");

        EmailField emailField = new EmailField("E-Mail-Adresse");
        emailField.setWidthFull();
        emailField.setRequired(true);
        emailField.setAutofocus(true);

        Button cancel = new Button("Abbrechen", e -> getUI().ifPresent(ui -> ui.navigate("login")));
        cancel.addThemeVariants(ButtonVariant.LUMO_TERTIARY);
        Button submit = new Button("E-Mail senden", e -> {
            String email = emailField.getValue();
            if (email == null || email.isBlank()) {
                Notification.show("Bitte E-Mail eingeben.", 3000, Notification.Position.MIDDLE);
                return;
            }
            String baseUrl = getBaseUrl();
            passwordResetService.initiateResetAuto(email.trim(), baseUrl);
            Notification.show("Falls die E-Mail existiert, wurde ein Link versendet.", 4000,
                    Notification.Position.MIDDLE);
            getUI().ifPresent(ui -> ui.navigate("login"));
        });
        submit.addThemeVariants(ButtonVariant.LUMO_PRIMARY);

        HorizontalLayout actions = new HorizontalLayout(cancel, submit);
        actions.setJustifyContentMode(FlexComponent.JustifyContentMode.END);

        container.add(title, emailField, actions);
        add(container);
    }

    private String getBaseUrl() {
        var vReq = VaadinService.getCurrentRequest();
        if (vReq instanceof com.vaadin.flow.server.VaadinServletRequest servletRequest) {
            var req = servletRequest.getHttpServletRequest();
            String scheme = req.getScheme();
            String serverName = req.getServerName();
            int serverPort = req.getServerPort();
            String contextPath = req.getContextPath();
            String portPart = ("http".equals(scheme) && serverPort == 80)
                    || ("https".equals(scheme) && serverPort == 443) ? "" : ":" + serverPort;
            return scheme + "://" + serverName + portPart + contextPath;
        }
        return "";
    }

    @Override
    public String getPageTitle() {
        return getTranslation("page.title.password.reset");
    }
}
