<?php

class ModuleNetworkConfig extends Module
{
  public array $ConfigItems;

  function __construct(System $System)
  {
    parent::__construct($System);
    $this->Name = 'NetworkConfig';
    $this->Version = '1.0';
    $this->Creator = 'Chronos';
    $this->License = 'GNU/GPLv3';
    $this->Description = 'Network device remote configuration';
    $this->Dependencies = array(ModuleNetwork::GetName());
    $this->Models = array(NetworkConfigurationLog::GetClassName(), NetworkConfiguration::GetClassName());

    $this->ConfigItems = array();
  }

  function DoStart(): void
  {
    $this->System->FormManager->RegisterClass('NetworkConfiguration', array(
      'Title' => 'Restart síťových služeb',
      'Table' => 'NetworkConfiguration',
      'Items' => array(
        'Caption' => array('Type' => 'String', 'Caption' => 'Jméno', 'Default' => ''),
        'SysName' => array('Type' => 'String', 'Caption' => 'Systémové jméno', 'Default' => ''),
        'Changed' => array('Type' => 'TNetworkConfigurationState', 'Caption' => 'Stav', 'Default' => 0),
        'LastTime' => array('Type' => 'DateTime', 'Caption' => 'Naposledy spuštěno', 'ReadOnly' => true),
        'ExecutionTime' => array('Type' => 'Integer', 'Caption' => 'Doba běhu', 'Default' => '0', 'Suffix' => 'sekund', 'ReadOnly' => true),
        'Enabled' => array('Type' => 'Boolean', 'Caption' => 'Povoleno', 'Default' => '0'),
        'Period' => array('Type' => 'Integer', 'Caption' => 'Min. perioda', 'Default' => '60', 'Suffix' => 'sekund'),
      ),
      'ItemActions' => array(
        array('Caption' => 'Záznam', 'URL' => '/is/?a=view&t=NetworkConfigurationLog&i=#RowId'),
      ),
    ));
    $this->System->FormManager->RegisterClass('NetworkConfigurationLog', array(
      'Title' => 'Záznam restartu síťových služeb',
      'Table' => 'NetworkConfiguration',
      'Items' => array(
        'Caption' => array('Type' => 'String', 'Caption' => 'Jméno', 'Default' => ''),
        'Log' => array('Type' => 'Text', 'Caption' => 'Záznam', 'Default' => '', 'ReadOnly' => true),
      ),
    ));
    $this->System->FormManager->RegisterFormType('TNetworkConfigurationState', array(
      'Type' => 'Enumeration',
      'States' => array('Neplánováno', 'V plánu', 'Provádí se'),
    ));

    $this->System->RegisterCommandLine('config', 'Configures network services.', array($this, 'Config'));
    $this->System->Models['NetworkDevice']->RegisterOnChange('NetworkConfig', array($this, 'DoNetworkChange'));
    $this->System->Models['NetworkInterface']->RegisterOnChange('NetworkConfig', array($this, 'DoNetworkChange'));
  }

  function DoNetworkChange(): void
  {
    $this->Database->query('UPDATE `NetworkConfiguration` SET `Changed`=1 WHERE '.
      '(`Id`=1) OR (`Id`=7) OR (`Id`=8) OR (`Id`=9) OR (`Id`=10) OR (`Id`=11) OR (`Id`=12) OR (`Id`=13)');
  }

  function RegisterConfigItem(string $Name, string $ClassName): void
  {
    $this->ConfigItems[$Name] = $ClassName;
  }

  function UnregisterConfigItem(string $Name): void
  {
    unset($this->ConfigItems[$Name]);
  }

  function Config(array $Parameters): void
  {
    $Output = '';
    if ($Parameters >= 3)
    {
      $ConfigItemName = $Parameters[2];
      if (array_key_exists($ConfigItemName, $this->ConfigItems))
      {
        $ClassName = $this->ConfigItems[$ConfigItemName];
        $ConfigItem = new $ClassName($this->System);
        $ConfigItem->Run();
      } else $Output = 'Config item '.$ConfigItemName.' not found';
    } else $Output = 'Not enough parameters';
    echo($Output);
  }

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

class NetworkConfigItem extends Model
{
  function Run(): void
  {
  }
}

class NetworkConfiguration extends Model
{
  static function GetModelDesc(): ModelDesc
  {
    $Desc = new ModelDesc(self::GetClassName());
    $Desc->AddString('Caption');
    $Desc->AddString('SysName');
    $Desc->AddBoolean('Changed');
    $Desc->AddDateTime('LastTime');
    $Desc->AddInteger('ExecutionTime');
    $Desc->AddBoolean('Enabled');
    $Desc->AddInteger('Period');
    return $Desc;
  }
}

class NetworkConfigurationLog extends Model
{
  static function GetModelDesc(): ModelDesc
  {
    $Desc = new ModelDesc(self::GetClassName());
    $Desc->AddString('Caption');
    $Desc->AddText('Log');
    return $Desc;
  }
}