<?php declare(strict_types=1);

require_once __DIR__ . '/src/QrbApi.php';

// Target for POST with action "getProcessStatus" in QrBillsProcess.js.
//
// This code will respond with HTTP code 200 (ok), as long as there is more text to read.
// Answers with HTTP code 400 (bad request)  if the received request was not for 'getProcessStatus'.
// Answers with HTTP code 404 (not found)    if there is not yet any text (but probably will be).
// Answers with HTTP code 410 (gone)         if there is no more text (processing finished).
// Answers with HTTP code 500 (server error) if some unforeseen exception occurred (processing aborted).

const HTTP_CODE_BAD_REQUEST         = 400;
const HTTP_CODE_NOT_FOUND           = 404;
const HTTP_CODE_GONE                = 410;
const HTTP_CODE_SERVER_ERROR        = 500;

if (!array_key_exists('getProcessStatus', $_POST))
{
    http_response_code(HTTP_CODE_BAD_REQUEST);
    exit;
}

$qrbStatusTracker = QrbApi::createProcessStatusTracker();
$result = $qrbStatusTracker->trackStatus();

switch ($result)
{
    case QrbProcessStatusTracker::GOOD: 
        print $qrbStatusTracker->getStatusText();
        break;  // results in HTTP code 200: There's more to read.
        
    case QrbProcessStatusTracker::NOT_FOUND;
        http_response_code(HTTP_CODE_NOT_FOUND);
        exit;
        
    case QrbProcessStatusTracker::EOF:
        http_response_code(HTTP_CODE_GONE);
        exit;
        
    case QrbProcessStatusTracker::ERROR:
    default:
        http_response_code(HTTP_CODE_SERVER_ERROR);
        exit;
}

?>
