<?php
namespace nur\sery\wip\schema\_scalar;

use nulib\tests\TestCase;
use nulib\ValueException;
use nur\sery\wip\schema\input\Input;
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 checkVerifix(ScalarSchema $schema, $orig, bool $verifix, $value, bool $present, bool $available, bool $valid, bool $normalized, ?array $inputParams=null): void {
    $wrapper = $schema->getWrapper();
    if ($inputParams !== null) $input = new Input($orig, $inputParams);
    else $input = $orig;
    $wrapper->reset($input, null, $verifix);
    $this->checkValue($wrapper, $value, $present, $available, $valid, $normalized);
  }

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

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

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

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

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

    $schema = new ScalarSchema("raw");

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    $this->checkVerifix($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->checkVerifix($schema, null, false, null, true, true, false, false, $inputParams);
    $this->checkException($schema, null, true, ValueException::class, $inputParams);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    $this->checkVerifix($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->checkVerifix($schema, null, false, null, true, true, false, false, $inputParams);
    $this->checkException($schema, null, true, ValueException::class, $inputParams);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    $this->checkVerifix($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->checkVerifix($schema, null, false, null, true, true, false, false, $inputParams);
    $this->checkException($schema, null, true, ValueException::class, $inputParams);

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

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

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

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