<?php
/**
 * @copyright (c) Kufer Software GmbH, Loisachstrasse 4, D-84453 Mühldorf
 */

namespace Kufer\KuferTools\Basis\Button;

use FunctionOperations;

class Buttonleiste
{
  
  public function getHtmlButtonleiste($buttons, $mainClass = '')
  {
    $buttonViewModel = new \Kufer\KuferTools\Basis\Button\ButtonViewModel();
    $buttonViewModel->mainClass = $mainClass;
    $buttonViewModel->canCollapse = $this->canCollapse($buttons);
    $noCollapseButtons = $this->getNonCollapseButtons($buttons);
    $buttonViewModel->no_collapse_buttons = $this->getButtonsAsHTML($noCollapseButtons, true);
    $view = new \Kufer\KuferTools\Basis\Button\ButtonView($buttonViewModel);

    if (count($buttons) == 0) {
      return $view->getNoData();
    }

    $result = $view->getHeader();
    $result .= $this->getButtonsAsHTML($buttons, false);
    $result .= $view->getFooter();

    return $result;
  }

  
  private function canCollapse($buttons)
  {
    return count($buttons) > 2;
  }

  
  private function getNonCollapseButtons($buttons)
  {
    return array_filter($buttons, function($button) {
      return $button[0]->collapse == false;
    });
  }

  
  private function getButtonsAsHTML($buttons, $nebenFunktionenButton)
  {
    $buttonEinzelnViewModel = new ButtonEinzelnViewModel();
    $buttonGruppeViewModel = new ButtonGruppeViewModel();
    $html = '';
    foreach ($buttons as $buttonGroup) {
      if (empty($buttonGroup)) {
        continue;
      }

      $buttonGroupCount = count($buttonGroup);
      if ($buttonGroupCount === 1) {
        $buttonEinzelnViewModel->button = $buttonGroup[0];
        $buttonEinzelnViewModel->button->nebenFunktionenButton = $nebenFunktionenButton;
        $buttonEinzelnViewModel->button->canCollapse = $this->canCollapse($buttons);
        $buttonView = new ButtonEinzelnView($buttonEinzelnViewModel);
      } else  {
        foreach ($buttonGroup as $button) {
          $button->nebenFunktionenButton = $nebenFunktionenButton;
          $button->canCollapse = $this->canCollapse($buttons);
        }
        $buttonGruppeViewModel->buttons = $buttonGroup;
        $buttonView = new ButtonGruppeView($buttonGruppeViewModel);
      }
      $html .= $buttonView->getHeader();
      $html .= $buttonView->getBody();
      $html .= $buttonView->getFooter();
    }

    return $html;
  }
}

class HtmlButton extends \Datamodul
{
  public $beschriftung;
  public $class;
  public $link;
  public $icon;
  public $target;
  public $title;
  public $value;
  public $type;
  public $collapse;
  
  public $disabled;

  
  public $bootstrapColor;

  public function __construct($beschriftung, $class, $link, $icon, $title = '', $target = '', $bootstrapColor = 'default', $value = '', $type = 'button', $collapse = true, bool $disabled = false)
  {
    parent::__construct();

    $this->beschriftung = $beschriftung;
    $this->class = $class;
    $this->link = $link;
    $this->icon = $icon;
    $this->target = $target ?? '';
    $this->bootstrapColor = $bootstrapColor ?? 'default';
    $this->title = $title;
    $this->value = $value ?? '';
    $this->type = $type ?? 'button';
    $this->collapse = $collapse ?? true;
    $this->disabled = $disabled ?? false;
  }

  
  public function overwriteButtonWithView(\Kufer\KuferTools\Basis\View\ViewSight $view): self
  {
    
    $fnClean = FunctionOperations::combine(
      
      \FunctionOperations::args('\\preg_replace', '/<!--.*-->/m', ''),
      '\\trim'
    );

    $newBeschriftung = $fnClean($view->getHeader());
    $newTitle = $fnClean($view->getBody());
    $newIcon = $fnClean($view->getFooter());
    $newBootstrapColor = $fnClean($view->getNoData());
    $this->beschriftung = $newBeschriftung === '' ? $this->beschriftung : $newBeschriftung;
    $this->title = $newTitle === '' ? $this->title : $newTitle;
    $this->icon = $newIcon === '' ? $this->icon : $newIcon;
    $this->bootstrapColor = $newBootstrapColor === '' ? $this->bootstrapColor : $newBootstrapColor;

    return $this;
  }
}