<?php

define('CONTACT_CATEGORY_EMAIL', 4);

class ModuleNotify extends Module
{
  public array $Checks;

  function __construct(System $System)
  {
    parent::__construct($System);
    $this->Name = 'Notify';
    $this->Version = '1.0';
    $this->Creator = 'Chronos';
    $this->License = 'GNU/GPLv3';
    $this->Description = 'Send notification messages to selected users';
    $this->Dependencies = array(ModuleUser::GetName(), ModuleRSS::GetName());
    $this->Models = array(NotifyCategory::GetClassName(), NotifyUser::GetClassName());

    $this->Checks = array();
  }

  function DoStart(): void
  {
    $this->System->FormManager->RegisterClass('NotifyUser', array(
      'Title' => 'Upozornění uživatelé',
      'Table' => 'NotifyUser',
      'Items' => array(
        'User' => array('Type' => 'TUser', 'Caption' => 'Uživatel', 'Default' => ''),
        'Contact' => array('Type' => 'TContact', 'Caption' => 'Kontakt', 'Default' => ''),
        'Period' => array('Type' => 'Integer', 'Caption' => 'Interval', 'Default' => '60'),
      ),
    ));
    $this->System->FormManager->RegisterClass('NotifyCategory', array(
      'Title' => 'Kategorie upozornění',
      'Table' => 'NotifyCategory',
      'Items' => array(
        'Name' => array('Type' => 'String', 'Caption' => 'Jméno', 'Default' => ''),
        'SysName' => array('Type' => 'String', 'Caption' => 'Systémové jméno', 'Default' => ''),
      ),
    ));
    $this->System->RegisterPage(['notify'], 'PageNotify');
    $this->System->RegisterCommandLine('notify', 'Perform notifications processing.', array($this, 'RunCheck'));
    ModuleRSS::Cast($this->System->GetModule('RSS'))->RegisterRSS(array('Title' => 'Notify log',
      'Channel' => 'notifylog', 'Callback' => array($this, 'ShowLogRSS'),
      'Permission' => array('Module' => 'Notify', 'Operation' => 'RSS')));
  }

  function RegisterCheck(string $Name, callable $Callback): void
  {
    if (array_key_exists($Name, $this->Checks))
      throw new Exception('Check function "'.$Name.'" already registered.');
    $this->Checks[$Name] = array('Callback' => $Callback);
  }

  function UnregisterCheck(string $Name): void
  {
    if (!array_key_exists($Name, $this->Checks))
      throw new Exception('Check function "'.$Name.'" not registered.');
    unset($this->Checks[$Name]);
  }

  function Check(): string
  {
    $Output = '';
    $Content = '';
    $Title = '';

    // Get output from checks
    $DbResult2 = $this->Database->query('SELECT `Id`, `Name`, `SysName` FROM `NotifyCategory`');
    while ($Category = $DbResult2->fetch_assoc())
    {
      if (array_key_exists($Category['SysName'], $this->Checks))
      {
        $Status = call_user_func($this->Checks[$Category['SysName']]['Callback']);
        if ($Status['Report'] != '')
        {
          $Output .= 'Kategorie: '.$Category['Name'].":\n";
          $Content .= 'Kategorie: '.$Category['Name']."<br>\n";
          $Content .= $Status['Report'];
        }
        if (strlen($Title) > 0) $Title .= ', ';
        $Title .= $Status['Title'].':'.$Status['Count'];
      }
    }

    $Time = time();

    // Send content to users
    $DbResult = $this->Database->query('SELECT `NotifyUser`.`Id`, `NotifyUser`.`LastTime`, `User`.`Name`, `Contact`.`Value`, `Contact`.`Category` FROM `NotifyUser` '.
      'LEFT JOIN `User` ON `User`.`Id` = `NotifyUser`.`User` '.
      'LEFT JOIN `Contact` ON `Contact`.`Id` = `NotifyUser`.`Contact`');
    while ($User = $DbResult->fetch_assoc())
    {
      $Output .= 'User '.$User['Name'].'<br/>';

      $this->Database->update('NotifyUser', '`Id`='.$User['Id'], array('LastTime' => TimeToMysqlDateTime($Time)));

      if (($User['Category'] == CONTACT_CATEGORY_EMAIL) and ($Content != ''))
      {
        $Mail = new Mail();
        $Mail->Subject = $Title;
        $Mail->From = $this->System->Config['Web']['Title'].' <noreplay@zdechov.net>';
        $Mail->AddTo($User['Value'], $User['Name']);
        $Mail->AddBody(strip_tags($Content), 'text/plain');
        $Mail->AddBody('<style>
table { border-collapse: collapse; }
table, th, td { border: 1px solid black; }
td { padding: 5px; }
        </style>'.$Content, 'text/html');
        $Mail->Send();
        $Output .= 'Sent email to '.$User['Value'].' ('.$User['Name'].')<br/>';
        $Output .= strip_tags(str_replace("</", " </", str_replace('<br/>', "\n", $Content)));
      }
    }

    if ($Content != '')
    {
      $this->Database->insert('NotifyLog', array(
        'Time' => TimeToMysqlDateTime($Time),
        'Title' => $Title,
        'Content' => $Content)
      );
    }

    return $Output;
  }  

  function RunCheck(array $Parameters): void
  {
    RepeatFunction(30, array($this, 'Check'));
  }

  function DoInstall(): void
  {
    $this->Database->query("INSERT INTO `NotifyCategory` (`Id`, `Name`, `SysName`) VALUES
    (1, 'Dostupnost zařízení (ping)', 'NetworkReachability'),
    (2, 'Dostupnost URL', 'URL'),
    (3, 'Minimální úroveň signálu', 'WirelessSignal'),
    (4, 'Dostupnost síťového portu', 'NetworkPort'),
    (5, 'Minimální odezva', 'NetworkLatency'),
    (6, 'Minimální propustnost', 'NetworkBandwidth');");
  }

  function ShowLogRSS(): string
  {
    Header('Content-Type: text/xml');
    $Count = 20;

    $Items = array();
    $sql = 'SELECT `Id`,`Title`,`Content`, UNIX_TIMESTAMP(`Time`) AS `Time` FROM `NotifyLog`'.
      ' ORDER BY `Time` DESC LIMIT '.$Count;
    $DbResult = $this->System->Database->query($sql);
    while ($Line = $DbResult->fetch_assoc())
    {
      $Items[] = array
      (
        'Title' => $Line['Title'],
        'Link' => 'https://'.$this->System->Config['Web']['Host'].$this->System->Link('/notify/?i='.$Line['Id']),
        'Description' => $Line['Content'],
        'Time' => $Line['Time'],
      );
    }

    $RSS = new RSS();
    $RSS->Title = $this->System->Config['Web']['Title'].' - Záznamy upozornění';
    $RSS->Link = 'https://'.$this->System->Config['Web']['Host'].'/';
    $RSS->Description = 'Záznam upozornění '.$this->System->Config['Web']['Description'];
    $RSS->WebmasterEmail = $this->System->Config['Web']['AdminEmail'];
    $RSS->Items = $Items;
    return $RSS->Generate();
  }

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

class PageNotify extends Page
{
  function __construct(System $System)
  {
    parent::__construct($System);
    $this->Title = 'Upozornění';
    $this->Description = 'Upozornění';
    $this->ParentClass = 'PagePortal';
  }

  function Show(): string
  {
    if (!ModuleUser::Cast($this->System->GetModule('User'))->User->CheckPermission('Notify', 'Show'))
      return 'Nemáte oprávnění';

    $Output = '<style>
    table { border-collapse: collapse; }
    table, th, td { border: 1px solid gray; }
    td { padding: 5px; }
    </style>';
    if (!array_key_exists('i', $_GET) or !is_numeric($_GET['i'])) return 'Položka nenalezena';    
    $Id = $_GET['i'] * 1;
    $DbResult = $this->Database->select('NotifyLog', 'Title,Content, UNIX_TIMESTAMP(`Time`) AS `Time`', 'Id='.$Id);
    if ($DbResult->num_rows > 0)
    {
      $DbRow = $DbResult->fetch_assoc();
      $Output .= '<h3>'.$DbRow['Title'].'</h3>'.
        '<div>Čas: '.TimeToMysqlDateTime($DbRow['Time']).'</div>'.
        '<p>'.$DbRow['Content'].'</p>';
    } else $Output = 'Položka nenalezena';
    return $Output;
  }
}

class NotifyCategory extends Model
{
  static function GetModelDesc(): ModelDesc
  {
    $Desc = new ModelDesc(self::GetClassName());
    $Desc->AddString('Name');
    $Desc->AddString('SysName');
    return $Desc;
  }
}

class NotifyUser extends Model
{
  static function GetModelDesc(): ModelDesc
  {
    $Desc = new ModelDesc(self::GetClassName());
    $Desc->AddReference('User', User::GetClassName());
    $Desc->AddReference('Contact', Contact::GetClassName());
    $Desc->AddInteger('Period');
    return $Desc;
  }
}
