/*
    SPDX-FileCopyrightText: 2023 Volker Krause <vkrause@kde.org>
    SPDX-FileCopyrightText: 2026 Carl Schwan <carlschwan@kde.org>
    SPDX-License-Identifier: LGPL-2.0-or-later
*/

function extractPdf(pdf) {
    let res = JsonLd.newBusReservation();
    const text = pdf.text;
    const bookingId = text.match(/ID # ([^\d]*)(\d+)/);
    res.reservationNumber = bookingId[1] + bookingId[2];
    res.reservedTicket.ticketToken = 'qrCode:' + bookingId[2];
    res.reservationFor.departureBusStop.name = text.match(/From: *(\S.*?\S)  /)[1];
    res.reservationFor.arrivalBusStop.name = text.match(/To: *(\S.*?\S)  /)[1];
    const dt = text.match(/(\d+ \S{3} \d{4}) (\d\d:\d\d).*\n?.*Arrival .*(\d\d:\d\d)/);
    res.reservationFor.departureTime = JsonLd.toDateTime(dt[1] + dt[2], 'dd MMM yyyyhh:mm', 'en');
    res.reservationFor.arrivalTime = JsonLd.toDateTime(dt[1] + dt[3], 'dd MMM yyyyhh:mm', 'en');
    const loc = text.match(/(\d+\.\d+) ,?(\d+\.\d+)\n(.*)\n/);
    res.reservationFor.departureBusStop.geo.latitude = loc[1];
    res.reservationFor.departureBusStop.geo.longitude = loc[2];
    const addr = loc[3].split(',');
    res.reservationFor.departureBusStop.address.addressCountry = addr[addr.length - 1];
    res.reservationFor.departureBusStop.address.addressLocality = addr[addr.length - 2];
    res.reservationFor.departureBusStop.address.streetAddress = addr.slice(0, addr.length -2).join(',');
    const currency = text.match(/Passenger.*\s+([A-Z]{3})/);
    const price = text.match(/Total\s+([\d,]+)\n/);
    if (currency && price) {
        res.priceCurrency = currency[1];
        res.totalPrice = price[1].replace(',', '');
    }
    return res;
}

function fixSchemaOrg(html, node) {
    let results = node.result;
    for (res of results) {
        if (!res["potentialAction"])
            continue; // not a schema.org result
        // doesn't match what's in the PDF
        res.reservationNumber = undefined;
        if (res.reservationFor.departureTime == res.reservationFor.arrivalTime)
            res.reservationFor.arrivalTime = undefined;
        // contains server rather than location timezone
        res.reservationFor.departureTime = res.reservationFor.departureTime.substr(0, 19);
    }
    return results;
}

function extractReceipt(content) {
    let res = JsonLd.newBusReservation();


    const lines = content.split('\n');

    for (let i = 0, count = lines.length; i < count; i++) {
        const line = lines[i];

        if (line.startsWith('Name:')) {
            res.underName.name = line.split(': ')[1];
        }

        if (line.startsWith('Booking number:')) {
            res.reservationNumber = lines[i + 1];
        }

        if (line.startsWith('Route:')) {
            const match = line.match("Route: (.*)-(.*)");
            res.reservationFor.departureBusStop.name = match[1];
            res.reservationFor.arrivalBusStop.name = match[2];
        }

        if (line.startsWith('Departure:')) {
            const match = line.match("Departure: (.*), (.*)");
            res.reservationFor.departureTime = JsonLd.toDateTime(match[2] + ' ' + match[1], 'dd MMM yyyy hh:mm AP', 'en');
        }

        if (line.startsWith('Total amount:')) {
            const match = line.match("Total amount: (.*) (.*)");
            res.totalPrice = parseFloat(match[1]);
            res.priceCurrency = match[2];
        }
    }
    return res;
}
