<?php
namespace nulib\schema\_scalar;

use nulib\schema\input\Input;
use nulib\tests\TestCase;
use nulib\ValueException;
use stdClass;

class ScalarWrapperTest extends TestCase {
  function checkValue(ScalarWrapper $wrapper, $value, bool $present, bool $available, bool $valid, bool $normalized): void {
    self::assertSame($value, $wrapper->get(), "value");
    self::assertSame($present, $wrapper->isPresent(), "present");
    self::assertSame($available, $wrapper->isAvailable(), "available");
    self::assertSame($valid, $wrapper->isValid(), "valid");
    self::assertSame($normalized, $wrapper->isNormalized(), "normalized");
  }

  function checkNormalize(ScalarSchema $schema, $orig, bool $normalize, $value, bool $present, bool $available, bool $valid, bool $normalized, ?array $inputParams=null): void {
    $wrapper = $schema->getWrapper();
    $wrapper->resetParams(["normalize" => $normalize]);
    if ($inputParams !== null) $input = new Input($orig, $inputParams);
    else $input = $orig;
    $wrapper->reset($input);
    $this->checkValue($wrapper, $value, $present, $available, $valid, $normalized);
  }

  function checkException(ScalarSchema $schema, $orig, bool $normalize, string $exceptionClass, ?array $inputParams=null) {
    $wrapper = $schema->getWrapper();
    if ($inputParams !== null) $orig = new Input($orig, $inputParams);
    self::assertException($exceptionClass, function() use ($wrapper, &$orig, $normalize) {
      $wrapper->resetParams(["normalize" => $normalize]);
      $wrapper->reset($orig);
    });
  }

  function testRaw() {
    $schema = new ScalarSchema();

    $this->checkNormalize($schema, false, false, false, true, true, true, true);
    $this->checkNormalize($schema, false, true, false, true, true, true, true);

    $this->checkNormalize($schema, null, false, null, true, true, true, true);
    $this->checkNormalize($schema, null, true, null, true, true, true, true);

    $obj = new stdClass();
    $this->checkNormalize($schema, $obj, false, $obj, true, true, true, true);
    $this->checkNormalize($schema, $obj, true, $obj, true, true, true, true);

    $schema = new ScalarSchema("raw");

    $this->checkNormalize($schema, false, false, false, true, true, true, true);
    $this->checkNormalize($schema, false, true, false, true, true, true, true);

    $this->checkNormalize($schema, null, false, null, true, true, true, true);
    $this->checkNormalize($schema, null, true, null, true, true, true, true);

    $obj = new stdClass();
    $this->checkNormalize($schema, $obj, false, $obj, true, true, true, true);
    $this->checkNormalize($schema, $obj, true, $obj, true, true, true, true);
  }

  function testMixed() {
    $schema = new ScalarSchema("mixed");

    $this->checkNormalize($schema, false, false, false, true, true, true, true);
    $this->checkNormalize($schema, false, true, false, true, true, true, true);

    $this->checkNormalize($schema, null, false, null, true, true, false, false);
    $this->checkException($schema, null, true, ValueException::class);

    $obj = new stdClass();
    $this->checkNormalize($schema, $obj, false, $obj, true, true, true, true);
    $this->checkNormalize($schema, $obj, true, $obj, true, true, true, true);

    $schema = new ScalarSchema("?mixed");

    $this->checkNormalize($schema, false, false, false, true, true, true, true);
    $this->checkNormalize($schema, false, true, false, true, true, true, true);

    $this->checkNormalize($schema, null, false, null, true, true, true, true);
    $this->checkNormalize($schema, null, true, null, true, true, true, true);

    $obj = new stdClass();
    $this->checkNormalize($schema, $obj, false, $obj, true, true, true, true);
    $this->checkNormalize($schema, $obj, true, $obj, true, true, true, true);
  }

  function testRawstring() {
    $schema = new ScalarSchema("rawstring");

    $this->checkNormalize($schema, false, false, null, true, false, true, true);
    $this->checkNormalize($schema, false, true, null, true, false, true, true);

    $this->checkNormalize($schema, null, false, null, true, true, false, false);
    $this->checkException($schema, null, true, ValueException::class);

    $this->checkNormalize($schema, "", false, "", true, true, true, true);
    $this->checkNormalize($schema, "", true, "", true, true, true, true);

    $this->checkNormalize($schema, "   ", false, "   ", true, true, true, true);
    $this->checkNormalize($schema, "   ", true, "   ", true, true, true, true);

    $this->checkNormalize($schema, "text", false, "text", true, true, true, true);
    $this->checkNormalize($schema, "text", true, "text", true, true, true, true);

    $this->checkNormalize($schema, "  text  ", false, "  text  ", true, true, true, true);
    $this->checkNormalize($schema, "  text  ", true, "  text  ", true, true, true, true);

    $this->checkNormalize($schema, true, false, true, true, true, true, false);
    $this->checkNormalize($schema, true, true, "1", true, true, true, false);

    $this->checkNormalize($schema, 42, false, 42, true, true, true, false);
    $this->checkNormalize($schema, 42, true, "42", true, true, true, false);

    $this->checkNormalize($schema, [], false, [], true, true, false, false);
    $this->checkException($schema, [], true, ValueException::class);

    ## Tester valeur par défaut
    $schema = new ScalarSchema(["rawstring", null]);

    $this->checkNormalize($schema, false, false, null, true, false, true, true);
    $this->checkNormalize($schema, false, true, null, true, false, true, true);

    $this->checkNormalize($schema, null, false, null, true, true, false, false);
    $this->checkException($schema, null, true, ValueException::class);

    $schema = new ScalarSchema(["rawstring", "default"]);

    $this->checkNormalize($schema, false, false, "default", true, true, true, true);
    $this->checkNormalize($schema, false, true, "default", true, true, true, true);

    $this->checkNormalize($schema, null, false, null, true, true, false, false);
    $this->checkException($schema, null, true, ValueException::class);

    ## Tester nullable
    $schema = new ScalarSchema("?rawstring");

    $this->checkNormalize($schema, null, false, null, true, true, true, true);
    $this->checkNormalize($schema, null, true, null, true, true, true, true);

    ## Tester required
    $schema = new ScalarSchema(["rawstring", "required" => true]);

    $this->checkNormalize($schema, false, false, null, true, false, false, false);
    $this->checkException($schema, false, true, ValueException::class);

    ## Tester allow_empty === false
    $inputParams = ["allow_empty" => false];
    $schema = new ScalarSchema("rawstring");

    $this->checkNormalize($schema, null, false, null, true, true, false, false, $inputParams);
    $this->checkException($schema, null, true, ValueException::class, $inputParams);

    $this->checkNormalize($schema, "", false, null, true, false, true, true, $inputParams);
    $this->checkNormalize($schema, "", true, null, true, false, true, true, $inputParams);

    $schema = new ScalarSchema("?rawstring");

    $this->checkNormalize($schema, null, false, null, true, true, true, true, $inputParams);
    $this->checkNormalize($schema, null, true, null, true, true, true, true, $inputParams);

    $this->checkNormalize($schema, "", false, null, true, false, true, true, $inputParams);
    $this->checkNormalize($schema, "", true, null, true, false, true, true, $inputParams);
  }

  function testString() {
    $schema = new ScalarSchema("string");

    $this->checkNormalize($schema, false, false, null, true, false, true, true);
    $this->checkNormalize($schema, false, true, null, true, false, true, true);

    $this->checkNormalize($schema, null, false, null, true, true, false, false);
    $this->checkException($schema, null, true, ValueException::class);

    $this->checkNormalize($schema, "", false, "", true, true, true, true);
    $this->checkNormalize($schema, "", true, "", true, true, true, true);

    $this->checkNormalize($schema, "   ", false, "", true, true, true, false);
    $this->checkNormalize($schema, "   ", true, "", true, true, true, false);

    $this->checkNormalize($schema, "text", false, "text", true, true, true, true);
    $this->checkNormalize($schema, "text", true, "text", true, true, true, true);

    $this->checkNormalize($schema, "  text  ", false, "text", true, true, true, false);
    $this->checkNormalize($schema, "  text  ", true, "text", true, true, true, false);

    $this->checkNormalize($schema, true, false, true, true, true, true, false);
    $this->checkNormalize($schema, true, true, "1", true, true, true, false);

    $this->checkNormalize($schema, 42, false, 42, true, true, true, false);
    $this->checkNormalize($schema, 42, true, "42", true, true, true, false);

    $this->checkNormalize($schema, [], false, [], true, true, false, false);
    $this->checkException($schema, [], true, ValueException::class);

    ## Tester nullable
    $schema = new ScalarSchema("?string");

    $this->checkNormalize($schema, null, false, null, true, true, true, true);
    $this->checkNormalize($schema, null, true, null, true, true, true, true);

    ## Tester required
    $schema = new ScalarSchema(["string", "required" => true]);

    $this->checkNormalize($schema, false, false, null, true, false, false, false);
    $this->checkException($schema, false, true, ValueException::class);

    ## Tester allow_empty === false
    $inputParams = ["allow_empty" => false];
    $schema = new ScalarSchema("string");

    $this->checkNormalize($schema, null, false, null, true, true, false, false, $inputParams);
    $this->checkException($schema, null, true, ValueException::class, $inputParams);

    $this->checkNormalize($schema, "", false, null, true, false, true, true, $inputParams);
    $this->checkNormalize($schema, "", true, null, true, false, true, true, $inputParams);

    $schema = new ScalarSchema("?string");

    $this->checkNormalize($schema, null, false, null, true, true, true, true, $inputParams);
    $this->checkNormalize($schema, null, true, null, true, true, true, true, $inputParams);

    $this->checkNormalize($schema, "", false, null, true, false, true, true, $inputParams);
    $this->checkNormalize($schema, "", true, null, true, false, true, true, $inputParams);
  }

  function testInt() {
    $schema = new ScalarSchema("int");

    $this->checkNormalize($schema, false, false, null, true, false, true, true);
    $this->checkNormalize($schema, false, true, null, true, false, true, true);

    $this->checkNormalize($schema, null, false, null, true, true, false, false);
    $this->checkException($schema, null, true, ValueException::class);

    $this->checkNormalize($schema, 42, false, 42, true, true, true, true);
    $this->checkNormalize($schema, 42, true, 42, true, true, true, true);

    $this->checkNormalize($schema, "42", false, "42", true, true, true, false);
    $this->checkNormalize($schema, "42", true, 42, true, true, true, false);

    $this->checkNormalize($schema, "42.5", false, "42.5", true, true, true, false);
    $this->checkNormalize($schema, "42.5", true, 42, true, true, true, false);

    $this->checkNormalize($schema, "42,5", false, "42,5", true, true, true, false);
    $this->checkNormalize($schema, "42,5", true, 42, true, true, true, false);

    $this->checkNormalize($schema, "  42  ", false, "42", true, true, true, false);
    $this->checkNormalize($schema, "  42  ", true, 42, true, true, true, false);

    $this->checkNormalize($schema, "", false, "", true, true, false, false);
    $this->checkException($schema, "", true, ValueException::class);

    $this->checkNormalize($schema, "   ", false, "   ", true, true, false, false);
    $this->checkException($schema, "   ", true, ValueException::class);

    $this->checkNormalize($schema, "text", false, "text", true, true, false, false);
    $this->checkException($schema, "text", true, ValueException::class);

    $this->checkNormalize($schema, true, false, true, true, true, true, false);
    $this->checkNormalize($schema, true, true, 1, true, true, true, false);

    $this->checkNormalize($schema, [], false, [], true, true, false, false);
    $this->checkException($schema, [], true, ValueException::class);

    ## Tester nullable
    $schema = new ScalarSchema("?int");

    $this->checkNormalize($schema, null, false, null, true, true, true, true);
    $this->checkNormalize($schema, null, true, null, true, true, true, true);

    ## Tester required
    $schema = new ScalarSchema(["int", "required" => true]);

    $this->checkNormalize($schema, false, false, null, true, false, false, false);
    $this->checkException($schema, false, true, ValueException::class);

    ## Tester allow_empty === false
    $inputParams = ["allow_empty" => false];
    $schema = new ScalarSchema("int");

    $this->checkNormalize($schema, null, false, null, true, true, false, false, $inputParams);
    $this->checkException($schema, null, true, ValueException::class, $inputParams);

    $this->checkNormalize($schema, "", false, null, true, false, true, true, $inputParams);
    $this->checkNormalize($schema, "", true, null, true, false, true, true, $inputParams);

    $schema = new ScalarSchema("?int");

    $this->checkNormalize($schema, null, false, null, true, true, true, true, $inputParams);
    $this->checkNormalize($schema, null, true, null, true, true, true, true, $inputParams);

    $this->checkNormalize($schema, "", false, null, true, false, true, true, $inputParams);
    $this->checkNormalize($schema, "", true, null, true, false, true, true, $inputParams);
  }
}
