<?php

class ModuleUser extends Module
{
  public User $User;

  function __construct(System $System)
  {
    parent::__construct($System);
    $this->Name = 'User';
    $this->Version = '1.1';
    $this->Creator = 'Chronos';
    $this->License = 'GNU/GPL';
    $this->Description = 'User and permission management';
    $this->Dependencies = array();

    $this->User = new User($this->System);
  }

  function DoStart(): void
  {    
    $this->System->RegisterPage(['users'], 'PageUserList');
    $this->System->RegisterPage(['options'], 'PageUserOptions');
    $this->System->RegisterPage(['registration'], 'PageUserRegistration');
    $this->System->RegisterPage(['user'], 'PageUserProfile');
    $this->System->RegisterPage(['login'], 'PageUserLogin');
    Core::Cast($this->System)->RegisterMenuItem(array(
      'Title' => T('Translators'),
      'Hint' => 'Seznam registrovaných uživatelů',
      'Link' => $this->System->Link('/users/'),
      'Permission' => LICENCE_ANONYMOUS,
      'Icon' => '',
    ), 0);
    if (array_key_exists('Search', $this->System->ModuleManager->Modules))
      $this->System->ModuleManager->Modules['Search']->RegisterSearch('user',
      T('Translators'), array('Name'), '`User`', $this->System->Link('/users/?search='));
      Core::Cast($this->System)->RegisterPageBarItem('Top', 'User', array($this, 'TopBarCallback'));
      Core::Cast($this->System)->RegisterPageBarItem('Left', 'User', array($this, 'ShowOnlineList'));
  }

  function ShowOnlineList()
  {
    $Output = T('Online translators').':<br />';
    $DbResult = $this->System->Database->query('SELECT * FROM ('.
      'SELECT `User`.`Name`, `User`.`ID` FROM `UserOnline` '.
      'JOIN `User` ON `User`.`ID` = `UserOnline`.`User` '.
      'WHERE (`ActivityTime` >= NOW() - 300) '.
      'ORDER BY `ActivityTime` DESC ) AS `T` GROUP BY `Name`');
    while ($DbUser = $DbResult->fetch_assoc())
    {
      $Name = '<a href="'.$this->System->Link('/user/?user='.$DbUser['ID']).'">'.$DbUser['Name'].'</a>';
      $Output .= $Name.'<br />';
    }
    return $Output;
  }

  function TopBarCallback()
  {
    $Output = '';
    $User = ModuleUser::Cast($this->System->GetModule('User'))->User;
    if ($User->Licence(LICENCE_USER))
    {
      //$DbResult =$this->Database->query('SELECT `Id`, `Name` FROM `Team` WHERE `Id`='.$this->System->User->Team);
      //$Team = $DbResult->fetch_assoc();
      //$Output .= ''<span class="MenuItem">Moje překlady: <a href="">Dokončené</a> <a href="">Rozpracované</a> <a href="">Exporty</a> Tým: <a href="">'.$Team['name'].'</a></span>';
      $Output .= $User->Name.' <a href="'.$this->System->Link('/?action=logout').'">'.T('Logout').'</a>'.
          ' <a href="'.$this->System->Link('/user/?user='.$User->Id).'">'.T('My page').'</a>'.
          ' <a href="'.$this->System->Link('/options/').'">'.T('Options').'</a>'.
          ' <a title="Vámi přeložené texty" href="'.$this->System->Link('/TranslationList.php?user='.
          $User->Id.'&amp;group=0&amp;state=2&amp;text=&amp;entry=').'">'.T('Translated').'</a>'.
          ' <a title="Vaše rozpracované text" href="'.$this->System->Link('/TranslationList.php?user='.
          $User->Id.'&amp;group=0&amp;state=3&amp;text=&amp;entry=').'">'.T('Unfinished').'</a>'.
          ' <a title="Nikým nepřeložené texty" href="'.
          $this->System->Link('/TranslationList.php?user=0&amp;group=0&amp;state=1&amp;text=&amp;entry=').'">'.T('Untranslated').'</a>';
    } else
    {
      $Output .= '<a href="'.$this->System->Link('/login/').'">'.T('Login').'</a>&nbsp;'.
        '<a href="'.$this->System->Link('/registration/').'">'.T('Registration').'</a>';
    }
    return $Output;
  }

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