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

use nur\A;
use nur\b\params\Tparametrable;
use nur\base;
use nur\func;
use nur\v\v;

class ControlSelect extends ControlVisual {
  use Tparametrable;

  const PARAMETRABLE_PARAMS_SCHEMA = [
    "label" => ["?content", null, "libellé du champ"],
    "items" => ["?iterable", null, "liste d'éléments à afficher"],
    "items_func" => ["?callable", null, "fonction fournissant une liste d'éléments à afficher"],
    "item_value_key" => ["?key", null, "clé de la valeur d'un élément"],
    "item_value_func" => ["?callable", null, "fonction fournissant la clé de la valeur d'un élément"],
    "item_text_key" => ["?key", null, "clé du libellé d'un élément"],
    "item_text_func" => ["?callable", null, "fonction fournissant la clé du libellé d'un élément"],
    "no_item_value" => ["?string", null, "valeur si aucun élément n'est sélectionné"],
    "no_item_text" => ["?content", null, "libellé si aucun élément n'est sélectionné"],
    "required" => ["?bool", null, "ce champ est-il requis?"],
    "show_required" => ["?bool", null, "faut-il afficher la marque de champ requis?"],
    "option_prefix" => ["?content", "\n", "contenu à afficher avant chaque option"],
    "option_suffix" => ["?content", null, "contenu à afficher après chaque option"],
  ];

  function __construct(Form $form, ?array $params=null) {
    parent::__construct($form, $params);
    base::update_n($this->ppShowRequired, $this->ppRequired);
  }

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

  /** @var ?iterable */
  protected $ppItems;

  /** @var ?callable */
  protected $ppItemsFunc;

  /** @var string|int|null */
  protected $ppItemValueKey;

  /** @var ?callable */
  protected $ppItemValueFunc;

  /** @var string|int|null */
  protected $ppItemTextKey;

  /** @var ?callable */
  protected $ppItemTextFunc;

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

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

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

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

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

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

  protected function vof(
    $value, ?callable $func,
    $item=null, $itemKey=null, ?int $itemIndex=null,
    ...$funcArgs) {
    if ($value !== null) return $value;
    if ($func !== null) {
      func::ensure_func($func, $this, $funcArgs);
      $value = func::call($func, ...$funcArgs);
      if ($value !== null) return $value;
    }
    if (A::is_array($item)) {
      $item = A::with($item);
      if ($itemKey !== null) {
        $value = A::get($item, $itemKey);
        if ($value !== null) return $value;
      }
      if ($itemIndex !== null) {
        $index = 0;
        foreach ($item as $value) {
          if ($index === $itemIndex) return $value;
          $index++;
        }
        return null;
      }
    }
    return $item;
  }

  function getControl(): array {
    $name = $this->ppName;
    $value = $this->ppValue;
    $items = $this->vof($this->ppItems, $this->ppItemsFunc);
    $noItemValue = $this->ppNoItemValue;
    $noItemText = $this->ppNoItemText;

    $options = [];
    if ($noItemValue !== null) {
      $options[] = [
        "value" => $noItemValue,
        "text" => $noItemText,
        "selected" => $value == $noItemValue,
      ];
    }
    foreach ($items as $key => $item) {
      $itemValue = $this->vof(null, $this->ppItemValueFunc, $item, $this->ppItemValueKey, 0, $item, $key);
      $itemText = $this->vof(null, $this->ppItemTextFunc, $item, $this->ppItemTextKey, 1, $item, $key);
      if (!$itemText) $itemText = $itemValue;
      $options[] = array(
        "value" => $itemValue,
        "text" => $itemText,
        "selected" => $value == $itemValue,
      );
    }

    $control = [];
    $control[] = v::start("select", [
      "id" => $this->ppId,
      "name" => $name,
      "value" => $value,
      "readonly" => $this->ppReadonly? "readonly": false,
      "disabled" => $this->ppDisabled? "disabled": false,
      "required" => $this->ppRequired? "required": false,
      "class" => ["form-control", $this->ppClass],
      "style" => $this->ppStyle,
      $this->ppAttrs,
    ]);
    foreach ($options as $option) {
      $control[] = $this->ppOptionPrefix;
      $control[] = v::tag("option", [
        "value" => $option["value"],
        "selected" => $option["selected"]? "selected": false,
        q($option["text"]),
      ]);
      $control[] = $this->ppOptionSuffix;
    }
    $control[] = v::end("select");
    if ($this->ppNaked) return $control;

    $label = $this->ppLabel;
    if ($label && $this->ppRequired && $this->ppShowRequired) {
      $label = [q($label), " <sup class=\"required\" title=\"required\">(*)</sup>"];
    } else {
      $label = q($label);
    }

    [$fgClass, $feedback] = $this->getStdFeedback($name, $value);
    return $this->getFglcLayout($label, true, $control, $fgClass, $feedback);
  }
}