<?php
namespace nur\mapper\item;

use nur\mapper\base\mappers_command;
use nur\mapper\base\Tparams_command;

/**
 * Class ItemFilterMapper_command: classe de support pour construire une
 * instance de mapper pour une application recevant des commandes utilisateurs
 */
class ItemFilterMapper_command {
  use Tparams_command;

  const NAME = "seli";
  const DEF = [self::NAME, 
    "help" => "filtrer des éléments",
    "action" => [self::class, "create_command"],
    "cmd_args" => [
      "usage" => self::NAME,
      ["group",
        ["-a", "--action", "args" => "value",
          "action" => [self::class, "set_action"],
        ],
        ["-X", "--action-filter", "value" => "filter",
          "action" => [self::class, "set_action"],
        ],
        ["-K", "--action-keep", "value" => "keep",
          "action" => [self::class, "set_action"],
        ],
        ["-M", "--action-mark", "value" => "mark",
          "action" => [self::class, "set_action"],
        ],
      ],
      ["-s", "--skip-items", "args" => "value", "type" => "int",
        "action" => [self::class, "set_skip_items"],
      ],
      ["-m", "--max-count", "args" => "value", "type" => "int",
        "action" => [self::class, "set_max_count"],
      ],
      ["-o", "--param", "args" => "value",
        "action" => [self::class, "add_params"],
        "help" => "spécifier une option générique",
      ],
      ["args" => [["value", null]],
        "action" => [self::class, "add_conditions"],
      ],
    ],
  ];

  /** @var ItemFilterMapper */
  protected static $command;

  static function create_command() {
    self::$command = mappers_command::add(new ItemFilterMapper());
  }

  static function set_action(string $action) {
    self::get()->setAction($action);
  }

  static function set_skip_items(int $skip_items) {
    self::get()->setSkipItems($skip_items);
  }

  static function set_max_count(int $max_count) {
    self::get()->setMaxCount($max_count);
  }

  static function add_conditions(array $conditions) {
    $command = self::get();
    foreach ($conditions as $condition) {
      $command->addCondition($condition);
    }
  }
}
