<?php
require_once __DIR__ . '/common.php';

$dbconn = getDBConnection();

//cams endpoint
if (isset($_GET['cams'])) {
    if($_GET['lat1']) {
        $lat1 = getParam('lat1');
        if($_GET['lon1']) {
            $lon1 = getParam('lon1');
            if($_GET['lat2']) {
                $lat2 = getParam('lat2');
                if($_GET['lon2']) {
                    $lon2 = getParam('lon2');
                    if($_GET['elevbottom']) {
                        $elevbottom = getParam('elevbottom');
                        if($_GET['elevtop']) {
                            $elevtop = getParam('elevtop');

                            $result = pg_query_params($dbconn,
                            "select camid,url,description from cams where method = 'rtsp' and active = true and cwa = 'RLX' and elevation > $5 and elevation < $6 and (EXTRACT(EPOCH FROM (current_timestamp - lastsuccess ))/60) < (interval + 20) and lat < $1 and lat > $2 and lon < $3 and lon > $4 order by elevation desc",
                            array($lat1,$lat2,$lon1,$lon2,$elevbottom,$elevtop)) or die('Query failed: ' . pg_last_error());

                            while ($line = pg_fetch_array($result, null, PGSQL_ASSOC)) {
                                $array[] = $line;
                            }
                            echo json_encode($array);
                            pg_free_result($result);
                        }
                    }
                }
            }
        }
    }
}

// camstatic endpoint
if (isset($_GET['camstatic'])) {
    if($_GET['lat1']) {
        $lat1 = getParam('lat1');
        if($_GET['lon1']) {
            $lon1 = getParam('lon1');
            if($_GET['radius']) {
                $radius = getParam('radius');
                $rad = $radius / 70;

                $lat1 = floatval($lat1);
                $lon1 = floatval($lon1);
                $radius = floatval($rad);
                $query = "select * from cams where method = 'rtsp' and active = true and cwa = 'RLX' and (EXTRACT(EPOCH FROM (current_timestamp - lastsuccess ))/60) < (interval + 20) and st_dwithin(geom, ST_SetSRID(ST_Point(" . strval($lon1) . ", " . strval($lat1) . "), 4326)," . strval($radius) . ") order by elevation desc";

                $result = pg_query($dbconn,$query) or die('Query failed: ' . pg_last_error());

                while ($line = pg_fetch_array($result, null, PGSQL_ASSOC)) {
                    $array[] = $line;
                }
                echo json_encode($array);
                pg_free_result($result);
            }
        }
    }
}

// camdb endpoint
if (isset($_GET['camdb'])) {
    $result = pg_query($dbconn,
    "SELECT COUNT(*) FROM camdb") or die('Query failed: ' . pg_last_error());
    while ($line = pg_fetch_array($result, null, PGSQL_ASSOC)) {
        $array[] = $line;
    }
    echo json_encode($array);
    pg_free_result($result);
}

// Close database connection when needed
if (isset($dbconn)) {
    pg_close($dbconn);
}
?>