<?php

namespace {
  function func36(): int { return 36; }
  
  function func_m1($a): array { return [$a]; }
  function func_o1($b=9): array { return [$b]; }
  function func_v(...$c): array { return [...$c]; }
  function func_m1o1($a, $b=9): array { return [$a, $b]; }
  function func_m1v($a, ...$c): array { return [$a, ...$c]; }
  function func_m1o1v($a, $b=9, ...$c): array { return [$a, $b, ...$c]; }
  function func_o1v($b=9, ...$c): array { return [$b, ...$c]; }
}

namespace nur\sery\php {

  use nulib\tests\TestCase;

  class nur_funcTest extends TestCase {
    function testIs_static() {
      self::assertFalse(nur_func::is_static(null));
      self::assertFalse(nur_func::is_static(""));
      self::assertFalse(nur_func::is_static("::"));
      self::assertFalse(nur_func::is_static("xxx::"));
      self::assertFalse(nur_func::is_static([]));
      self::assertFalse(nur_func::is_static([""]));
      self::assertFalse(nur_func::is_static([null, ""]));
      self::assertFalse(nur_func::is_static(["xxx", ""]));

      self::assertTrue(nur_func::is_static("::xxx"));
      self::assertTrue(nur_func::is_static(["xxx"]));
      self::assertTrue(nur_func::is_static([null, "yyy"]));
      self::assertTrue(nur_func::is_static(["xxx", "yyy"]));
      self::assertTrue(nur_func::is_static([null, "yyy", "aaa"]));
      self::assertTrue(nur_func::is_static(["xxx", "yyy", "aaa"]));
    }
    
    function testFix_static() {
      $class = "class";
      $func = "::xxx";
        nur_func::fix_static($func, $class);
        self::assertSame("class::xxx", $func);
      $func = ["xxx"];
        nur_func::fix_static($func, $class);
        self::assertSame(["class", "xxx"], $func);
      $func = [null, "yyy"];
        nur_func::fix_static($func, $class);
        self::assertSame(["class", "yyy"], $func);
      $func = ["xxx", "yyy"];
        nur_func::fix_static($func, $class);
        self::assertSame(["xxx", "yyy"], $func);
      $func = [null, "yyy", "aaa"];
        nur_func::fix_static($func, $class);
        self::assertSame(["class", "yyy", "aaa"], $func);
      $func = ["xxx", "yyy", "aaa"];
        nur_func::fix_static($func, $class);
        self::assertSame(["xxx", "yyy", "aaa"], $func);
    }

    function testIs_method() {
      self::assertFalse(nur_func::is_method(null));
      self::assertFalse(nur_func::is_method(""));
      self::assertFalse(nur_func::is_method("->"));
      self::assertFalse(nur_func::is_method([]));
      self::assertFalse(nur_func::is_method([""]));
      self::assertFalse(nur_func::is_method([null, "->"]));
      self::assertFalse(nur_func::is_method(["xxx", "->"]));

      self::assertTrue(nur_func::is_method("->xxx"));
      self::assertTrue(nur_func::is_method(["->xxx"]));
      self::assertTrue(nur_func::is_method([null, "->yyy"]));
      self::assertTrue(nur_func::is_method(["xxx", "->yyy"]));
      self::assertTrue(nur_func::is_method([null, "->yyy", "aaa"]));
      self::assertTrue(nur_func::is_method(["xxx", "->yyy", "aaa"]));
    }

    function testFix_method() {
      $object = new \stdClass();
      $func= "->xxx";
        nur_func::fix_method($func, $object);
        self::assertSame([$object, "xxx"], $func);
      $func= ["->xxx"];
        nur_func::fix_method($func, $object);
        self::assertSame([$object, "xxx"], $func);
      $func= [null, "->yyy"];
        nur_func::fix_method($func, $object);
        self::assertSame([$object, "yyy"], $func);
      $func= ["xxx", "->yyy"];
        nur_func::fix_method($func, $object);
        self::assertSame([$object, "yyy"], $func);
      $func= [null, "->yyy", "aaa"];
        nur_func::fix_method($func, $object);
        self::assertSame([$object, "yyy", "aaa"], $func);
      $func= ["xxx", "->yyy", "aaa"];
        nur_func::fix_method($func, $object);
        self::assertSame([$object, "yyy", "aaa"], $func);
    }

    function testCall() {
      self::assertSame(36, nur_func::call("func36"));
      self::assertSame(12, nur_func::call(TC::class."::method"));
      self::assertSame(12, nur_func::call([TC::class, "method"]));
      $closure = function() {
        return 21;
      };
      self::assertSame(21, nur_func::call($closure));
    }

    function test_prepare_fill() {
      # vérifier que les arguments sont bien remplis, en fonction du fait qu'ils
      # soient obligatoires, facultatifs ou variadiques
      
      # m1
      self::assertSame([null], nur_func::call("func_m1"));
      self::assertSame([null], nur_func::call("func_m1", null));
      self::assertSame([null], nur_func::call("func_m1", null, null));
      self::assertSame([null], nur_func::call("func_m1", null, null, null));
      self::assertSame([null], nur_func::call("func_m1", null, null, null, null));
      self::assertSame([1], nur_func::call("func_m1", 1));
      self::assertSame([1], nur_func::call("func_m1", 1, 2));
      self::assertSame([1], nur_func::call("func_m1", 1, 2, 3));
      self::assertSame([1], nur_func::call("func_m1", 1, 2, 3, 4));
      
      # o1
      self::assertSame([9], nur_func::call("func_o1"));
      self::assertSame([null], nur_func::call("func_o1", null));
      self::assertSame([null], nur_func::call("func_o1", null, null));
      self::assertSame([null], nur_func::call("func_o1", null, null, null));
      self::assertSame([null], nur_func::call("func_o1", null, null, null, null));
      self::assertSame([1], nur_func::call("func_o1", 1));
      self::assertSame([1], nur_func::call("func_o1", 1, 2));
      self::assertSame([1], nur_func::call("func_o1", 1, 2, 3));
      self::assertSame([1], nur_func::call("func_o1", 1, 2, 3, 4));
      
      # v
      self::assertSame([], nur_func::call("func_v"));
      self::assertSame([null], nur_func::call("func_v", null));
      self::assertSame([null, null], nur_func::call("func_v", null, null));
      self::assertSame([null, null, null], nur_func::call("func_v", null, null, null));
      self::assertSame([null, null, null, null], nur_func::call("func_v", null, null, null, null));
      self::assertSame([1], nur_func::call("func_v", 1));
      self::assertSame([1, 2], nur_func::call("func_v", 1, 2));
      self::assertSame([1, 2, 3], nur_func::call("func_v", 1, 2, 3));
      self::assertSame([1, 2, 3, 4], nur_func::call("func_v", 1, 2, 3, 4));
      
      # m1o1
      self::assertSame([null, 9], nur_func::call("func_m1o1"));
      self::assertSame([null, 9], nur_func::call("func_m1o1", null));
      self::assertSame([null, null], nur_func::call("func_m1o1", null, null));
      self::assertSame([null, null], nur_func::call("func_m1o1", null, null, null));
      self::assertSame([null, null], nur_func::call("func_m1o1", null, null, null, null));
      self::assertSame([1, 9], nur_func::call("func_m1o1", 1));
      self::assertSame([1, 2], nur_func::call("func_m1o1", 1, 2));
      self::assertSame([1, 2], nur_func::call("func_m1o1", 1, 2, 3));
      self::assertSame([1, 2], nur_func::call("func_m1o1", 1, 2, 3, 4));
      
      # m1v
      self::assertSame([null], nur_func::call("func_m1v"));
      self::assertSame([null], nur_func::call("func_m1v", null));
      self::assertSame([null, null], nur_func::call("func_m1v", null, null));
      self::assertSame([null, null, null], nur_func::call("func_m1v", null, null, null));
      self::assertSame([null, null, null, null], nur_func::call("func_m1v", null, null, null, null));
      self::assertSame([1], nur_func::call("func_m1v", 1));
      self::assertSame([1, 2], nur_func::call("func_m1v", 1, 2));
      self::assertSame([1, 2, 3], nur_func::call("func_m1v", 1, 2, 3));
      self::assertSame([1, 2, 3, 4], nur_func::call("func_m1v", 1, 2, 3, 4));
      
      # m1o1v
      self::assertSame([null, 9], nur_func::call("func_m1o1v"));
      self::assertSame([null, 9], nur_func::call("func_m1o1v", null));
      self::assertSame([null, null], nur_func::call("func_m1o1v", null, null));
      self::assertSame([null, null, null], nur_func::call("func_m1o1v", null, null, null));
      self::assertSame([null, null, null, null], nur_func::call("func_m1o1v", null, null, null, null));
      self::assertSame([1, 9], nur_func::call("func_m1o1v", 1));
      self::assertSame([1, 2], nur_func::call("func_m1o1v", 1, 2));
      self::assertSame([1, 2, 3], nur_func::call("func_m1o1v", 1, 2, 3));
      self::assertSame([1, 2, 3, 4], nur_func::call("func_m1o1v", 1, 2, 3, 4));
      
      # o1v
      self::assertSame([9], nur_func::call("func_o1v"));
      self::assertSame([null], nur_func::call("func_o1v", null));
      self::assertSame([null, null], nur_func::call("func_o1v", null, null));
      self::assertSame([null, null, null], nur_func::call("func_o1v", null, null, null));
      self::assertSame([null, null, null, null], nur_func::call("func_o1v", null, null, null, null));
      self::assertSame([1], nur_func::call("func_o1v", 1));
      self::assertSame([1, 2], nur_func::call("func_o1v", 1, 2));
      self::assertSame([1, 2, 3], nur_func::call("func_o1v", 1, 2, 3));
      self::assertSame([1, 2, 3, 4], nur_func::call("func_o1v", 1, 2, 3, 4));
    }

    function testCall_all() {
      $c1 = new C1();
      $c2 = new C2();
      $c3 = new C3();

      self::assertSameValues([11, 12], nur_func::call_all(C1::class));
      self::assertSameValues([11, 12, 21, 22], nur_func::call_all($c1));
      self::assertSameValues([13, 11, 12], nur_func::call_all(C2::class));
      self::assertSameValues([13, 23, 11, 12, 21, 22], nur_func::call_all($c2));
      self::assertSameValues([111, 13, 12], nur_func::call_all(C3::class));
      self::assertSameValues([111, 121, 13, 23, 12, 22], nur_func::call_all($c3));

      $options = "conf";
      self::assertSameValues([11], nur_func::call_all(C1::class, $options));
      self::assertSameValues([11, 21], nur_func::call_all($c1, $options));
      self::assertSameValues([11], nur_func::call_all(C2::class, $options));
      self::assertSameValues([11, 21], nur_func::call_all($c2, $options));
      self::assertSameValues([111], nur_func::call_all(C3::class, $options));
      self::assertSameValues([111, 121], nur_func::call_all($c3, $options));

      $options = ["prefix" => "conf"];
      self::assertSameValues([11], nur_func::call_all(C1::class, $options));
      self::assertSameValues([11, 21], nur_func::call_all($c1, $options));
      self::assertSameValues([11], nur_func::call_all(C2::class, $options));
      self::assertSameValues([11, 21], nur_func::call_all($c2, $options));
      self::assertSameValues([111], nur_func::call_all(C3::class, $options));
      self::assertSameValues([111, 121], nur_func::call_all($c3, $options));

      self::assertSameValues([11, 12], nur_func::call_all($c1, ["include" => "x"]));
      self::assertSameValues([11, 21], nur_func::call_all($c1, ["include" => "y"]));
      self::assertSameValues([11, 12, 21], nur_func::call_all($c1, ["include" => ["x", "y"]]));

      self::assertSameValues([21, 22], nur_func::call_all($c1, ["exclude" => "x"]));
      self::assertSameValues([12, 22], nur_func::call_all($c1, ["exclude" => "y"]));
      self::assertSameValues([22], nur_func::call_all($c1, ["exclude" => ["x", "y"]]));

      self::assertSameValues([12], nur_func::call_all($c1, ["include" => "x", "exclude" => "y"]));
    }

    function testCons() {
      $obj1 = nur_func::cons(WoCons::class, 1, 2, 3);
      self::assertInstanceOf(WoCons::class, $obj1);

      $obj2 = nur_func::cons(WithEmptyCons::class, 1, 2, 3);
      self::assertInstanceOf(WithEmptyCons::class, $obj2);

      $obj3 = nur_func::cons(WithCons::class, 1, 2, 3);
      self::assertInstanceOf(WithCons::class, $obj3);
      self::assertSame(1, $obj3->first);
    }
  }

  class WoCons {
  }
  class WithEmptyCons {
    function __construct() {
    }
  }
  class WithCons {
    public $first;
    function __construct($first) {
      $this->first = $first;
    }
  }

  class TC {
    static function method() {
      return 12;
    }
  }

  class C1 {
    static function confps1_xy() {
      return 11;
    }
    static function ps2_x() {
      return 12;
    }
    function confp1_y() {
      return 21;
    }
    function p2() {
      return 22;
    }
  }
  class C2 extends C1 {
    static function ps3() {
      return 13;
    }
    function p3() {
      return 23;
    }
  }
  class C3 extends C2 {
    static function confps1_xy() {
      return 111;
    }
    function confp1_y() {
      return 121;
    }
  }
}
