/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.flamingo.test.docker;

import java.net.URL;
import java.util.Map;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.xwiki.export.pdf.test.po.PDFDocument;
import org.xwiki.test.docker.junit5.TestConfiguration;
import org.xwiki.test.docker.junit5.UITest;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
 * Tests related to the server-side PDF export (to be moved when the PDF export feature is moved out of oldcore).
 *
 * @version $Id$
 */
// Note: vnc is set to false since otherwise our test framework fails to take a video since no page has been accessed
// by the test. Since we don't need VNC, we turn it off and thus no video is taken.
@UITest(vnc = false)
class PDFExportIT
{
    private String prefix;

    @BeforeEach
    void beforeEach(TestConfiguration testConfiguration)
    {
        String host = testConfiguration.getServletEngine().getHostIP();
        this.prefix = String.format("http://%s:8080/", host);
    }

    /**
     * Verify that the PDF export feature works on a single simple page by downloading the PDF and parsing it using
     * PDFBox.
     *
     * @see "XWIKI-7048: PDF export templates can display properties of other objects if the XWiki.PDFClass object is
     *      missing"
     */
    @Test
    void exportSingleSimplePageAsPDF() throws Exception
    {
        // We're using Dashboard.WebHome page because it has objects of type XWiki.GadgetClass and they have a title
        // property which was mistaken with the title property of XWiki.PDFClass before XWIKI-7048 was fixed. The gadget
        // title contains Velocity code that isn't wrapped in a Velocity macro so it is printed as is if not rendered in
        // the right context.
        URL pdfURL = new URL(createURL("xwiki/bin/export/Dashboard/WebHome?format=pdf"));
        try (PDFDocument document = new PDFDocument(pdfURL)) {
            String text = document.getText();
            // Note: This is the title of the Pages gadget when it's working
            assertTrue(text.contains("Pages"), "Invalid content");
            // Note: This is the title of the Pages gadget before XWIKI-7048 was fixed
            assertFalse(text.contains("$services.localization.render("), "Invalid content");
        }
    }

    /**
     * Verify that we can export content having links to attachments.
     *
     * @see "XWIKI-8978: PDF Export does not handle XWiki links to attached files properly"
     */
    @Test
    void exportContentWithAttachmentLink() throws Exception
    {
        URL pdfURL = new URL(createURL("xwiki/bin/export/Sandbox/WebHome?format=pdf"));
        try (PDFDocument document = new PDFDocument(pdfURL)) {
            Map<String, String> links = document.getLinks();
            assertTrue(links.containsKey("XWikiLogo.png"));
            assertEquals("http://localhost:8080/xwiki/bin/download/Sandbox/WebHome/XWikiLogo.png?rev=1.1",
                links.get("XWikiLogo.png"));

            // Ideally we should be asserting for a value of 1 (for the embedded XWikiLogo.png image) but it seems the
            // PDF contains 2 image objects (for some reason I don't understand ATM - they seem to be variations of the
            // same image - the logo - in color, in black and white, etc).
            assertEquals(2, document.getImages().size());
        }
    }

    /**
     * Verify the PDF export with table of contents.
     *
     * @see "XWIKI-9370: PDF Export doesn't list the Table of Contents under certain circumstances"
     */
    @Test
    void exportTableOfContents() throws Exception
    {
        URL pdfURL =
            new URL(createURL("xwiki/bin/export/Sandbox/WebHome?format=pdf&pdftoc=1&attachments=1&pdfcover=0"));
        try (PDFDocument document = new PDFDocument(pdfURL)) {
            Map<String, String> links = document.getLinksFromPage(0);
            // Make sure we have a Table of Contents.
            assertTrue(links.containsKey("Mixed list"));
            // Make sure the Table of Contents links point to their corresponding heading.
            for (Map.Entry<String, String> entry : links.entrySet()) {
                assertTrue(entry.getValue().contains(entry.getKey()));
            }
        }
    }

    private String createURL(String suffix)
    {
        return String.format("%s%s", this.prefix, suffix);
    }
}

@@ -1936,14 +1936,77 @@
 .get
-HostIP
+IP();%0A        int port = testConfiguration.getServletEngine().getPort
 ();%0A
@@ -2056,20 +2056,18 @@
 /%25s:
-8080
+%25s
 /%22, host
 );%0A 
@@ -2062,16 +2062,22 @@
 /%22, host
+, port
 );%0A    %7D
@@ -3934,31 +3934,25 @@
 als(
-%22http://localhost:8080/
+String.format(%22%25s
 xwik
@@ -4005,16 +4005,30 @@
 ev=1.1%22,
+ this.prefix),
 %0A       

Fix IT test (works for xwiki running inside and outside of docker). Note: it's possible it'll fail in DOOD cases (I cannot test locally ATM since the DOOD setup doesn't work on my mac m1). If this happens I'll fix it.