<?php
// https://weichie.com/blog/curl-api-calls-with-php/
function callAPI($method, $url, $data)
{
    $curl = curl_init();
    switch ($method) {
        // case "POST":
        // curl_setopt($curl, CURLOPT_POST, 1);
        // if ($data)
        // curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        // break;
        // case "PUT":
        // curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
        // if ($data)
        // curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        // break;
        
        default:
            if ($data)
                $url = sprintf("%s?%s", $url, http_build_query($data));
    }
    // OPTIONS:
    $aryApiKey = file('../api.key');
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_HTTPHEADER, array(
        'Authorization: bearer ' . $aryApiKey[0],
        'Accept: application/json'
    ));
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    // EXECUTE:
    $result = curl_exec($curl);
    if (!$result) {
        die("Connection Failure");
    }
    curl_close($curl);
    return $result;
}

$strHTML = "";

$get_data = callAPI('GET', 'https://connectapi.feuersoftware.com/interfaces/public/operation', false);
$response = json_decode($get_data, true);
if (!$response) {
    $strHTML .= "Aktuell sind keine Eins&auml;tze aktiv.";
} else {
    
    for ($i = 0; $i < count($response); $i++) {
        $date1    = new \DateTime("now");
        $date2    = new \DateTime($response[$i]['Start']);
        $interval = date_diff($date2, $date1);
        
        $minutes = $interval->format('%D') * 24 * 60;
        $minutes += $interval->format('%h') * 60;
        $minutes += $interval->format('%i');
        
        if ($minutes <= 60) {
            preg_match('/^(\w*)\s?(\d)$/', $response[$i]['Keyword'], $aryMatches);
            if (!$aryMatches) {
                // finde anderes muster
                switch (true) {
                    case (preg_match('/PROBEALARM/i', $response[$i]['Keyword']) ? true : false):
                        $strHTML .= '<b>' . $date2->format('d.m.Y H:i') . '</b><br>';
                        $strHTML .= '<span style="color:#003399;">🛠️ Techniktest der Alarmierung</span><p>';
                        break;
                    case (preg_match('/THL.*Unwetter/i', $response[$i]['Keyword']) ? true : false):
                        $strHTML .= '<b>' . $date2->format('d.m.Y H:i') . '</b><br>';
                        $strHTML .= '<span style="color:#003399;">⛈️ Technische Hilfeleistung: Unwetter</span><p>';
                        break;
                    case (preg_match('/THL.?P.?EINGESCHLOSSEN/i', $response[$i]['Keyword']) ? true : false):
                        $strHTML .= '<b>' . $date2->format('d.m.Y H:i') . '</b><br>';
                        $strHTML .= '<span style="color:#003399;">🔑 Technische Hilfeleistung: T&uuml;r&ouml;ffnung</span><p>';
                        break;
                    case (preg_match('/THL.?P.?RETTUNG/i', $response[$i]['Keyword']) ? true : false):
                        $strHTML .= '<b>' . $date2->format('d.m.Y H:i') . '</b><br>';
                        $strHTML .= '<span style="color:#003399;">⛑️Technische Hilfeleistung: Personenrettung</span><p>';
                        break;
                    case (preg_match('/THL.?FIRST RESPONDER/i', $response[$i]['Keyword']) ? true : false):
                        $strHTML .= '<b>' . $date2->format('d.m.Y H:i') . '</b><br>';
                        $strHTML .= '<span style="color:#003399;">🆘 Technische Hilfeleistung: Medizinischer Notfall</span><p>';
                        break;
                    case (preg_match('/THL.?TRAGEHILFE/i', $response[$i]['Keyword']) ? true : false):
                        $strHTML .= '<b>' . $date2->format('d.m.Y H:i') . '</b><br>';
                        $strHTML .= '<span style="color:#003399;">🆘 Technische Hilfeleistung: Medizinischer Notfall - Tragehilfe</span><p>';
                        break;
                    //case (preg_match('/PHL/i', $response[$i]['Keyword']) ? true : false):
                        //$strHTML .= '<b>' . $date2->format('d.m.Y H:i') . '</b><br>';
                        //$strHTML .= '<span style="color:#003399;">🆘 Technische Hilfeleistung: Medizinischer Notfall</span><p>';
                        
                        break;
                    case (preg_match('/B\s?(\d)\s?PERSON/i', $response[$i]['Keyword'], $aryTemp) ? true : false):
                        $strHTML .= '<b>' . $date2->format('d.m.Y H:i') . '</b><br>';
                        $strHTML .= '<span style="color:#D63B37;">🔥 Brandeinsatz der Stufe: ' . $aryTemp[1] . '. Person in Gefahr</span><p>';
                        break;
                    default:
                        $strHTML .= '<b>' . $date2->format('d.m.Y H:i') . '</b><br>';
                        $strHTML .= "❔ Für diesen Einsatz liegen keine weitere Informationen vor.<p>";
                        break;
                }
            } else {
                switch ($aryMatches[1]) {
                    case 'B':
                        $strHTML .= '<b>' . $date2->format('d.m.Y H:i') . '</b><br>';
                        $strHTML .= '<span style="color:#D63B37;">🔥 Brandeinsatz der Stufe: ' . $aryMatches[2] . '</span><p>';
                        break;
                    case 'THL':
                        $strHTML .= '<b>' . $date2->format('d.m.Y H:i') . '</b><br>';
                        $strHTML .= '<span style="color:#003399;">🛠️ Technische Hilfeleistung der Stufe: ' . $aryMatches[2] . '</span><p>';
                        break;
                    case 'VU':
                        $strHTML .= '<b>' . $date2->format('d.m.Y H:i') . '</b><br>';
                        $strHTML .= '<span style="color:#ff9900;">🛑 Verkehrsunfall der Stufe: ' . $aryMatches[2] . '</span><p>';
                        break;
                    default:
                        $strHTML .= '<b>' . $date2->format('d.m.Y H:i') . '</b><br>';
                        $strHTML .= '<span style="color:#D63B37;">❔ Nicht definierter Einsatz der Stufe: ' . $aryMatches[2] . '</span><p>';
                        break;
                }
            }
            
        }
    }
    if ($strHTML == "") {
        $strHTML .= "Aktuell sind keine Eins&auml;tze aktiv.";
    }
}

echo $strHTML;
?>