<?php

include_once(dirname(__FILE__).'/../../Base/View.php');

class DebugView extends View
{
  var $ItemListFormClass = array(
    'Title' => 'Restarty',
    'Table' => 'Debug',
    'Items' => array(
      'Time' => array('Type' => 'String', 'Caption' => 'Čas', 'Default' => ''),
      'MangosVersion' => array('Type' => 'String', 'Caption' => 'Verze emulátoru', 'Default' => ''),
      'DbVersion' => array('Type' => 'String', 'Caption' => 'Verze databáze', 'Default' => ''),
      'MaxPlayerCount' => array('Type' => 'Integer', 'Caption' => 'Max. online hráčů', 'Default' => ''),
      'Uptime' => array('Type' => 'String', 'Caption' => 'Doba běhu', 'Default' => ''),
    ),
  );
  var $ItemFormClass = array(
    'Title' => 'Záznam restartu',
    'Table' => 'Debug',
    'Items' => array(
      'Time' => array('Type' => 'DateTime', 'Caption' => 'Čas', 'Default' => ''),
      'MangosVersion' => array('Type' => 'String', 'Caption' => 'Verze emulátoru', 'Default' => ''),
      'DbVersion' => array('Type' => 'String', 'Caption' => 'Verze databáze', 'Default' => ''),
      'MaxPlayerCount' => array('Type' => 'Integer', 'Caption' => 'Max. online hráčů', 'Default' => ''),
      'Uptime' => array('Type' => 'String', 'Caption' => 'Doba běhu', 'Default' => ''),
    ),
  );
  
  function ShowListOnRow($Row)
  {
    $Row['Time'] = '<a href="?Module=Debug&amp;Action=View&amp;Id='.$Row['Id'].'&amp;Show=Backtrace">'.str_replace(' ', '&nbsp;', $Row['Time']).'</a>';
    $Row['Uptime'] = TimeToHumanTime($Row['Uptime']);
    return($Row);
  }
  
  function ItemList()
  {
    if($this->System->Modules['User']->User['Role'] >= USER_ROLE_USER)
    {
      $Realm = new Realm($this->System, $_GET['Id']);
      if(($this->System->Modules['User']->User['Id'] == $Realm->GetUser()) or ($this->System->Modules['User']->User['Role'] >= USER_ROLE_ADMINISTRATOR))
      {
        $Output = '<h4>Ladící záznamy restartů</h4>';
        $Table = new Table($this->ItemListFormClass, $this->System);
        $Table->OnRow = array($this, 'ShowListOnRow');
        $Table->Definition['Table'] = '(SELECT * FROM Debug WHERE Realm='.$_GET['Id'].')';
        $Table->LoadValuesFromDatabase($this->Database);
        $Output .= $Table->Show();
        if($this->System->Modules['User']->User['Role'] >= USER_ROLE_ADMINISTRATOR) 
        {
          //$Output .= '<br /><div style="text-align: center;"><a href="?Action=BackupAdd">Zálohovat</a></dev>';      
        }
      } else $this->SystemMessage('Ladící záznamy', 'Nemáte oprávnění');
    } else $Output .= USER_BAD_ROLE;
    return($Output);
  }

  function Item()
  {
    if($this->System->Modules['User']->User['Role'] >= USER_ROLE_USER)
    {
      $Realm = new Realm($this->System, $_GET['Id']);
      if(($this->System->Modules['User']->User['Id'] == $Realm->GetUser()) or ($this->System->Modules['User']->User['Role'] >= USER_ROLE_ADMINISTRATOR))
      {
        $Output = '<div>Ladící informace serveru</div>'; 
    
    $MangosDebug = new MangosDebug($this->System, $this->System);
    $Form = new Form($this->System, $this->ItemFormClass);
    if($Form->LoadValuesFromDatabase($_GET['Id']))
    {
        $Form->Values['Uptime'] = TimeToHumanTime($Form->Values['Uptime']);
        $Output .= $Form->ShowTable();
      
        $Output .= '<a href="?Module=Debug&amp;Action=View&amp;Id='.$_GET['Id'].'&amp;Show=Backtrace">Backtrace</a>'.
          '  <a href="?Module=Debug&amp;Action=View&amp;Id='.$_GET['Id'].'&amp;Show=Log">Mangos log</a>'.
          '  <a href="?Module=Debug&amp;Action=View&amp;Id='.$_GET['Id'].'&amp;Show=Error">Console error log</a>'.
          '  <a href="?Module=Debug&amp;Action=View&amp;Id='.$_GET['Id'].'&amp;Show=DbErrors">Database error log</a>'.
          '  <a href="?Module=Debug&amp;Action=View&amp;Id='.$_GET['Id'].'&amp;Show=Configuration">Mangos configuration</a>'.
          '<hr>';
        if(array_key_exists('Show', $_GET))
        {
          $Show = addslashes($_GET['Show']);     
          switch($Show)
          {
            case 'Backtrace':
              $DbResult = $this->Database->select('Debug', 'Backtrace', 'Id='.$_GET['Id']);
              $DbRow = $DbResult->fetch_assoc();
              $Content = htmlspecialchars($DbRow['Backtrace']);
              for($I = 1; $I < $MangosDebug->MaxMangosThreadCount; $I++)
              {
                $Content = str_replace('Thread '.$I.' ', '<hr><strong id="'.$I.'">Thread '.$I.'</strong>', $Content);
                $Content = str_replace(' '.$I.' Thread ', '<a href="#'.$I.'"">'.$I.' Thread</a>', $Content);
              }
              $Output .= '<strong>Backtrace:</strong> <br><pre>'.$Content.'</pre>';
              break;
            case 'Log': 
              $DbResult = $this->Database->select('Debug', 'Log', 'Id='.$_GET['Id']);
              $DbRow = $DbResult->fetch_assoc();
              $Output .= '<strong>Console standard output log:</strong> <br><pre>'.htmlspecialchars($DbRow['Log']).'</pre>';
              break;
            case 'Error': 
              $DbResult = $this->Database->select('Debug', 'ErrorLog', 'Id='.$_GET['Id']);
              $DbRow = $DbResult->fetch_assoc();
              $Output .= '<strong>Console error log:</strong> <br><pre>'.htmlspecialchars($DbRow['ErrorLog']).'</pre>';
              break;
            case 'DbErrors': 
              $DbResult = $this->Database->select('Debug', 'DbErrors', 'Id='.$_GET['Id']);
              $DbRow = $DbResult->fetch_assoc();
              $Output .= '<strong>Database error log:</strong> <br><pre>'.htmlspecialchars($DbRow['DbErrors']).'</pre>';
              break;
            case 'Configuration': 
              $DbResult = $this->Database->select('Debug', 'Configuration', 'Id='.$_GET['Id']);
              $DbRow = $DbResult->fetch_assoc();
              $Output .= '<strong>Mangos configuration:</strong> <br><pre>'.htmlspecialchars($DbRow['Configuration']).'</pre>';
              break;
          }
        }
    } else $Output = 'Položka nenalezena';
      } else $this->SystemMessage('Ladící informace', 'Nemáte oprávnění');
    } else $Output = USER_BAD_ROLE;
    return($Output);
  }
}
