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




abstract class FarbeAPI
{
  
  abstract public static function getStatus(\Belegung $pObj): string;

  
  abstract protected static function getStatusAry(): array;

  
  abstract protected static function getSettingsName(): string;

  
  abstract protected static function getDescriptionForStatus(string $pStatus): string;

  
  protected static function getSetting(bool $pRefreshCache = false): array
  {
    $settingName = static::getSettingsName();
    static $settingCache = array();
    if ($pRefreshCache || !isset($settingCache[$settingName])) {
      $tool0Settings = ToolSettingsAPI::getToolSettings(0);
      $settingCache[$settingName] = SerializeOperations::unserialize($tool0Settings->{$settingName});
    }

    return $settingCache[$settingName];
  }

  
  protected static function getMandatoryStatus(): array
  {
    return array();
  }

  
  public static function getColor(string $pStatus): string
  {
    $stat_kurs_farbe = static::getSetting();

    if (isset($stat_kurs_farbe[$pStatus], $stat_kurs_farbe[$pStatus]['color'])) {
      return '#' . $stat_kurs_farbe[$pStatus]['color'];
    }

    return '#FFFFFF';
  }

  
  protected static function getDescriptionForStatusFromSettings(string $pStatus): string
  {
    $statSetting = static::getSetting();

    return $statSetting[$pStatus]['description'] ?? '';
  }

  
  public static function getSetting4ZentraleEinstellungen(string $pName, bool $pShowInfoBox = false): string
  {
    
    
    
    $statusAry = static::getStatusAry();
    $stat_kurs_farbe = static::getSetting();
    $mandatoryStatusAry = static::getMandatoryStatus();

    $result = '<div class="settings_status_div text_main_menu">';
    $result .= '<div class="settings_status_head">
                 <span class="head_status">Status</span>
                 <span class="head_aktiv">aktiv?</span>
                 <span class="head_bezeichnung">Ihre Bezeichnung</span>
                 <span class="head_farbe">Farbe</span>
               </div>';
    $result .= '<ul class="setting_status_farbe">';
    $i = 0;
    $name4Html = CleanOperations::cleanStringForHtml($pName);

    foreach ($statusAry as $status) {
      $checked = '';
      $color = 'FFFFFF';
      $disabled = '';

      if (in_array($status, $mandatoryStatusAry)) {
        $checked = 'checked="checked"';
        $disabled = 'disabled="disabled"';
      }

      if (isset($stat_kurs_farbe[$status], $stat_kurs_farbe[$status]['color'])) {
        $checked = 'checked="checked"';
        $color = CleanOperations::cleanStringForHtml($stat_kurs_farbe[$status]['color']);
      }

      $statusName = CleanOperations::cleanStringForHtml(static::getDescriptionForStatus($status));
      $statusNameFromSetting = CleanOperations::cleanStringForHtml(static::getDescriptionForStatusFromSettings($status));

      if (trim($statusNameFromSetting) == '') {
        $statusNameFromSetting = $statusName;
      }

      $status4Html = CleanOperations::cleanStringForHtml($status);

      $even = ($i % 2 == 0) ? 'class="even"' : '';
      $i++;
      $result .= '<li ' . $even . '>';
      $result .= '<label for="setting_status_' . $name4Html . '_' . $status4Html . '" class="setting_status_text">' . $statusName . '</label>';
      $result .= '<input id="setting_status_' . $name4Html . '_' . $status4Html . '" type="checkbox"  class="setting_status_checkbox" value="' . $status4Html . '" name="' . $name4Html . '[]" ' . $checked . ' ' . $disabled . '>';
      $result .= '<input type="text" class="setting_description_input" name="setting_description_' . $status4Html . '_' . $name4Html . '" value="' . $statusNameFromSetting . '" >';
      $result .= '<span class="setting_setColor" style="background-color:#' . $color . ';"><input type="hidden" name="setting_color_' . $status4Html . '_' . $name4Html . '" class="hidden_color" value="' . $color . '"></span>';
      $result .= '</li>';
    }
    $result .= '</ul>';

    if ($pShowInfoBox) {
      $infoBoxBody = array('<span class="farbInfoZeile"><span class="setting_info">aktiv?:</span>Entscheiden Sie, ob der Status berücksichtigt wird</span>',
                            '<span class="farbInfoZeile"><span class="setting_info">Bezeichnung:</span>Stellen Sie hier die Beschreibung für die Legende ein</span>',
                            '<span class="farbInfoZeile"><span class="setting_info">Farbe:</span>Klicken Sie in das Feld, um die Farbe zu ändern</span>');

      $result .= HTMLOperations::getInfoBox($infoBoxBody, 'Farben einstellen');
    }
    
    $result .= '</div>';

    return $result;
  }

  
  public static function getLegend(string $title = 'Status Legende'): string
  {
    $body = self::getLegendBody();

    return '<div class="status_legende">' . HTMLOperations::getInfoBox($body, CleanOperations::cleanStringForHtml($title)) . '</div>';
  }

  public static function getLegendBody(): string
  {
    $statusAry = static::getStatusAry();
    $colorAry = static::getSetting();

    if (empty($colorAry)) {
      return '';
    }

    $result = '<ul class="status_legende_list">';

    foreach ($statusAry as $status) {
      $color = 'FFFFFF';

      if (isset($colorAry[$status], $colorAry[$status]['color'])) {
        $color = $colorAry[$status]['color'];
      } else {
        continue;
      }

      $statusNameFromSetting = static::getDescriptionForStatusFromSettings($status);
      if (trim($statusNameFromSetting) == '') {
        $statusNameFromSetting = static::getDescriptionForStatus($status);
      }

      $result .= '<li class="status_legende_' . CleanOperations::cleanStringForHtml($status) . '">';
      $result .= '<span class="status_legende_colorSpan" style="background-color:#' . CleanOperations::cleanStringForHtml($color) . ';"></span>';
      $result .= '<span">' . CleanOperations::cleanStringForHtml($statusNameFromSetting) . '</span>';
      $result .= '</li>';
    }
    $result .= '</ul>';

    return $result;
  }

  
  public static function getSetting4Obj(string $pName): string
  {
    $statusAry = PostOperations::getVarValue($pName, array());
    $mandatoryStatusAry = static::getMandatoryStatus();
    $statusAry = array_merge($statusAry, $mandatoryStatusAry);
    $settingsAry = array();
    foreach ($statusAry as $status) {
      $color = PostOperations::getVarValue('setting_color_' . $status . '_' . $pName, 'FFFFFF');
      $description = PostOperations::getVarValue('setting_description_' . $status . '_' . $pName, '');
      $settingsAry[$status]['color'] = $color;
      $settingsAry[$status]['description'] = $description;
    }

    return SerializeOperations::serialize4Settings($settingsAry);
  }
}