<?php

include_once(dirname(__FILE__).'/../../Base/View.php');

class EmulatorView extends View
{
  var $ItemFormClass = array(
    'Title' => 'Emulátor',
    'Table' => 'Emulator',
    'Items' => array(
      'Name' => array('Type' => 'String', 'Caption' => 'Název', 'Default' => ''),
      'Version' => array('Type' => 'String', 'Caption' => 'Verze', 'Default' => ''),
      'Revision' => array('Type' => 'Integer', 'Caption' => 'Revize', 'Default' => 0),
      'Client' => array('Type' => 'Client', 'Caption' => 'Verze klienta', 'Default' => 0),
      'ScriptDev2Revision' => array('Type' => 'Integer', 'Caption' => 'Verze SD2', 'Default' => 0),
      'ScriptDev2PatchFileName' => array('Type' => 'String', 'Caption' => 'SD2 záplata', 'Default' => ''),
      'CommitHash' => array('Type' => 'String', 'Caption' => 'Commit HASH', 'Default' => ''),
    ),
  );
  var $ItemListFormClass = array(
    'Title' => 'Emulátory',
    'Table' => '(SELECT `Emulator`.`Name`, `Emulator`.`Id`, `Client`.`Version` AS `ClientVersion`, `Emulator`.`Revision`, `Emulator`.`ScriptDev2Revision`, `Emulator`.`ScriptDev2PatchFileName`, `Emulator`.`Version`, `Emulator`.`CommitHash` FROM `Emulator` JOIN `Client` ON `Client`.`Id` = `Emulator`.`Client` WHERE `Emulator`.`Enable` = 1)',
    'DefaultOrderColumn' => 'Revision',
    'DefaultOrderDirection' => 1,
    'Items' => array(
      'Name' => array('Type' => 'String', 'Caption' => 'Název', 'Default' => ''),
      'Version' => array('Type' => 'String', 'Caption' => 'Verze', 'Default' => ''),
      'Revision' => array('Type' => 'Integer', 'Caption' => 'Revize', 'Default' => 0),
      'ClientVersion' => array('Type' => 'Integer', 'Caption' => 'Verze klienta', 'Default' => 0),
      'ScriptDev2Revision' => array('Type' => 'Integer', 'Caption' => 'Verze SD2', 'Default' => 0),
      //'ScriptDev2PatchFileName' => array('Type' => 'String', 'Caption' => 'SD2 záplata', 'Default' => ''),
      //'CommitHash' => array('Type' => 'String', 'Caption' => 'Commit HASH', 'Default' => ''),
    ),
  );

  function ItemList()
  {
    $Output = '<h4>Seznam verzí emulátoru</h4>';
    $Table = new Table($this->ItemListFormClass, $this->System);
    $Table->Definition['Items']['Id'] = array('Type' => 'Hidden', 'Caption' => '', 'Default' => '');
    $Table->LoadValuesFromDatabase($this->Database);
    $Table->Definition['Items']['Actions'] = array('Type' => 'String', 'Caption' => '', 'Default' => '');
    foreach($Table->Values as $Index => $Item)
    {
      $Table->Values[$Index]['Actions'] = '<a href="?Module=Emulator&amp;Action=Show&amp;Id='.$Item['Id'].'">Podrobnosti</a>';
      unset($Table->Values[$Index]['Id']);
    }
    $Output .= $Table->Show();
    if($this->System->Modules['Permission']->Check('Emulator', 'Add'))
    {
      $Output .= '<br /><div style="text-align: center;"><a href="?Module=Emulator&amp;Action=Add">Přidat emulátor</a></dev>';      
    }
    return($Output);
  }

  function Item()
  {
    $Id = $_GET['Id'];
    //$Server = new Server($this->System, $Id);
    $Output = '<h4>Podrobnosti emulátoru</h4>';
    $Form = new Form($this->System, $this->ItemFormClass);
    $Form->LoadValuesFromDatabase($Id);
    $Output .= $Form->ShowTable();
    $Output .= '<div style="text-align: center">';
    $Emulator = new Emulator($this->System, $Id);
    if($this->System->Modules['Permission']->Check('Emulator', 'Download', $Emulator->Id))
    {
      if($Emulator->Emulator['Lock'] == 0) $Output .= ' <a href="?Module=Emulator&amp;Action=Download&amp;Id='.$Id.'">Stáhnout</a>';
    }
    if($this->System->Modules['Permission']->Check('Emulator', 'Compile', $Emulator->Id))
    {
      if($Emulator->Emulator['Lock'] == 0) $Output .= ' <a href="?Module=Emulator&amp;Action=Compile&amp;Id='.$Id.'">Přeložit</a>';
    }
    $Output .= '</div>';
    return($Output);
  }
  
  function Add()
  {
    if($this->System->Modules['Permission']->Check('Emulator', 'Add'))
    {
      $Form = new Form($this->System, $this->ItemFormClass);
      $Form->LoadValuesFromForm();
      $Form->OnSubmit = '?Module=Emulator&amp;Action=AddFinish';
      $Output = $Form->ShowEditForm();
    } else $Output = USER_BAD_ROLE;
    return($Output);
  }
    
  function AddFinish()
  {
    if($this->System->Modules['Permission']->Check('Emulator', 'Add'))
    {
      $Form = new Form($this->System, $this->ItemFormClass);
      $Form->LoadValuesFromForm();
      $Form->SaveValuesToDatabase(0);
      $Output = $this->System->SystemMessage('Přidání nového emulátoru', 'Emulátor přidán.');
    } else $Output = USER_BAD_ROLE;
    return($Output);    
  }
  
  function Download()
  {
    if(array_key_exists('Id', $_GET)) 
    {
      $Emulator = new Emulator($this->System, $_GET['Id']);
      if($this->System->Modules['Permission']->Check('Emulator', 'Download', $Emulator->Id))
      {
        $Emulator->Download();       
        $Output = $this->System->SystemMessage('Stažení emulátoru', 'Úloha zařazena do fronty');
        $TaskView = new TaskView($this->System);
        $Output .= $TaskView->ItemList();
      } else $Output = $this->System->Modules['Permission']->AccessDenied();
    } else $Output = $this->System->SystemMessage('Stažení emulátoru', 'Nebylo zadáno Id');
    return($Output);
  }
  
  function Compile()
  {
    if(array_key_exists('Id', $_GET)) 
    {
      $Emulator = new Emulator($this->System, $_GET['Id']);
      if($this->System->Modules['Permission']->Check('Emulator', 'Download', $Emulator->Id))
      {
        $Emulator->Compile();       
        $Output = $this->System->SystemMessage('Překlad emulátoru', 'Úloha zařazena do fronty');
        $TaskView = new TaskView($this->System);
        $Output .= $TaskView->ItemList();
      } else $Output = $this->System->Modules['Permission']->AccessDenied();
    } else $Output = $this->System->SystemMessage('Překlad emulátoru', 'Nebylo zadáno Id');
    return($Output);
  }
}
