package com.alexanderlogistics.rest;

import java.util.logging.Logger;

import org.imixs.melman.CookieAuthenticator;
import org.imixs.melman.DocumentClient;
import org.imixs.workflow.exceptions.PluginException;
import org.junit.Before;
import org.junit.Test;

import com.oracle.truffle.api.library.ExportMessage.Ignore;

import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.WebTarget;
import jakarta.ws.rs.core.Cookie;
import jakarta.ws.rs.core.Response;

/**
 * Test to request data via a JSESSIONID only....
 * This technique is used by the DataRestService
 * 
 */
@Ignore
public class TestDataClient {

    private static Logger logger = Logger.getLogger(TestDataClient.class.getName());

    DocumentClient client = null;
    Client rsClient = null;

    String restAPI = "https://alexander-logistics.office-workflow.de/api";
    String sessionID = "rMxsrGJwT2GOisKrjEcaI5kpny_DnWieEPbIUvH2.imixs-office-workflow-8c9795689-d62t5";
    String reportName = "Steuerbescheide";

    @Before
    public void setup() throws PluginException {
        // Create a Cookie object with a name and a value
        Cookie cookie = new Cookie.Builder("JSESSIONID")
                .value(sessionID)
                .path("/")
                .domain("alexander-logistics.office-workflow.de")
                .build();

        client = new DocumentClient(restAPI.trim());
        CookieAuthenticator aut = new CookieAuthenticator(cookie);
        client.registerClientRequestFilter(aut);
        rsClient = client.newClient();
        rsClient.register(aut);
    }

    /**
     * Test to read data....
     * 
     */
    @Test
    public void testGetReportData() {

        // Test access
        String url = client.getBaseURI() + "documents/";
        logger.info("send request " + url);
        WebTarget target = rsClient.target(url);
        // Send a GET request and get the response
        Response response = target.request().get();

        // Check the response status and process the response
        if (response.getStatus() == 200) {
            String result = response.readEntity(String.class);
            if (result.contains("j_password")) {
                System.out.println("login failed");
            }
            System.out.println("Response: " + result);
        } else {
            System.out.println("Request failed with status code: " + response.getStatus());
        }

        // Specify the target URL
        url = client.getBaseURI() + "report/Steuerbescheide.html?pageSize=100"; // + reportName;
        logger.info("send request " + url);
        target = rsClient.target(url);
        // Send a GET request and get the response
        response = target.request().get();

        // Check the response status and process the response
        if (response.getStatus() == 200) {
            String result = response.readEntity(String.class);
            System.out.println("Response: " + result);
        } else {
            System.out.println("Request failed with status code: " + response.getStatus());
        }

    }

}
