<?php
/**
 * @copyright (c) Kufer Software GmbH, Oderstraße 7, D-84453 Mühldorf
 */

namespace Kufer\Web\Classes\Menu\MenuPunkt;


class MenuPunkt4Tool extends MenuPunkt
{
  public $toolId;
  public $isToolKursvorschlag;

  
  public function __construct($pToolId, $pBeschriftung = '', $pAccessKey = '', $pIsToolKursvorschlag = false)
  {
    $this->toolId = $pToolId;
    $this->beschriftung = $pBeschriftung;
    $this->accesskey = $pAccessKey;
    $this->isToolKursvorschlag = $pIsToolKursvorschlag;
  }

  

  
  public function createButton($buttonWidth = 150)
  {
    $buttonWidth = ($buttonWidth < 150) ? 150 : $buttonWidth;

    if (\SessionOperations::isWebadmin()) {
      $result = '<div class="backgroundButton glass">' . $this->getBeschriftung() . '</div>' . "\n";
    }
    
    else {
      $result = '<img alt="" style="width: ' . $buttonWidth . 'px;" src="layout/button_background.gif">';
      $result .= '<span class="text_left_menu" style="width: ' . $buttonWidth . 'px">' . $this->getBeschriftung() . '</span>';
    }

    return $result;
  }

  
  public function getType()
  {
    return 'tool';
  }

  
  public function getTool()
  {
    return $this->toolId;
  }

  
  public function getBeschriftung()
  {
    if ($this->beschriftung != '') {
      return $this->beschriftung;
    } else {
      return self::buttonGetDefaultBeschriftung($this->toolId);
    }
  }

  
  public function getLink()
  {
    $firstSectionAry = \ConstantOperations::getFirstToolSections();
    $objsettings = \ToolSettingsAPI::getToolSettings(0);

    
    
    $checkBoxToolsAry = array(1, 5, 8, 20, 21, 32, 34);
    $link = '';
    if (($this->toolId) > 0) {
      $link = 'usertools.php';
      $link .= '?tool_id=' . $this->toolId . '&toolsection_id=' . (isset($firstSectionAry[$this->toolId]) ? $firstSectionAry[$this->toolId] : null);
      $link .= '&einstieg=1';

      
      
      if ($objsettings->keepFilterSettings) {
        switch ($this->toolId) {
          case 11:
            $searchString = 'searchteilnehmer=1';
            break;
          case 12:
            $searchString = 'searchdozent=1';
            break;
          case 33:
            $searchString = 'searchadresse=1';
            break;
          case 5:
          case 34:
            $searchString = 'searchgebaeude=1';
            break;
          default:
            $searchString = 'searchkurs=1';
            break;
        }
        $link .= '&' . $searchString;
      }

      
      
      if (in_array($this->toolId, $checkBoxToolsAry)) {
        $link .= '&markerAction=loescheMarkierungen';
      }
    } else { 
      switch ($this->toolId) {
        
        
        
        

        case -1: 
          $tool0Settings = \ToolSettingsAPI::getToolSettings(0);
          $toolId = \RequestOperations::getVarValue('tool_id');
          $sightId = \RequestOperations::getVarValue('toolsection_id');
          if ($tool0Settings->linkToCurrentToolSettings && $toolId != '' && $sightId != '') {
            $link = 'hleinstell.php?tool=' . $toolId . '&sightid=' . $sightId;
          } else {
            $link = 'hleinstell.php?tool=0&sightid=-1';
          }
          break;
        case -2: 
          $link = 'usertools.php?change_pwd=true';
          break;
        case -3: 
          if ($objsettings->vhswebsite) {
            $link = $objsettings->vhswebsite;
          } else {
            $link = '';
          }
          break;
      }
    }

    return $link;
  }

  
  public static function buttonGetDefaultBeschriftung($toolid): string
  {
    switch ($toolid) {
      default:
        try {
          return \ToolsAPI::getMenuTitle($toolid);
        } catch (\InvalidArgumentException $ex) {
          return '';
        }
        
      case -5:
        return 'Einstellungen';
        
      
        
      case -3:
        return 'Zu unserer Homepage';
        
      case -2:
        return 'Passwort &auml;ndern';
        
      case -1:
        return 'Zentrale Einstellungen';
    }
  }

  
  public function isToolKursvorschlag()
  {
    return $this->isToolKursvorschlag;
  }

  public function getToolsResponsiveLink(): string
  {
    if ($this->toolId == -3) { 
      $objsettings = \ToolSettingsAPI::getToolSettings(0);

      return $objsettings->vhswebsite;
    }
    if ($this->isToolKursvorschlag() && $this->getTool() == \ToolsAPI::TOOL_KURSPLANUNG_DOZ) {
      return 'index.php?tool_id=' . $this->getTool() . '&action=kursvorschlag';
    }

    return 'index.php?tool_id=' . $this->getTool() . '&einstieg=1';
  }

  public function isSameMenupunkt($menupunkt)
  {
    return $menupunkt->getType() == 'tool'
            && $menupunkt->getTool() == $this->getTool()
            && $menupunkt->isToolKursvorschlag() == $this->isToolKursvorschlag();
  }
}