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

namespace Kufer\Web\Classes\Menu\MenuPunkt;

final class MenuPunktFactory
{
  private function __construct()
  {
  }

  
  private static function createMenuPunktToolFromSettings(
    array $pAllowedToolIdsAry,
    bool $useOldMenuBarSettingsForResponsiveTools,
    bool $isResponsiveToolsOrWebadmin,
    $webUserId,
    array $buttonSettings,
    array &$checkDubletten
  ): ?MenuPunkt {
    if (!isset($buttonSettings['toolid']) || !\is_numeric($buttonSettings['toolid'])) {
      return null; 
    }

    $toolId = (int)$buttonSettings['toolid'];
    if (!\in_array($toolId, $pAllowedToolIdsAry)) {
      return null; 
    }

    $isToolKursvorschlag = (bool)($buttonSettings['isToolKursvorschlag'] ?? false);
    if ($isResponsiveToolsOrWebadmin && $toolId == \ToolsAPI::TOOL_KURSPLANUNG_DOZ) {
      
      if (($isToolKursvorschlag && !\MenuBar::isRecht4ButtonNeuerKursvorschlagAllowed($webUserId))
          || !\MenuBar::isButtonKursplanungAllowed($webUserId)) {
        return null;
      }
    }

    if (in_array('tool#' . $buttonSettings['toolid'] . $isToolKursvorschlag, $checkDubletten)) {
      
      return null;
    } else {
      $punkt = new MenuPunkt4Tool($toolId, self::getBeschriftung($buttonSettings), self::getAccessKey($buttonSettings), $isToolKursvorschlag);
      $checkDubletten[] = 'tool#' . $buttonSettings['toolid'] . $isToolKursvorschlag;

      if ($useOldMenuBarSettingsForResponsiveTools) {
        $gruppe = \MenuBar::getToolGruppe($toolId);
        if ($gruppe !== '') {
          
          $punkt->setGroupName($gruppe);
        }

        return $punkt; 
      } else {
        return $punkt;
      }
    }
  }

  private static function createMenuPunktGruppeFromSettings(
    array $pAllowedToolIdsAry,
    bool $useOldMenuBarSettingsForResponsiveTools,
    bool $isResponsiveToolsOrWebadmin,
    bool $pLoginAllowed,
    bool $pPwAllowed,
    $webUserId,
    array $buttonSettings,
    array &$checkDubletten = array()
  ): MenuPunkt {
    $menuPunktGruppe = new MenuPunkt4ToolGruppe(self::getBeschriftung($buttonSettings));
    foreach ($buttonSettings as $unterpunkt) {
      if (\is_array($unterpunkt)) { 
        $punkt = self::createFromSettings(
          $pAllowedToolIdsAry,
          $useOldMenuBarSettingsForResponsiveTools,
          $isResponsiveToolsOrWebadmin,
          $pLoginAllowed,
          $pPwAllowed,
          $webUserId,
          $unterpunkt,
          $checkDubletten
        );
        if (!\is_null($punkt)) {
          $punkt->setGroupName($menuPunktGruppe->getBeschriftung());
          $menuPunktGruppe->unterpunkte[] = $punkt;
        }
      }
    }

    return $menuPunktGruppe;
  }

  private static function createMenuPunktSpaceFromSettings(array $buttonSettings): MenuPunkt
  {
    return new MenuPunkt4Space();
  }

  private static function createMenuPunktLinkFromSettings(
    array $pAllowedToolIdsAry,
    array $buttonSettings,
    array &$checkDubletten = array()
  ): ?MenuPunkt {
    $link = \StringOperations::html_entity_decode($buttonSettings['link'], ENT_NOQUOTES);

    if (trim($link) === 'BFI_SPECIAL_FEEDBACK' && \DBWorkBASysSettings::isBFI()) {
      return new MenuPunkt4BFIEvaluation(self::getBeschriftung($buttonSettings), self::getAccessKey($buttonSettings));
    }

    if (!(\DBWorkBASysSettings::isBlak() && \in_array(47, $pAllowedToolIdsAry))) { 
      if (in_array('link#' . $buttonSettings['link'], $checkDubletten)) {
        
        return null;
      }

      $checkDubletten[] = 'link#' . $buttonSettings['link'];

      return new MenuPunkt4Link($link, self::getBeschriftung($buttonSettings), self::getAccessKey($buttonSettings));
    }

    return null;
  }

  private static function createMenuPunktNewLoginFromSettings(bool $pLoginAllowed, array $buttonSettings): ?MenuPunkt
  {
    if ($pLoginAllowed) {
      return new MenuPunkt4NewLogin(self::getBeschriftung($buttonSettings), self::getAccessKey($buttonSettings));
    }

    return null;
  }

  private static function createMenuPunktNewPasswordFromSettings(bool $pPwAllowed, array $buttonSettings): ?MenuPunkt
  {
    if ($pPwAllowed) {
      return new MenuPunkt4NewPw(self::getBeschriftung($buttonSettings), self::getAccessKey($buttonSettings));
    }

    return null;
  }

  private static function getBeschriftung(array $buttonSettings): string
  {
    return \StringOperations::html_entity_decode($buttonSettings['beschriftung'] ?? '', ENT_NOQUOTES);
  }

  private static function getAccessKey(array $buttonSettings): string
  {
    return \StringOperations::html_entity_decode($buttonSettings['accesskey'] ?? '', ENT_NOQUOTES);
  }

  
  public static function createFromSettings(
    array $pAllowedToolIdsAry,
    bool $useOldMenuBarSettingsForResponsiveTools,
    bool $isResponsiveToolsOrWebadmin,
    bool $pLoginAllowed,
    bool $pPwAllowed,
    $webUserId,
    array $buttonSettings,
    array &$checkDubletten = array()
  ): ?MenuPunkt {
    switch ($buttonSettings['type'] ?? null) { 
      default:
        
        return null;
      case 'tool':
        return self::createMenuPunktToolFromSettings(
          $pAllowedToolIdsAry,
          $useOldMenuBarSettingsForResponsiveTools,
          $isResponsiveToolsOrWebadmin,
          $webUserId,
          $buttonSettings,
          $checkDubletten
        );
      case 'menuGruppe':
        return self::createMenuPunktGruppeFromSettings(
          $pAllowedToolIdsAry,
          $useOldMenuBarSettingsForResponsiveTools,
          $isResponsiveToolsOrWebadmin,
          $pLoginAllowed,
          $pPwAllowed,
          $webUserId,
          $buttonSettings,
          $checkDubletten
        );
      case 'space':
        return self::createMenuPunktSpaceFromSettings($buttonSettings);
      case 'link':
        return self::createMenuPunktLinkFromSettings($pAllowedToolIdsAry, $buttonSettings, $checkDubletten);
      case 'login':
        return self::createMenuPunktNewLoginFromSettings($pLoginAllowed, $buttonSettings);
      case 'password':
        return self::createMenuPunktNewPasswordFromSettings($pPwAllowed, $buttonSettings);
    }
  }
}