<?php

class PageMeteo extends Page
{
  function __construct(System $System)
  {
    parent::__construct($System);
    $this->Title = 'Meteostanice';
    $this->Description = 'Stav meteostanice';
    $this->ParentClass = 'PagePortal';
  }

  function Show(): string
  {
    $Output = 'Stav meteostanice:<br/>';
    $Output .= '<img src="'.$this->System->Link('/Modules/Meteostation/cache/1.png').'" alt="stav meteostanice"/>';
    return $Output;
  }
}

class MeteoStation extends Model
{
  public int $Id;
  public string $Name;
  public int $Period;
  public string $URL;

  function DownloadData(): void
  {
    $XmlData = simplexml_load_file($this->URL);

    $Data = array('MeteoStation' => $this->Id,
      'WindSpeed' => trim($XmlData->windspeed),
      'WindDir' => trim($XmlData->winddir),
      'WindGust' => trim($XmlData->windgust),
      'Pressure' => trim($XmlData->pressure),
      'SysTemp' => trim($XmlData->systemp),
      'Temperature' => trim($XmlData->temperature),
      'BarAltitude' => trim($XmlData->baraltitude),
      'WindChill' => trim($XmlData->windchill),
      'RelHumidity' => trim($XmlData->relhumidity),
      'AbsHumidity' => trim($XmlData->abshumidity),
      'DewPoint' => trim($XmlData->dewpoint)
    );
    $this->Database->insert('MeteoStationMeasure', array(
      'Time' => TimeToMysqlDateTime(time()), 'MeteoStation' => $Data['MeteoStation'],
      'WindSpeed' => $Data['WindSpeed'], 'WindDir' => $Data['WindDir'],
      'WindGust' => $Data['WindGust'], 'Pressure' => $Data['Pressure'],
      'SysTemp' => $Data['SysTemp'], 'Temperature' => $Data['Temperature'],
      'BarAltitude' => $Data['BarAltitude'], 'WindChill' => $Data['WindChill'],
      'RelHumidity' => $Data['RelHumidity'], 'AbsHumidity' => $Data['AbsHumidity'],
      'DewPoint' => $Data['DewPoint']));
    $this->Data = $Data;
  }

  function CreateImage(string $FileName): void
  {
    $Image = new Image();
    $Image->SetSize(150, 150);
    $Image->Brush->Color = COLOR_WHITE;
    $Image->FillRect(0, 0, $Image->GetWidth(), $Image->GetHeight());
    //$Image->Font->Color = COLOR_RED;
    //$Image->Line(10, 10, 100, 100);
    $Image->TextOut(10, 10, 'Meteo '.$this->Name);
    $Image->TextOut(10, 30, 'Teplote: '.$this->Data['Temperature'].' °C');
    $Image->SaveToFile($FileName);
  }

  function LoadFromDb(): void
  {
    $DbResult = $this->Database->select('Meteostation', '*', 'Id = '.$this->Id);
    $DbRow = $DbResult->fetch_assoc();
    $this->Name = $DbRow['Name'];
    $this->URL = $DbRow['URL'];
    $this->Period = $DbRow['Period'];
  }
}

class ModuleMeteoStation extends Module
{
  function __construct(System $System)
  {
    parent::__construct($System);
    $this->Name = 'MeteoStation';
    $this->Version = '1.0';
    $this->Creator = 'Chronos';
    $this->License = 'GNU/GPLv3';
    $this->Description = 'Gathering and presentation of data from network meteostation.';
  }

  function DownloadAll(): void
  {
    $DbResult = $this->Database->select('MeteoStation', '*');
    while ($DbRow = $DbResult->fetch_assoc())
    {
      $MeteoStation = new MeteoStation($this->System);
      $MeteoStation->Id = $DbRow['Id'];
      $MeteoStation->LoadFromDb();
      $MeteoStation->DownloadData();
      $MeteoStation->CreateImage('cache/'.$DbRow['Id'].'.png');
    }
  }

  function DoStart(): void
  {
    $this->System->RegisterPage(['meteo'], 'PageMeteo');
  }
}
