package de.assecutor.votianlt.pages.view;

import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.html.H2;
import com.vaadin.flow.component.html.IFrame;
import com.vaadin.flow.component.notification.Notification;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.server.StreamResource;
import de.assecutor.votianlt.pages.base.ui.view.AdminLayout;
import de.assecutor.votianlt.service.CustomerInvoiceService;
import de.assecutor.votianlt.service.SystemInvoiceService;
import jakarta.annotation.security.RolesAllowed;

import java.io.ByteArrayInputStream;

@Route(value = "pdf-test", layout = AdminLayout.class)
@PageTitle("PDF Test")
@RolesAllowed("ADMIN")
public class PdfTestView extends VerticalLayout {
    private final SystemInvoiceService systemInvoiceService;
    private final CustomerInvoiceService customerInvoiceService;

    public PdfTestView(SystemInvoiceService systemInvoiceService, CustomerInvoiceService customerInvoiceService) {
        this.systemInvoiceService = systemInvoiceService;
        this.customerInvoiceService = customerInvoiceService;

        setSpacing(false);
        setPadding(false);
        getStyle().set("margin", "14px");
        setWidth("90%");

        H2 title = new H2("PDF Test");
        add(title);

        Button generateHtmlPdfButton = new Button("PDF aus system_invoice.html generieren");
        generateHtmlPdfButton.addClickListener(e -> generateHtmlPdf());

        Button generateCustomerInvoicePdfButton = new Button("PDF aus customer_invoice.html generieren");
        generateCustomerInvoicePdfButton.addClickListener(e -> generateCustomerInvoicePdf());

        // Create button layout
        HorizontalLayout buttonLayout = new HorizontalLayout();
        buttonLayout.add(generateHtmlPdfButton, generateCustomerInvoicePdfButton);
        buttonLayout.setSpacing(true);

        // Initialize PDF viewer
        IFrame pdfViewer = new IFrame();
        pdfViewer.setWidth("100%");
        pdfViewer.setHeight("800px");
        pdfViewer.getStyle().set("border", "1px solid #ccc");
        pdfViewer.setVisible(false);

        add(buttonLayout);
        add(pdfViewer);
    }

    private void generateHtmlPdf() {
        try {
            byte[] pdfBytes = systemInvoiceService.generateInvoicePdfFromHtml();

            StreamResource resource = new StreamResource("vlt-invoice.pdf",
                    () -> new ByteArrayInputStream(pdfBytes));
            resource.setContentType("application/pdf");

            getUI().ifPresent(ui -> {
                var registration = ui.getSession().getResourceRegistry().registerResource(resource);
                ui.getPage().open(registration.getResourceUri().toString(), "_blank");
            });

            Notification.show("PDF aus HTML erfolgreich generiert!", 3000, Notification.Position.BOTTOM_CENTER);
        } catch (Exception ex) {
            Notification.show("Fehler beim Generieren des PDFs aus HTML: " + ex.getMessage(),
                             5000, Notification.Position.BOTTOM_CENTER);
        }
    }

    private void generateCustomerInvoicePdf() {
        try {
            byte[] pdfBytes = customerInvoiceService.generateCustomerInvoicePdf();

            StreamResource resource = new StreamResource("customer-invoice.pdf",
                    () -> new ByteArrayInputStream(pdfBytes));
            resource.setContentType("application/pdf");

            getUI().ifPresent(ui -> {
                var registration = ui.getSession().getResourceRegistry().registerResource(resource);
                ui.getPage().open(registration.getResourceUri().toString(), "_blank");
            });

            Notification.show("Customer PDF erfolgreich generiert!", 3000, Notification.Position.BOTTOM_CENTER);
        } catch (Exception ex) {
            Notification.show("Fehler beim Generieren des Customer PDFs: " + ex.getMessage(),
                             5000, Notification.Position.BOTTOM_CENTER);
        }
    }
}
