<?php
require_once "../../config/settings.inc.php";
require_once "../../include/iemprop.php";
$camera_refresh = get_iemprop("webcam.interval");
require_once "../../include/myview.php";
require_once "../../include/forms.php";
require_once "../../include/cameras.inc.php";
$t = new MyView();
/* CGI GET Requests */
$year = get_int404("year", date("Y"));
$month = get_int404("month", date("m"));
$day = get_int404("day", date("d"));
$hour = get_int404("hour", date("H"));
$minute = get_int404("minute", date("i"));
$isarchive = isset($_GET["archive"]);
$network = get_str404("network", "KCCI");

$ts = time();
if ($isarchive) {
    $radts = mktime($hour, $minute, 0, $month, $day, $year) - (($minute % 5) * 60);
} else {
    $radts = time();
}
$num = 1;
if ($network == "KCRG") {
    $cameras["KCCI-017"]["network"] = "KCRG";
}
foreach ($cameras as $id => $val) {
    if ($cameras[$id]["network"] != $network) continue;

    $cameras[$id]["url"] = sprintf("/data/camera/stills/%s.jpg?%s", $id, time());
    $camfn = "/mesonet/ldmdata/camera/stills/{$id}.jpg";
    if (!is_file($camfn) || filemtime($camfn) < (time() - 1200)) {
        $cameras[$id]["active"] = false;
    }

    if ($isarchive) {
        $cameras[$id]["active"] = true;
        $ts = mktime($hour, $minute, 0, $month, $day, $year);
        $ts5 = mktime($hour, $minute - ($minute % 5), 0, $month, $day, $year);

        $fp = "/mesonet/ARCHIVE/data/" . gmdate("Y/m/d/", $ts) . "camera/$id/{$id}_" . gmdate("YmdHi", $ts) . ".jpg";
        $url = "/archive/data/" . gmdate("Y/m/d/", $ts) . "camera/$id/{$id}_" . gmdate("YmdHi", $ts) . ".jpg";
        $fp5 = "/mesonet/ARCHIVE/data/" . gmdate("Y/m/d/", $ts5) . "camera/$id/{$id}_" . gmdate("YmdHi", $ts5) . ".jpg";
        $url5 = "/archive/data/" . gmdate("Y/m/d/", $ts5) . "camera/$id/{$id}_" . gmdate("YmdHi", $ts5) . ".jpg";

        if (is_file($fp)) $cameras[$id]["url"] = $url;
        else if (is_file($fp5)) $cameras[$id]["url"] = $url5;
        else $cameras[$id]["active"] = false;
    }

    if ($cameras[$id]["active"]) {
        $cameras[$id]["num"] = intval(substr($id, 5, 3));
    }
}/* End of while */

reset($cameras);

$t->title = "Web Cameras";
$t->jsextra = '<script type="module" src="camera.module.js"></script>';

if (!$isarchive && isset($camera_refresh))
    $t->refresh = $camera_refresh;

$selback = array(
    "KELO" => "#fff",
    "KCCI" => "#fff",
    "KCRG" => "#fff",
    "MCFC" => "#fff"
);
$selback[$network] = "#ffcc99";

$isarchived = ($isarchive) ? " CHECKED=CHECKED" : "";
$ys = yearSelect(2003, $year, "year");
$month_select = monthSelect($month, "month");
$ds = daySelect($day, "day");
$hs = hourSelect($hour, "hour");
$ms = minuteSelect($minute, "minute", 1);
$dd = date("YmdHi", $radts);

$misstxt = "Cameras Missing: ";
$control = "";
foreach ($cameras as $id => $v) {
    if ($v["network"] != $network) {
        continue;
    }
    if (!$v["active"]) {
        $misstxt .= $v["name"] . " , ";
        continue;
    }
    $control .= sprintf("<div style=\"float: left; margin-left: 5px;\"><b>%s. %s, %s</b> (%s County)<br /><img src=\"%s\"></div>", $v["num"], $v["name"], $v["state"], $v["county"], $v["url"]);
}

$t->content = <<<EOM

<div class="alert alert-danger">This legacy app will be removed at some point
in the future.  Please use the <a href="/current/webcam.php">newer version</a>.</div>

<strong>Webcam Networks</strong>
<div style="width:775px; border: 1px dashed #ccc;">
<div style="float: left; width: 250px; text-align: center; border: 3px #eee solid; background: {$selback["KCCI"]}">
  <a href="camera.phtml?network=KCCI">KCCI-TV<br />Des Moines, IA</a>
</div>
<div style="float: left; width: 250px; text-align: center; border: 3px #eee solid; background: {$selback["KCRG"]}">
  <a href="camera.phtml?network=KCRG">KCRG-TV<br />Cedar Rapids, IA</a>
</div>
<div style="float: left; width: 250px; text-align: center; border: 3px #eee solid; background: {$selback["KELO"]}">
  <a href="camera.phtml?network=KELO">KELO-TV<br />Sioux Falls, SD</a>
</div>
<br clear="all" />
</div>

<form method="GET" action="camera.phtml">
<input type="hidden" value="{$network}" name="network">
<table><caption>Time Settings:</caption>
<thead><tr><th>&nbsp;</th><th>Year:</th><th>Month:</th><th>Day:</th><th>Hour:</th><th>Minute</th><td></td></tr></thead>
<tbody>
<tr>
<td>
<input type="checkbox" value="yes" id="isarchived" name="archive" {$isarchived}>
<label for="isarchived">Archived Images</label>
</td>
<td>{$ys}</td>
<td>{$month_select}</td>
<td>{$ds}</td>
<td>{$hs}</td>
<td>{$ms}</td>
<td><input type="submit" value="GO!"></td>
</tr>
</tbody></table>
</form>

<div style="float: left; margin-left: 5px;"><b>Radar View</b><br />
<img src="camrad.php?network={$network}&ts={$dd}"></div>

{$control}
{$misstxt}

<br style="clear: both;">

<p><b>Cool Shots!</b>
<ul>
 <li><a href="camera.phtml?archive=yes&year=2004&month=6&day=11&hour=19&minute=32">11 Jun 2004 - 7:32 PM, Webster City Tornado</a></li>
 <li><a href="camera.phtml?archive=yes&year=2005&month=5&day=26&hour=19&minute=15">26 May 2005 - 7:15 PM, Pella Double Rainbow</a></li>
 <li><a href="camera.phtml?archive=yes&year=2005&month=6&day=8&hour=20&minute=55">8 Jun 2005 - 8:55 PM, All sorts of colours</a></li>
 <li><a href="camera.phtml?archive=yes&year=2005&month=9&day=8&hour=12&minute=30">8 Sep 2005 - 12:30 PM, Blurry shot of Ames Tornado</a></li>
 <li><a href="camera.phtml?archive=yes&year=2005&month=11&day=12&hour=16&minute=38">12 Nov 2005 - 4:38 PM, Woodward tornado from Madrid</a></li>
 <li><a href="camera.phtml?archive=yes&year=2005&month=11&day=12&hour=17&minute=00">12 Nov 2005 - 5:00 PM, Ames tornado</a></li>
 <li><a href="camera.phtml?archive=yes&year=2006&month=07&day=17&hour=16&minute=50">17 Jul 2006 - 4:50 PM, Tama possible brief tornado</a></li>
 <li><a href="camera.phtml?archive=yes&year=2007&month=10&day=2&hour=17&minute=56">2 Oct 2007 - 5:56 PM, Twin Cedars possible tornado</a></li>
 <li><a href="camera.phtml?archive=yes&year=2008&month=8&day=30&hour=20&minute=20">30 Aug 2008 - 8:20 PM, Interesting Sunset Halos</a></li>
</ul>
EOM;
$t->render('single.phtml');
