<?php
namespace nur\v\bs3\fo;

use nur\b\params\Tparametrable;
use nur\c;
use nur\v\v;

abstract class ControlVisual extends Control {
  use Tparametrable;

  const PARAMETRABLE_PARAMS_SCHEMA = [
    "help" => ["?content", null, "texte d'aide à la saisie"],
    "class" => ["?array", null, "classes CSS du champ"],
    "style" => ["?string", null, "style CSS du champ"],
    "readonly" => ["?bool", null, "ce contrôle est-il en lecture seule?"],
    "disabled" => ["?bool", null, "ce contrôle est-il désactivé?"],
    "naked" => ["bool", false, "faut-il afficher le contrôle uniquement"],
    "prefix" => ["?content", null, "contenu à afficher avant"],
    "suffix" => ["?content", null, "contenu à afficher après"],
  ];

  /** @var array|string */
  protected $ppHelp;

  /** @var ?array */
  protected $ppClass;

  /** @var ?string */
  protected $ppStyle;

  /** @var ?bool */
  protected $ppReadonly;

  /** @var ?bool */
  protected $ppDisabled;

  /** @var bool */
  protected $ppNaked;

  /** @var array|string */
  protected $ppPrefix;

  /** @var array|string */
  protected $ppSuffix;

  protected function getStdFeedback(string $name, $value): array {
    $fgClass = null;
    $feedback = null;
    $result = $this->form->_getResult($name);
    if ($this->form->isSubmitted() && $result !== null) {
      if ($result["valid"]) {
        $fgClass = "has-success";
      } else {
        $fgClass = "has-error has-feedback";
        $feedback = v::span([
          "class" => "glyphicon glyphicon-remove form-control-feedback",
          "aria-hidden" => "true",
        ]);
      }
    }
    return [$fgClass, $feedback];
  }

  protected function getFglcLayout($label, bool $showLabel, array $control, ?string $fgClass, ?array $feedback): array {
    $form = $this->form;
    $vs = [$form->getControlPrefix(), q($this->ppPrefix)];

    $showHelp = $form->SHOW_HELP();
    if ($form->HORIZONTAL()) {
      $input = [
        $control,
        $feedback,
        v::if($showHelp, v::span([
          "class" => "help-block",
          c::qnz($this->ppHelp),
        ])),
      ];
      $fgcClass = $form->FGC_CLASS();
      if ($fgcClass !== null) {
        $input = v::div([
          "class" => $fgcClass,
          $input,
        ]);
      }
      $vs[] = v::div([
        "class" => ["form-group", $fgClass],
        v::tag("label", [
          "for" => $this->ppId,
          "class" => [
            "control-label",
            $showLabel? null: "sr-only",
            $form->FGL_CLASS(),
          ],
          $label,
        ]),
        $input,
      ]);
    } else {
      $vs[] = v::div([
        "class" => ["form-group", $fgClass],
        v::tag("label", [
          "for" => $this->ppId,
          "class" => $showLabel? null: "sr-only",
          $label, "&nbsp;",
        ]),
        $control,
        $feedback,
        v::if($showHelp, v::span([
          "class" => "help-block",
          c::qnz($this->ppHelp),
        ])),
      ]);
    }

    $vs[] = q($this->ppSuffix);
    $vs[] = $form->getControlSuffix();
    return $vs;
  }

  protected function getFgsLayout(array $control, bool $helpBefore=false): array {
    $form = $this->form;
    $vs = [$form->getControlPrefix(), q($this->ppPrefix)];

    $showHelp = $form->SHOW_HELP();
    if ($form->HORIZONTAL()) {
      $vs[] = v::div([
        "class" => "form-group",
        v::div([
          "class" => $form->FGS_CLASS(),
          v::if($showHelp && $helpBefore, v::span([
            "class" => "help-block",
            c::qnz($this->ppHelp),
          ])),
          $control,
          v::if($showHelp && !$helpBefore, v::span([
            "class" => "help-block",
            c::qnz($this->ppHelp),
          ])),
        ]),
      ]);
    } else {
      $vs[] = [
        v::if($showHelp && $helpBefore, v::span([
          "class" => "help-block",
          c::qnz($this->ppHelp),
        ])),
        $control,
        v::if($showHelp && !$helpBefore, v::span([
          "class" => "help-block",
          c::qnz($this->ppHelp),
        ])),
      ];
    }

    $vs[] = q($this->ppSuffix);
    $vs[] = $form->getControlSuffix();
    return $vs;
  }
}
