<?php

class PageWebcam extends Page
{
  function __construct(System $System)
  {
    parent::__construct($System);
    $this->Title = 'Kamera';
    $this->Description = 'Webová kamera';
    $this->ParentClass = 'PagePortal';
  }

  function Show(): string
  {
    if (file_exists(ModuleWebCam::Cast($this->System->GetModule('WebCam'))->ImageFileName))
    {
      $Output = '<script language="JavaScript">
      var ImageURL = "'.$this->System->Config['Web']['RootFolder'].'/webcam/'.ModuleWebCam::Cast($this->System->GetModule('WebCam'))->ImageFileName.'";

      // Force an immediate image load
      var theTimer = setTimeout("reloadImage()", 1);

      function reloadImage()
      {
        theDate = new Date();
        var url = ImageURL;
        url += "?dummy=";
        url += theDate.getTime().toString(10);
        // The above dummy cgi-parameter enforce a bypass of the browser image cache.
        // Here we actually load the image
        document.theImage.src = url;

        // Reload the image every defined period
        theTimer = setTimeout("reloadImage()", '.($this->System->Config['Web']['WebcamRefresh'] * 1000).');
      }
      </script>';

      $Output .= '<br /><div align="center"><img name="theImage" src="" idth="640" height="480" alt="Webcam image"><br>Poslední aktualizace: '.
        date('j.n.Y G:i', filemtime(ModuleWebCam::Cast($this->System->GetModule('WebCam'))->ImageFileName)).
        '<br>Obnovování po '.$this->System->Config['Web']['WebcamRefresh'].' sekundách</div><br />';
    } else $Output = '<br />Obrázek nenalezen.<br /><br />';

    $Output .= '<strong>Kamery v okolí:</strong><br />'.
      '<a href="http://www.mestovsetin.cz/vismo/dokumenty2.asp?id_org=18676&id=480245">Webové kamery ve Vsetíně</a><br />';

    return $Output;
  }
}

class ModuleWebCam extends Module
{
  public string $ImageFileName = 'webcam.jpg';

  function __construct(System $System)
  {
    parent::__construct($System);
    $this->Name = 'WebCam';
    $this->Version = '1.0';
    $this->Creator = 'Chronos';
    $this->License = 'GNU/GPLv3';
    $this->Description = 'Web camera image presentetation';
  }

  function DoStart(): void
  {
    $this->System->Pages['webcam'] = 'PageWebcam';
  }

 function ShowImage(): string
  {
    $Output = '';
    $Output .= '<a href="//www.zdechov.net/kamery/?Id=5"><img alt="Koupaliště" width="140" height="79" src="//www.zdechov.net/images/webcam/webcam5.jpg" /></a>';
    $Output .= '<a href="//www.zdechov.net/kamery/?Id=1"><img alt="Centrum obce" width="140" height="79" src="//www.zdechov.net/images/webcam/webcam1.jpg" /></a>';
    $Output .= '<a href="//www.zdechov.net/kamery/?Id=2"><img alt="Střed obce obloha" width="140" height="79" src="//www.zdechov.net/images/webcam/webcam2.jpg" /></a>';
    $Output .= '<a href="//www.zdechov.net/kamery/?Id=3"><img alt="Skiareál, motokrosová grapa" width="140" height="79" src="//www.zdechov.net/images/webcam/webcam3.jpg" /></a>';
    $Output .= '<a href="//www.zdechov.net/kamery/?Id=4"><img alt="Fotbalové hřiště" width="140" height="79" src="//www.zdechov.net/images/webcam/webcam4.jpg" /></a>';
    return $Output;
  }

  static function Cast(Module $Module): ModuleWebCam
  {
    if ($Module instanceof ModuleWebCam)
    {
      return $Module;
    }
    throw new Exception('Expected ModuleWebCam type but got '.gettype($Module));
  }
}
